How to update Update record in a dataview [Resolved]

Posted by Akiii under C# on 4/29/2011 | Points: 10 | Views : 4771 | Status : [Member] | Replies : 4
Hi,
I want to update a record in my dataview. But it is not working. The code is...

string connstr = "Data Source=ARKA-PC;Initial Catalog=rgdb;User Id = sa;password = 12345";

SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd;
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataView dv;

try
{
conn.Open();
cmd = new SqlCommand("select * from reg_table", conn);
sda.SelectCommand = cmd;
sda.Fill(ds, "reg_table");
sda.Dispose();
cmd.Dispose();
conn.Close();

dv = new DataView(ds.Tables[0], "", "country", DataViewRowState.CurrentRows);

int index = dv.Find("'yemen'");

if (index == -1)
{
Response.Write("country not found");
}
else
{
dv[index]["country"] = "germany";
Response.Write("country Updated !");
}


GridView1.DataSource = dv;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}


Can anybody help ??

Thanks
Akiii




Responses

Posted by: Prabhakar on: 4/29/2011 [Member] [MVP] Starter | Points: 50

Up
0
Down

Resolved
Hi akkii


Correct this line . .


wrong : int index = dv.Find("'yemen'");

Right :
int index = dv.Find("yemen");
remove single quotation mark in dv.find()





Best Regard's
Prabhakar

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 4/29/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Akiii

. . what's the error come on ur code show it .. and which line . .


Best Regard's
Prabhakar

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 4/29/2011 [Member] Bronze | Points: 25

Up
0
Down
hi prabhakar....

There is no error but it is not fetching any data......
I want to fetch data from the dataview(if found) then i want to update it......

int index = dv.Find("'yemen'");




if (index == -1)

{

Response.Write("country not found");

}

else

{

dv[index]["country"] = "germany";

Response.Write("country Updated !");

}


Tell me one thing, changes in a dataview will reflect back in the database or it will remain with the dataview only ??

Thanks
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 4/29/2011 [Member] Bronze | Points: 25

Up
0
Down
hey thanks prabhakar........

now its fetching the data........it was my mistake.......

i mark it as an answer....

Thanks
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response