How To Update A Column In A DataGrid Using C#.NET?

I am getting InvalidCastException as (Specified cast is not
valid) while updating 2nd column in a datagrid?
Id,firstname,lastname are the three columns of my datagrid
respectively. I wanted to edit the second column(lastname)
and update it. I did the following code in DataGrid's
updatecommand(),but failed to update !

Int varid=(int)DataGrid1.DataKeys[e.Item.ItemIndex];
TextBox lnm=(TextBox)e.Item.Cells[2].Controls[0]; string
str=lnm.Text ; SqlCommand cmd=new SqlCommand("update
customer set lastname='" + str + "' where id=" + varid
+ "",con); cmd.ExecuteNonQuery(); DataGrid1.EditItemIndex=-
1; DataGrid1.DataBind();

Answers were Sorted based on User's Feedback



How To Update A Column In A DataGrid Using C#.NET? I am getting InvalidCastException as (Specifie..

Answer / aravazhi

Try this query you can avoid InvalidCastException...

string strQry = "update customer set lastname=@LastName
where id=@VarId";
SqlCommand cmd=new SqlCommand(strQry,con);
int varid = (int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
string LName = ((TextBox)e.Item.FondControl
("txtLName")).Text;//txtLname is ID of control
cmd.Paramters.Add(new SqlParameter("@LastName",LName));
cmd.Paramters.Add(new SqlParameter("@VarId",varid));
cmd.ExecuteNonQuery();
DataGrid1.EditItemIndex=- 1;
DataGrid1.DataBind();

Is This Answer Correct ?    15 Yes 4 No

How To Update A Column In A DataGrid Using C#.NET? I am getting InvalidCastException as (Specifie..

Answer / pk

public void DataGrid1_Update(Object sender,
DataGridCommandEventArgs e)
{

string unitprice =
((TextBox)e.Item.Cells[3].Controls[0]).Text;

string quantity =
((TextBox)e.Item.Cells[4].Controls[0]).Text;

string discount =
((TextBox)e.Item.Cells[5].Controls[0]).Text;

int orderid =
(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];

string productid =
((TextBox)e.Item.Cells[2].Controls[0]).Text;

try
{

string updateCmd = "UPDATE [Order Details] SET
UnitPrice = @UnitPrice,"

+ "Quantity = @Quantity, Discount = @Discount
where OrderId =@OrderId and ProductId=@ProductId";

SqlConnection cn = new SqlConnection(strConn);

SqlCommand myCommand = new SqlCommand(updateCmd,
cn);

myCommand.Parameters.Add(new
SqlParameter("@UnitPrice", Convert.ToDecimal(unitprice)));

myCommand.Parameters.Add(new
SqlParameter("@Quantity", Convert.ToInt16(quantity)));

myCommand.Parameters.Add(new
SqlParameter("@Discount", Convert.ToInt16(discount)));

myCommand.Parameters.Add(new
SqlParameter("@OrderId", orderid));

myCommand.Parameters.Add(new
SqlParameter("@ProductId", Convert.ToInt16(productid)));

cn.Open();

myCommand.ExecuteNonQuery();

DataGrid1.EditItemIndex = -1;

BindGrid();

}

catch (Exception ex)
{

lblError.Visible = true;

lblError.Text = (ex.Message);

}

Is This Answer Correct ?    2 Yes 5 No

Post New Answer

More ADO.NET Interview Questions

About ADO and its objects ?

1 Answers   DELL,


What are the ADO.NET Controls?

6 Answers  


What are the important features of ado.net 2.0?

0 Answers  


What is difference between ado.net and asp net?

0 Answers  


We all know that Dataset is purely disconnected architechure, but we also know that we can update the changes made to the dataset can be updated in the backend database. when there is no connection how does the update happedn?

6 Answers   Accenture, FastStream,






what is sql Injection?

4 Answers   Microsoft,


What is a sqldataadapter?

0 Answers  


What is the difference between executequery and executenonquery?

0 Answers  


Which method do you invoke on the DataAdapter control to load your generated dataset with data?

4 Answers  


How can we add/remove row's in "datatable" object of "dataset"?

0 Answers  


Command objects uses, purposes and their methods.

0 Answers  


What is DataReader Object?

0 Answers  


Categories