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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Update in C# using SQL Server Database.

Update in C# using SQL Server Database.

Scheduled Pinned Locked Moved C#
csharpdatabasehelpsharepointsql-server
50 Posts 4 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.
  • N Norris Chappell

    Thanks for pointing that out to me. One last thing do my parameters look right. Should I I removed this too? cmd.Parameters.Clear();

    A Offline
    A Offline
    Agent__007
    wrote on last edited by
    #41

    Norris Chappell wrote:

    Should I I removed this too? cmd.Parameters.Clear();

    Since you are not inside a loop anymore, you may want to remove that line. But that won't matter much as the SqlCommand is going to be disposed anyway (at the end of the using block). Also, since you are not using the foreach anymore, you may want to remove its "{ }" braces as well, as they are forming an unnecessary code-block at the moment.

    You have just been Sharapova'd.

    N 1 Reply Last reply
    0
    • A Agent__007

      Norris Chappell wrote:

      Should I I removed this too? cmd.Parameters.Clear();

      Since you are not inside a loop anymore, you may want to remove that line. But that won't matter much as the SqlCommand is going to be disposed anyway (at the end of the using block). Also, since you are not using the foreach anymore, you may want to remove its "{ }" braces as well, as they are forming an unnecessary code-block at the moment.

      You have just been Sharapova'd.

      N Offline
      N Offline
      Norris Chappell
      wrote on last edited by
      #42

      GridViewRow row = gvKeyPersonnel.Rows[e.RowIndex]; is giving me this error: it doesn't like the e.RowIndex 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' could be found (are you missing a using directive or an assembly reference?)

      A 1 Reply Last reply
      0
      • N Norris Chappell

        GridViewRow row = gvKeyPersonnel.Rows[e.RowIndex]; is giving me this error: it doesn't like the e.RowIndex 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewUpdatedEventArgs' could be found (are you missing a using directive or an assembly reference?)

        A Offline
        A Offline
        Agent__007
        wrote on last edited by
        #43

        If you are using GridViewUpdatedEventArgs e as a parameter to the gvKeyPersonnel_RowUpdating method/handler, then it's wrong. The method signature should look like this:

        protected void gvKeyPersonnel_RowUpdating(object sender, GridViewUpdateEventArgs e)

        that is, you initially had it correct. Note that it's GridViewUpdateEventArgs in the parameter. Don't get confused. The OnRowUpdating event that you already have should used to perform actual update (this you already have in place), where as the OnRowUpdated event I just suggested should be used to perform something "after" you have updated a row, like notify the user, rebind the grid, etc.

        You have just been Sharapova'd.

        N 1 Reply Last reply
        0
        • A Agent__007

          If you are using GridViewUpdatedEventArgs e as a parameter to the gvKeyPersonnel_RowUpdating method/handler, then it's wrong. The method signature should look like this:

          protected void gvKeyPersonnel_RowUpdating(object sender, GridViewUpdateEventArgs e)

          that is, you initially had it correct. Note that it's GridViewUpdateEventArgs in the parameter. Don't get confused. The OnRowUpdating event that you already have should used to perform actual update (this you already have in place), where as the OnRowUpdated event I just suggested should be used to perform something "after" you have updated a row, like notify the user, rebind the grid, etc.

          You have just been Sharapova'd.

          N Offline
          N Offline
          Norris Chappell
          wrote on last edited by
          #44

          Made that change but when I ran my code I got this error again: No overload for 'gvKeyPersonnel_RowUpdating' matches delegate 'System.EventHandler'

          A 1 Reply Last reply
          0
          • N Norris Chappell

            Made that change but when I ran my code I got this error again: No overload for 'gvKeyPersonnel_RowUpdating' matches delegate 'System.EventHandler'

            A Offline
            A Offline
            Agent__007
            wrote on last edited by
            #45

            Hmm, not sure. Can you show your grid's markup and "gvKeyPersonnel_RowUpdating" method again?

            You have just been Sharapova'd.

            N 2 Replies Last reply
            0
            • A Agent__007

              Hmm, not sure. Can you show your grid's markup and "gvKeyPersonnel_RowUpdating" method again?

              You have just been Sharapova'd.

              N Offline
              N Offline
              Norris Chappell
              wrote on last edited by
              #46

              protected void gvKeyPersonnel_RowUpdating(object sender, GridViewUpdateEventArgs e)
              {
              using (SqlCommand cmd = new SqlCommand())
              {

                          cmd.Connection = conn;
                          conn.Open();
                        
              
                          GridViewRow row =  gvKeyPersonnel.Rows\[e.RowIndex\];
                          // foreach (GridViewRow row in gvKeyPersonnel.Rows)
              
              
                          cmd.CommandText = "UPDATE SP2010\_EDCStaffing\_AppDB.dbo.CMS\_Key\_Personnel  SET  Name = @Name, VDCIDIQ = @VDCIDIQ, VDCFFS = @VDCFFS, VDCHIM = @VDCHIM, VDCWEBHOSTING = @VDCWEBHOSTING, VDCCWF = @VDCCWF WHERE ID = @id";
              
                          cmd.Parameters.AddWithValue("@id", Convert.ToInt32(gvKeyPersonnel.DataKeys\[row.RowIndex\].Values\[0\]));
                          cmd.Parameters.AddWithValue("@Name", row.Cells\[1\].Text);
                          cmd.Parameters.AddWithValue("@VDCIDIQ", row.Cells\[2\].Text);
                          cmd.Parameters.AddWithValue("@VDCFFS", row.Cells\[3\].Text);
                          cmd.Parameters.AddWithValue("@VDCHIM", row.Cells\[4\].Text);
                          cmd.Parameters.AddWithValue("@VDCWEBHOSTING", row.Cells\[5\].Text);
                          cmd.Parameters.AddWithValue("@VDCCWF", row.Cells\[6\].Text);
                          //cmd.Parameters.AddWithValue("@id", Convert.ToInt32(gvKeyPersonnel.DataKeys\[row.RowIndex\].Values\[0\]));
                          //cmd.Parameters.AddWithValue("@Name", Name.Text);
                          //cmd.Parameters.AddWithValue("@VDCIDIQ", VDCIDIQ.Text);
                          //cmd.Parameters.AddWithValue("@VDCFFS", VDCFFS.Text);
                          //cmd.Parameters.AddWithValue("@VDCHIM", VDCHIM.Text);
                          //cmd.Parameters.AddWithValue("@VDCWEBHOSTING", VDCWEBHOSTING.Text);
                          //cmd.Parameters.AddWithValue("@VDCCWF", VDCCWF.Text);
              
                          cmd.ExecuteNonQuery();
                          //   cmd.Parameters.Clear();
                      }
              
                      conn.Close();
                  }
              
              1 Reply Last reply
              0
              • A Agent__007

                Hmm, not sure. Can you show your grid's markup and "gvKeyPersonnel_RowUpdating" method again?

                You have just been Sharapova'd.

                N Offline
                N Offline
                Norris Chappell
                wrote on last edited by
                #47
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                
                A 1 Reply Last reply
                0
                • N Norris Chappell
                  <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                  <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                  
                  A Offline
                  A Offline
                  Agent__007
                  wrote on last edited by
                  #48

                  You are using the same event handler for handling ButtonClick event, that's not going to work. GridView's OnRowUpdating requires GridViewUpdateEventArgs type of EventArgs where as a Button's click requires EventArgs. You might get away with casting, but I won't recommend that. Also, I don't see any anything for handling the "Edit" operation. Note that, you generally have an Update on a grid only if you also have an Edit (AFAIK). Have a look at this[^] example on MSDN. Also search for something like "ASP.NET GridView inline edit and update." That should clear things up. All the best. :thumbsup:

                  You have just been Sharapova'd.

                  N 1 Reply Last reply
                  0
                  • A Agent__007

                    You are using the same event handler for handling ButtonClick event, that's not going to work. GridView's OnRowUpdating requires GridViewUpdateEventArgs type of EventArgs where as a Button's click requires EventArgs. You might get away with casting, but I won't recommend that. Also, I don't see any anything for handling the "Edit" operation. Note that, you generally have an Update on a grid only if you also have an Edit (AFAIK). Have a look at this[^] example on MSDN. Also search for something like "ASP.NET GridView inline edit and update." That should clear things up. All the best. :thumbsup:

                    You have just been Sharapova'd.

                    N Offline
                    N Offline
                    Norris Chappell
                    wrote on last edited by
                    #49

                    Hi, I have finally got the edit, update and delete to work. However, for the update can you have more than 1 set statement with the update command? I want to thanks everyone for there help and comments.

                    using System;
                    using System.Data;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Web;
                    using System.Web.UI;
                    using System.Web.UI.WebControls;
                    using System.Data.Sql;
                    using System.Data.SqlClient;
                    using System.Configuration;
                    using System.Web.UI.WebControls.WebParts;

                    namespace StaffingWebParts.keypernew
                    {
                    public partial class keypernewUserControl : UserControl
                    {
                    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
                    SqlCommand cmd = new SqlCommand();
                    protected void Page_Load(object sender, EventArgs e)
                    {
                    if (!IsPostBack)
                    {
                    KeyPersonnel();
                    }
                    }
                    protected void KeyPersonnel()
                    {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("Select id, Name from CMS_Key_Personnel", conn);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    int count = ds.Tables[0].Rows.Count;
                    conn.Close();
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                    gvKeyPersonnel.DataSource = ds;
                    gvKeyPersonnel.DataBind();
                    }
                    else
                    {
                    ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                    gvKeyPersonnel.DataSource = ds;
                    gvKeyPersonnel.DataBind();
                    int columncount = gvKeyPersonnel.Rows[0].Cells.Count;
                    lblmsg.Text = " No data found !!!";
                    }
                    }
                    protected void gvKeyPersonnel_RowEditing(object sender, GridViewEditEventArgs e)
                    {
                    gvKeyPersonnel.EditIndex = e.NewEditIndex;
                    KeyPersonnel();
                    }
                    protected void gvKeyPersonnel_RowUpdating(object sender, GridViewUpdateEventArgs e)
                    {

                           // string id = gvKeyPersonnel.DataKeys\[e.RowIndex\].Values\["id"\].ToString();
                            int id = Convert.ToInt32(gvKeyPersonnel.DataKeys\[e.RowIndex\].Value);
                    
                            TextBox Name = (TextBox)gvKeyPersonnel.Rows\[e.RowIndex\].FindControl("txtName");
                            //    TextBox stor\_address = (TextBox)gvKeyPersonnel.Rows\[e.RowIndex\].FindControl("txtaddress");
                            //
                    
                    N 1 Reply Last reply
                    0
                    • N Norris Chappell

                      Hi, I have finally got the edit, update and delete to work. However, for the update can you have more than 1 set statement with the update command? I want to thanks everyone for there help and comments.

                      using System;
                      using System.Data;
                      using System.Collections.Generic;
                      using System.Linq;
                      using System.Web;
                      using System.Web.UI;
                      using System.Web.UI.WebControls;
                      using System.Data.Sql;
                      using System.Data.SqlClient;
                      using System.Configuration;
                      using System.Web.UI.WebControls.WebParts;

                      namespace StaffingWebParts.keypernew
                      {
                      public partial class keypernewUserControl : UserControl
                      {
                      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
                      SqlCommand cmd = new SqlCommand();
                      protected void Page_Load(object sender, EventArgs e)
                      {
                      if (!IsPostBack)
                      {
                      KeyPersonnel();
                      }
                      }
                      protected void KeyPersonnel()
                      {
                      conn.Open();
                      SqlCommand cmd = new SqlCommand("Select id, Name from CMS_Key_Personnel", conn);
                      SqlDataAdapter da = new SqlDataAdapter(cmd);
                      DataSet ds = new DataSet();
                      da.Fill(ds);
                      int count = ds.Tables[0].Rows.Count;
                      conn.Close();
                      if (ds.Tables[0].Rows.Count > 0)
                      {
                      gvKeyPersonnel.DataSource = ds;
                      gvKeyPersonnel.DataBind();
                      }
                      else
                      {
                      ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                      gvKeyPersonnel.DataSource = ds;
                      gvKeyPersonnel.DataBind();
                      int columncount = gvKeyPersonnel.Rows[0].Cells.Count;
                      lblmsg.Text = " No data found !!!";
                      }
                      }
                      protected void gvKeyPersonnel_RowEditing(object sender, GridViewEditEventArgs e)
                      {
                      gvKeyPersonnel.EditIndex = e.NewEditIndex;
                      KeyPersonnel();
                      }
                      protected void gvKeyPersonnel_RowUpdating(object sender, GridViewUpdateEventArgs e)
                      {

                             // string id = gvKeyPersonnel.DataKeys\[e.RowIndex\].Values\["id"\].ToString();
                              int id = Convert.ToInt32(gvKeyPersonnel.DataKeys\[e.RowIndex\].Value);
                      
                              TextBox Name = (TextBox)gvKeyPersonnel.Rows\[e.RowIndex\].FindControl("txtName");
                              //    TextBox stor\_address = (TextBox)gvKeyPersonnel.Rows\[e.RowIndex\].FindControl("txtaddress");
                              //
                      
                      N Offline
                      N Offline
                      Norris Chappell
                      wrote on last edited by
                      #50

                      This one can be closed. Thanks to everyone that helped me get though this project.

                      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