Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Sql Updat Problem using DataGrid please help

Sql Updat Problem using DataGrid please help

Scheduled Pinned Locked Moved ASP.NET
helpdatabasecsharpsysadmindebugging
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jacal99
    wrote on last edited by
    #1

    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>

    P 1 Reply Last reply
    0
    • J jacal99

      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>

      P Offline
      P Offline
      Paul Watson
      wrote on last edited by
      #2

      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?

      J 1 Reply Last reply
      0
      • P Paul Watson

        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?

        J Offline
        J Offline
        jacal99
        wrote on last edited by
        #3

        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

        P 1 Reply Last reply
        0
        • J jacal99

          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

          P Offline
          P Offline
          Paul Watson
          wrote on last edited by
          #4

          You need a where clause in your update command. You have Update 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 use uid 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?

          J 1 Reply Last reply
          0
          • P Paul Watson

            You need a where clause in your update command. You have Update 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 use uid 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?

            J Offline
            J Offline
            jacal99
            wrote on last edited by
            #5

            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

            P 1 Reply Last reply
            0
            • J jacal99

              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

              P Offline
              P Offline
              Paul Watson
              wrote on last edited by
              #6

              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?

              J 1 Reply Last reply
              0
              • P Paul Watson

                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?

                J Offline
                J Offline
                jacal99
                wrote on last edited by
                #7

                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

                P 1 Reply Last reply
                0
                • J jacal99

                  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

                  P Offline
                  P Offline
                  Paul Watson
                  wrote on last edited by
                  #8

                  >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?

                  J 1 Reply Last reply
                  0
                  • P Paul Watson

                    >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?

                    J Offline
                    J Offline
                    jacal99
                    wrote on last edited by
                    #9

                    ok Paul thx for your flash reply ... i am going to my college and i will try it as you said. ok Tc. bye Mazhar Hussain

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups