Sql Updat Problem using DataGrid please help
-
hi i am getting error as under System.NullReferenceException: Object reference not set to an instance of an object error line is SqlCom.Connection.Open(); ------------------------ my all code is as under ------------------------ <%@ Page Language="C#" Debug="True" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> // Insert page code here SqlConnection SqlCon; void Page_Load(Object sender, EventArgs e) { SqlConnection sqlCon = new SqlConnection ("server = junaid; uid = sa; pwd = allah786; database = login"); if(!IsPostBack) BindGrid(); } public void dg_Edit(Object sender, DataGridCommandEventArgs e) { dg.Columns[0].HeaderText="Edit"; dg.EditItemIndex = (int)e.Item.ItemIndex; BindGrid(); } public void dg_Cancel(Object sender, DataGridCommandEventArgs e) { dg.Columns[0].HeaderText="Cancel"; dg.EditItemIndex = -1; BindGrid(); } public void dg_Update(Object sender, DataGridCommandEventArgs e) { //SqlConnection SqlCon = new SqlConnection ("server = junaid; uid = sa; pwd = allah786; database = login"); dg.Columns[0].HeaderText="Update"; string updateCmd = "Update users2 set username=@uid, upassword=@password, fname=@fname, lname=@lname, address=@address"; SqlCommand SqlCom = new SqlCommand(updateCmd, SqlCon); SqlCom.Parameters.Add(new SqlParameter("@uid",SqlDbType.VarChar, 50)); SqlCom.Parameters.Add(new SqlParameter("@password",SqlDbType.VarChar, 50)); SqlCom.Parameters.Add(new SqlParameter("@fname",SqlDbType.VarChar, 50)); SqlCom.Parameters.Add(new SqlParameter("@lname",SqlDbType.VarChar, 50)); SqlCom.Parameters.Add(new SqlParameter("@address",SqlDbType.VarChar, 50)); SqlCom.Parameters["@uid"].Value=((TextBox)e.Item.Cells[1].Controls[0]).Text; SqlCom.Parameters["@password"].Value=((TextBox)e.Item.Cells[2].Controls[0]).Text; SqlCom.Parameters["@fname"].Value=((TextBox)e.Item.Cells[3].Controls[0]).Text; SqlCom.Parameters["@lname"].Value=((TextBox)e.Item.Cells[4].Controls[0]).Text; SqlCom.Parameters["@address"].Value=((TextBox)e.Item.Cells[5].Controls[0]).Text; SqlCom.Connection.Open(); try { SqlCom.ExecuteNonQuery(); Response.Write("Record Updated"); dg.EditItemIndex = -1; } catch (SqlException exc) { Response.Write("There is an error..."); } } public void BindGr</x-turndown>
-
hi i am getting error as under System.NullReferenceException: Object reference not set to an instance of an object error line is SqlCom.Connection.Open(); ------------------------ my all code is as under ------------------------ <%@ Page Language="C#" Debug="True" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> // Insert page code here SqlConnection SqlCon; void Page_Load(Object sender, EventArgs e) { SqlConnection sqlCon = new SqlConnection ("server = junaid; uid = sa; pwd = allah786; database = login"); if(!IsPostBack) BindGrid(); } public void dg_Edit(Object sender, DataGridCommandEventArgs e) { dg.Columns[0].HeaderText="Edit"; dg.EditItemIndex = (int)e.Item.ItemIndex; BindGrid(); } public void dg_Cancel(Object sender, DataGridCommandEventArgs e) { dg.Columns[0].HeaderText="Cancel"; dg.EditItemIndex = -1; BindGrid(); } public void dg_Update(Object sender, DataGridCommandEventArgs e) { //SqlConnection SqlCon = new SqlConnection ("server = junaid; uid = sa; pwd = allah786; database = login"); dg.Columns[0].HeaderText="Update"; string updateCmd = "Update users2 set username=@uid, upassword=@password, fname=@fname, lname=@lname, address=@address"; SqlCommand SqlCom = new SqlCommand(updateCmd, SqlCon); SqlCom.Parameters.Add(new SqlParameter("@uid",SqlDbType.VarChar, 50)); SqlCom.Parameters.Add(new SqlParameter("@password",SqlDbType.VarChar, 50)); SqlCom.Parameters.Add(new SqlParameter("@fname",SqlDbType.VarChar, 50)); SqlCom.Parameters.Add(new SqlParameter("@lname",SqlDbType.VarChar, 50)); SqlCom.Parameters.Add(new SqlParameter("@address",SqlDbType.VarChar, 50)); SqlCom.Parameters["@uid"].Value=((TextBox)e.Item.Cells[1].Controls[0]).Text; SqlCom.Parameters["@password"].Value=((TextBox)e.Item.Cells[2].Controls[0]).Text; SqlCom.Parameters["@fname"].Value=((TextBox)e.Item.Cells[3].Controls[0]).Text; SqlCom.Parameters["@lname"].Value=((TextBox)e.Item.Cells[4].Controls[0]).Text; SqlCom.Parameters["@address"].Value=((TextBox)e.Item.Cells[5].Controls[0]).Text; SqlCom.Connection.Open(); try { SqlCom.ExecuteNonQuery(); Response.Write("Record Updated"); dg.EditItemIndex = -1; } catch (SqlException exc) { Response.Write("There is an error..."); } } public void BindGr</x-turndown>
You have commented out the connection:
public void dg_Update(Object sender, DataGridCommandEventArgs e)
{//SqlConnection SqlCon
:) regards, Paul Watson Bluegrass South Africa Chris Maunder wrote: "I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly." Jon Sagara replied: "I think we've all been in that situation before." Crikey! ain't life grand?
-
You have commented out the connection:
public void dg_Update(Object sender, DataGridCommandEventArgs e)
{//SqlConnection SqlCon
:) regards, Paul Watson Bluegrass South Africa Chris Maunder wrote: "I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly." Jon Sagara replied: "I think we've all been in that situation before." Crikey! ain't life grand?
Dear Sir ok you tell me to remove the comment i did and it is runing quite ok.... but when i try to update a single row and click the update link after that it updates all the rows in my sql database... you can see my update sql statement it is ok and i am using it as it is in my book. sir don't get angry on my mistakes because i am not that professional yet. Maz. Mazhar Hussain
-
Dear Sir ok you tell me to remove the comment i did and it is runing quite ok.... but when i try to update a single row and click the update link after that it updates all the rows in my sql database... you can see my update sql statement it is ok and i am using it as it is in my book. sir don't get angry on my mistakes because i am not that professional yet. Maz. Mazhar Hussain
You need a
where
clause in your update command. You haveUpdate users2 set username=@uid, upassword=@password, fname=@fname, lname=@lname, address=@address
which essentially tells SQL to update all records. I think you would need this:Update users2 set username=@uid, upassword=@password, fname=@fname, lname=@lname, address=@address **where ID = @ID**
This means you need a unique key in your table called ID. You can't useuid
because from what I can see you are letting your users change their username. The easiest unique key to do is a primary key field with auto-increment. Please note that I don't use databinding so the above may be wrong. regards, Paul Watson Bluegrass South Africa Chris Maunder wrote: "I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly." Jon Sagara replied: "I think we've all been in that situation before." Crikey! ain't life grand? -
You need a
where
clause in your update command. You haveUpdate users2 set username=@uid, upassword=@password, fname=@fname, lname=@lname, address=@address
which essentially tells SQL to update all records. I think you would need this:Update users2 set username=@uid, upassword=@password, fname=@fname, lname=@lname, address=@address **where ID = @ID**
This means you need a unique key in your table called ID. You can't useuid
because from what I can see you are letting your users change their username. The easiest unique key to do is a primary key field with auto-increment. Please note that I don't use databinding so the above may be wrong. regards, Paul Watson Bluegrass South Africa Chris Maunder wrote: "I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly." Jon Sagara replied: "I think we've all been in that situation before." Crikey! ain't life grand?Thank You Very Much Sir. :) sir one more question that i am getting the error in while deleteing data in sql database. i shall be ver thankful to you if you solve this one also.:) ------------------------------ System.Web.UI.WebControls.DataGridCommandEventArgs ------------------------------ my all code is as under ------------------------------ <%@ Page Language="C#" Debug="True" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> // Insert page code here SqlConnection SqlCon; void Page_Load(Object sender, EventArgs e) { lbl1.Text="Everything is just fine..."; SqlCon = new SqlConnection("server = junaid; uid = sa; pwd = allah786; database = login"); BindGrid(); } public void dg_delete(Object sender, DataGridCommandEventArgs e) { SqlCon = new SqlConnection("server = junaid; uid = sa; pwd = allah786; database = login"); string deleteCmd = "Delete from test1 WHERE username = @uid"; SqlCommand SqlCom = new SqlCommand(deleteCmd, SqlCon); SqlCom.Parameters.Add(new SqlParameter("@uid", SqlDbType.VarChar, 50)); //SqlCom.Parameters["@uid"].Value = dg1.DataKeys[(int)e.Item.ItemIndex]; SqlCom.Connection.Open(); try { SqlCom.ExecuteNonQuery(); lbl1.Text = "Record is Deleted... :) "; } catch(SqlException) { lbl1.Text = "Error in Sql Server or in Query actual erroe is as under .... <p> " + e ; } SqlCom.Connection.Close(); BindGrid(); } public void BindGrid() { SqlDataAdapter SqlCom = new SqlDataAdapter("select username, upassword, fname, lname, address from test1",SqlCon); DataSet ds = new DataSet(); SqlCom.Fill(ds, "test1"); dg1.DataSource = ds.Tables["test1"].DefaultView; dg1.DataBind(); }
Mazhar Hussain
-
Thank You Very Much Sir. :) sir one more question that i am getting the error in while deleteing data in sql database. i shall be ver thankful to you if you solve this one also.:) ------------------------------ System.Web.UI.WebControls.DataGridCommandEventArgs ------------------------------ my all code is as under ------------------------------ <%@ Page Language="C#" Debug="True" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> // Insert page code here SqlConnection SqlCon; void Page_Load(Object sender, EventArgs e) { lbl1.Text="Everything is just fine..."; SqlCon = new SqlConnection("server = junaid; uid = sa; pwd = allah786; database = login"); BindGrid(); } public void dg_delete(Object sender, DataGridCommandEventArgs e) { SqlCon = new SqlConnection("server = junaid; uid = sa; pwd = allah786; database = login"); string deleteCmd = "Delete from test1 WHERE username = @uid"; SqlCommand SqlCom = new SqlCommand(deleteCmd, SqlCon); SqlCom.Parameters.Add(new SqlParameter("@uid", SqlDbType.VarChar, 50)); //SqlCom.Parameters["@uid"].Value = dg1.DataKeys[(int)e.Item.ItemIndex]; SqlCom.Connection.Open(); try { SqlCom.ExecuteNonQuery(); lbl1.Text = "Record is Deleted... :) "; } catch(SqlException) { lbl1.Text = "Error in Sql Server or in Query actual erroe is as under .... <p> " + e ; } SqlCom.Connection.Close(); BindGrid(); } public void BindGrid() { SqlDataAdapter SqlCom = new SqlDataAdapter("select username, upassword, fname, lname, address from test1",SqlCon); DataSet ds = new DataSet(); SqlCom.Fill(ds, "test1"); dg1.DataSource = ds.Tables["test1"].DefaultView; dg1.DataBind(); }
Mazhar Hussain
What is the error you are getting? I see your try/catch but what is the innerexception it throws? Could you have relational constraints which prevent just deleting the user record? p.s. I am no sir :) regards, Paul Watson Bluegrass South Africa Chris Maunder wrote: "I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly." Jon Sagara replied: "I think we've all been in that situation before." Crikey! ain't life grand?
-
What is the error you are getting? I see your try/catch but what is the innerexception it throws? Could you have relational constraints which prevent just deleting the user record? p.s. I am no sir :) regards, Paul Watson Bluegrass South Africa Chris Maunder wrote: "I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly." Jon Sagara replied: "I think we've all been in that situation before." Crikey! ain't life grand?
i tell you that i have catch the exception and the error is as under it is not deleting the user record as i tell in the delete command and use the where clause. this is the error System.Web.UI.WebControls.DataGridCommandEventArgs please read my code carefully. no sir only friends :-O Mazhar Hussain
-
i tell you that i have catch the exception and the error is as under it is not deleting the user record as i tell in the delete command and use the where clause. this is the error System.Web.UI.WebControls.DataGridCommandEventArgs please read my code carefully. no sir only friends :-O Mazhar Hussain
>System.Web.UI.WebControls.DataGridCommandEventArgs That is not an error, that is a parameter. Here is what you should do; Comment out the try/catch block for now so that you can see the actual SQL error generated, not your custom error. Once you get the actual error, not yours, then we can help you more :) regards, Paul Watson Bluegrass South Africa Chris Maunder wrote: "I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly." Jon Sagara replied: "I think we've all been in that situation before." Crikey! ain't life grand?
-
>System.Web.UI.WebControls.DataGridCommandEventArgs That is not an error, that is a parameter. Here is what you should do; Comment out the try/catch block for now so that you can see the actual SQL error generated, not your custom error. Once you get the actual error, not yours, then we can help you more :) regards, Paul Watson Bluegrass South Africa Chris Maunder wrote: "I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly." Jon Sagara replied: "I think we've all been in that situation before." Crikey! ain't life grand?