Reputation: 1376
I have a datatable as
I want to replace all ESM of estm_suffix values with the a string which will come based on cust_Id. I am retrieving the string as
for (int i=0; i < dtRefNos.Rows.Count; i++)
{
intCustId = int.Parse(dtRefNos.Rows[i]["cust_Id"].ToString());
dsCustmr = custDCCls.FillCustDataSet(intCustId);
string shrtName = dsCustmr.Tables[0].Rows[0]["cust_Sname"].ToString();
}
No w I want to replace only ESM substring from estm_suffix with shrtName as ESM/00001/12-13 to shrtName/00001/12-13 and have to return the datatable. Please anybody help me.
Upvotes: 0
Views: 3134
Reputation: 223207
For your comment
each row with respective shrtNames
and
I dont want to replace all the value of estm_sufffix. I want to replace only ESM substring from ESM/00001/12-13
You can simply put one more line in your for
loop to replace estm_suffix
values with shrtName
using string.Replace, like:
for (int i=0; i < dtRefNos.Rows.Count; i++)
{
intCustId = int.Parse(dtRefNos.Rows[i]["cust_Id"].ToString());
dsCustmr = custDCCls.FillCustDataSet(intCustId);
string shrtName = dsCustmr.Tables[0].Rows[0]["cust_Sname"].ToString();
//New Line
dtRefNos.Rows[i]["estm_suffix"] = dtRefNos.Rows[i].Field<string>("estm_suffix")
.Replace("ESM", shrtName);
}
Upvotes: 2
Reputation: 258
Just use Regex.
using System.Text.RegularExpressions;
for (int i=0; i < dtRefNos.Rows.Count; i++)
{
intCustId = int.Parse(dtRefNos.Rows[i]["cust_Id"].ToString());
dsCustmr = custDCCls.FillCustDataSet(intCustId);
string shrtName = dsCustmr.Tables[0].Rows[0]["cust_Sname"].ToString();
string str = dtRefNos.Rows[i]["estm_suffix"].toString();
str = Regex.Replace(str, "ESM", dtRefNos.Rows[i]["shrtName"].toString());
dtRefNos.Rows[i]["estm_suffix"] = str;
}
Upvotes: 1