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

    It appears I don't because I not getting that OOR error any more. I getting:The variable name '@id' has already been declared. Variable names must be unique within a query batch or stored procedure. Do I need to make any field Label instead of TextBox, The ID field is being is being hidden. I really don't know what is wrong now. Here is my present code. Maybe by looking at all of the code you might can spot something I missed. I have been working on this 4 straight days.

    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.KeyPerTest
    {
    public partial class KeyPerTestUserControl : UserControl
    {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
    SqlCommand cmd = new SqlCommand();

        protected void Page\_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("ID");
                dt.Columns.Add("Name");
                dt.Columns.Add("VDCIDIQ");
                dt.Columns.Add("VDCFFS");
                dt.Columns.Add("VDCHIM");
                dt.Columns.Add("VDCWEBHOSTING");
                dt.Columns.Add("VDCCWF");
                SqlDataReader myReader = null;
                SqlCommand cmd = new SqlCommand("SELECT ID, Name, VDCIDIQ , VDCFFS, VDCHIM, VDCWEBHOSTING, VDCCWF  from CMS\_Key\_Personnel where Name <> '   ' order by  Name");
                cmd.Connection = conn;
                conn.Open();
                myReader = cmd.ExecuteReader();
                while (myReader.Read())
                {
                    DataRow dr = dt.NewRow();
                    dr\[0\] = myReader\["ID"\].ToString();
                    dr\[1\] = myReader\["Name"\].ToString();
                    dr\[2\] = myReader\["VDCIDIQ"\].ToString();
                    dr\[3\] = myReader\["VDCFFS"\].ToString();
                    dr\[4\] = myReader\["VDCHIM"\].ToString();
                    dr\[5\] = myReader\["VDCWEBHOSTING"\].ToString();
                    dr\[6\] = myReader\["VDCCWF"\].ToString();
                    dt.Rows.Add(dr);
                }
                
                gvKeyPersonnel.DataSource = dt;
    
    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #20

    The reason you're getting this exception is because you're attempting to add the parameters to the same command object inside a loop. This will work on the first trip through the loop, but will fail on the next trip through. A way around this is to do cmd.Parameters.Clear(); before you start adding the parameters in. This will ensure the parameters collection has no entries on each journey through the loop.

    N 1 Reply Last reply
    0
    • P Pete OHanlon

      The reason you're getting this exception is because you're attempting to add the parameters to the same command object inside a loop. This will work on the first trip through the loop, but will fail on the next trip through. A way around this is to do cmd.Parameters.Clear(); before you start adding the parameters in. This will ensure the parameters collection has no entries on each journey through the loop.

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

      Okay that got rid of that error. Thanks but my update delete all of the fields. I now have in my database the id's number of all of the previous rows. How can I just update the database with what I have in my datgridview?

      P 1 Reply Last reply
      0
      • N Norris Chappell

        Okay that got rid of that error. Thanks but my update delete all of the fields. I now have in my database the id's number of all of the previous rows. How can I just update the database with what I have in my datgridview?

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #22

        As I don't know what keys are on either side of the equation here, I can't help you. It's up to you now. Step through the code and note the values that you're seeing - in particular, what's getting assigned to @ID. That's the one that's causing you issues, so that's the line that you are going to have to inspect. It's pretty apparent that the wrong value is being written to that field - you are in the best position to see what that value is.

        N 1 Reply Last reply
        0
        • P Pete OHanlon

          As I don't know what keys are on either side of the equation here, I can't help you. It's up to you now. Step through the code and note the values that you're seeing - in particular, what's getting assigned to @ID. That's the one that's causing you issues, so that's the line that you are going to have to inspect. It's pretty apparent that the wrong value is being written to that field - you are in the best position to see what that value is.

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

          Thanks for your help. I am now writing out a record to the database. The problem is writing the same first record over 42 times. You are correct the @ID is the culprit. I got to figure how to increment it.

          P 1 Reply Last reply
          0
          • N Norris Chappell

            Thanks for your help. I am now writing out a record to the database. The problem is writing the same first record over 42 times. You are correct the @ID is the culprit. I got to figure how to increment it.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #24

            Looking at your code, you have put the ID in cell 0 and made it invisible. Why not just use the contents of that cell for your ID instead?

            cmd.Parameters.AddWithValue("@id", row.Cells[0].Text));
            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);

            N 1 Reply Last reply
            0
            • P Pete OHanlon

              Looking at your code, you have put the ID in cell 0 and made it invisible. Why not just use the contents of that cell for your ID instead?

              cmd.Parameters.AddWithValue("@id", row.Cells[0].Text));
              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);

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

              Sorry that didn't work. It started loaded at the second record. and loaded the database with that record only.

              P 1 Reply Last reply
              0
              • N Norris Chappell

                Sorry that didn't work. It started loaded at the second record. and loaded the database with that record only.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #26

                Post your updated code.

                N 1 Reply Last reply
                0
                • P Pete OHanlon

                  Post your updated code.

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

                  protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)
                  {
                  int @ID = Convert.ToInt32(gvKeyPersonnel.DataKeys[0].Value.ToString());
                  TextBox @Name = (TextBox)gvKeyPersonnel.Rows[0].FindControl("txtName");
                  TextBox @VDCIDIQ = (TextBox)gvKeyPersonnel.Rows[1].FindControl("txtVDCIDIQ");
                  TextBox @VDCFFS = (TextBox)gvKeyPersonnel.Rows[2].FindControl("txtVDCFFS");
                  TextBox @VDCHIM = (TextBox)gvKeyPersonnel.Rows[3].FindControl("txtVDCHIM");
                  TextBox @VDCWEBHOSTING = (TextBox)gvKeyPersonnel.Rows[4].FindControl("txtVDCWEBHOSTING");
                  TextBox @VDCCWF = (TextBox)gvKeyPersonnel.Rows[5].FindControl("txtVDCCWF");

                          using (SqlCommand cmd = new SqlCommand())
                          {
                             cmd.Connection = conn;
                              conn.Open();
                              // SqlDataReader myReader = null;
                              // myReader = cmd.ExecuteReader();
                              
                                  
                              
                              foreach (GridViewRow row in gvKeyPersonnel.Rows)
                            //    for (int i = 0; i < gvKeyPersonnel.Rows.Count; i++)
                                    
                                {
                                    
                                   
                                 
                                 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", row.Cells\[0\].Text);
                                 //cmd.Parameters.AddWithValue("@Name", row.Cells\[0\].Text);
                                 //cmd.Parameters.AddWithValue("@VDCIDIQ", row.Cells\[1\].Text);
                                 //cmd.Parameters.AddWithValue("@VDCFFS", row.Cells\[2\].Text);
                                 //cmd.Parameters.AddWithValue("@VDCHIM", row.Cells\[3\].Text);
                                 //cmd.Parameters.AddWithValue("@VDCWEBHOSTING", row.Cells\[4\].Text);
                                 //cmd.Parameters.AddWithValue("@VDCCWF", row.Cells\[5\].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.AddWit
                  
                  P 1 Reply Last reply
                  0
                  • N Norris Chappell

                    protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)
                    {
                    int @ID = Convert.ToInt32(gvKeyPersonnel.DataKeys[0].Value.ToString());
                    TextBox @Name = (TextBox)gvKeyPersonnel.Rows[0].FindControl("txtName");
                    TextBox @VDCIDIQ = (TextBox)gvKeyPersonnel.Rows[1].FindControl("txtVDCIDIQ");
                    TextBox @VDCFFS = (TextBox)gvKeyPersonnel.Rows[2].FindControl("txtVDCFFS");
                    TextBox @VDCHIM = (TextBox)gvKeyPersonnel.Rows[3].FindControl("txtVDCHIM");
                    TextBox @VDCWEBHOSTING = (TextBox)gvKeyPersonnel.Rows[4].FindControl("txtVDCWEBHOSTING");
                    TextBox @VDCCWF = (TextBox)gvKeyPersonnel.Rows[5].FindControl("txtVDCCWF");

                            using (SqlCommand cmd = new SqlCommand())
                            {
                               cmd.Connection = conn;
                                conn.Open();
                                // SqlDataReader myReader = null;
                                // myReader = cmd.ExecuteReader();
                                
                                    
                                
                                foreach (GridViewRow row in gvKeyPersonnel.Rows)
                              //    for (int i = 0; i < gvKeyPersonnel.Rows.Count; i++)
                                      
                                  {
                                      
                                     
                                   
                                   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", row.Cells\[0\].Text);
                                   //cmd.Parameters.AddWithValue("@Name", row.Cells\[0\].Text);
                                   //cmd.Parameters.AddWithValue("@VDCIDIQ", row.Cells\[1\].Text);
                                   //cmd.Parameters.AddWithValue("@VDCFFS", row.Cells\[2\].Text);
                                   //cmd.Parameters.AddWithValue("@VDCHIM", row.Cells\[3\].Text);
                                   //cmd.Parameters.AddWithValue("@VDCWEBHOSTING", row.Cells\[4\].Text);
                                   //cmd.Parameters.AddWithValue("@VDCCWF", row.Cells\[5\].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.AddWit
                    
                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #28

                    Whoah. Okay, that's really not going to work. You load the TextBoxes with one set of values and then you assign them on each iteration through the foreach. That's just going to set the values to the same values and I doubt that's what you want. You need to stop and have a think about what you're trying to achieve rather than just throwing things together in the hope that they will work. Write out the steps on a bit of paper and then code that up. Drop the TextBox approach here - it's just not going to work.

                    N 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Whoah. Okay, that's really not going to work. You load the TextBoxes with one set of values and then you assign them on each iteration through the foreach. That's just going to set the values to the same values and I doubt that's what you want. You need to stop and have a think about what you're trying to achieve rather than just throwing things together in the hope that they will work. Write out the steps on a bit of paper and then code that up. Drop the TextBox approach here - it's just not going to work.

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

                      Okay thanks. I am a newbie to C#. Don't I need to use Label instead of TextBox in ASP.Net GridView control?

                      P 1 Reply Last reply
                      0
                      • N Norris Chappell

                        Okay thanks. I am a newbie to C#. Don't I need to use Label instead of TextBox in ASP.Net GridView control?

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #30

                        It seems you have two separate issues that you're trying to fix here. The first is how to save the data from your GridView and the second one is how to display it/edit it. Is that correct? Let's solve the first one - by assuming that you have somehow managed to display and edit the data back into the GridView. If we assume that, then you use the approach I outlined to perform your save. Don't try to save directly off the TextBoxes as that only applies to saving a single row. So, follow the code I outlined using the value in cell 0 to get your Id and save off that.

                        N 1 Reply Last reply
                        0
                        • P Pete OHanlon

                          It seems you have two separate issues that you're trying to fix here. The first is how to save the data from your GridView and the second one is how to display it/edit it. Is that correct? Let's solve the first one - by assuming that you have somehow managed to display and edit the data back into the GridView. If we assume that, then you use the approach I outlined to perform your save. Don't try to save directly off the TextBoxes as that only applies to saving a single row. So, follow the code I outlined using the value in cell 0 to get your Id and save off that.

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

                          Yes I can display my Gridview and edit it. When I click update that is where the issue is? You told me to cmd.Parameters.Clear(); Should that be in the foreach loop?

                          protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)
                          {

                                  using (SqlCommand cmd = new SqlCommand())
                                  {
                                     cmd.Connection = conn;
                                      conn.Open();
                                                         
                                      
                                      foreach (GridViewRow row in gvKeyPersonnel.Rows)
                                                         
                                        {
                          
                                         cmd.Parameters.Clear(); 
                                         
                                         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.ExecuteNonQuery();   
                                         cmd.Parameters.AddWithValue("@id", row.Cells\[0\].Text);
                                         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);
                                                            
                                          
                                         
                                      }
                                      conn.Close();
                                  }
                              }
                          

                          I must be missing something here? Thanks for your help and looking at it?

                          P A 2 Replies Last reply
                          0
                          • N Norris Chappell

                            Yes I can display my Gridview and edit it. When I click update that is where the issue is? You told me to cmd.Parameters.Clear(); Should that be in the foreach loop?

                            protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)
                            {

                                    using (SqlCommand cmd = new SqlCommand())
                                    {
                                       cmd.Connection = conn;
                                        conn.Open();
                                                           
                                        
                                        foreach (GridViewRow row in gvKeyPersonnel.Rows)
                                                           
                                          {
                            
                                           cmd.Parameters.Clear(); 
                                           
                                           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.ExecuteNonQuery();   
                                           cmd.Parameters.AddWithValue("@id", row.Cells\[0\].Text);
                                           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);
                                                              
                                            
                                           
                                        }
                                        conn.Close();
                                    }
                                }
                            

                            I must be missing something here? Thanks for your help and looking at it?

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #32

                            Yes it should. You need that to ensure you don't try and add the same parameters in again.

                            N 1 Reply Last reply
                            0
                            • P Pete OHanlon

                              Yes it should. You need that to ensure you don't try and add the same parameters in again.

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

                              While in debug, I don't see any values in the fields? When I update all fields except the ID is deleted in the database. Don't I have to set the parameters first before using cmd.Parameters.AddWithValue?

                              1 Reply Last reply
                              0
                              • N Norris Chappell

                                Yes I can display my Gridview and edit it. When I click update that is where the issue is? You told me to cmd.Parameters.Clear(); Should that be in the foreach loop?

                                protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)
                                {

                                        using (SqlCommand cmd = new SqlCommand())
                                        {
                                           cmd.Connection = conn;
                                            conn.Open();
                                                               
                                            
                                            foreach (GridViewRow row in gvKeyPersonnel.Rows)
                                                               
                                              {
                                
                                               cmd.Parameters.Clear(); 
                                               
                                               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.ExecuteNonQuery();   
                                               cmd.Parameters.AddWithValue("@id", row.Cells\[0\].Text);
                                               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);
                                                                  
                                                
                                               
                                            }
                                            conn.Close();
                                        }
                                    }
                                

                                I must be missing something here? Thanks for your help and looking at it?

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

                                Ta da! You are executing the command "before" adding the parameters. Move

                                cmd.ExecuteNonQuery();

                                line after you have added the parameters. Also a tip: - Remove the foreach loop and add the below line instead:

                                GridViewRow row = gvKeyPersonnel.Rows[e.RowIndex];

                                Hope this helps.

                                You have just been Sharapova'd.

                                N 1 Reply Last reply
                                0
                                • A Agent__007

                                  Ta da! You are executing the command "before" adding the parameters. Move

                                  cmd.ExecuteNonQuery();

                                  line after you have added the parameters. Also a tip: - Remove the foreach loop and add the below line instead:

                                  GridViewRow row = gvKeyPersonnel.Rows[e.RowIndex];

                                  Hope this helps.

                                  You have just been Sharapova'd.

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

                                  I have the cmd.ExecuteNonQuery(); at the end But by removing the foreach loop and adding GridViewRow row = gvKeyPersonnel.Rows[e.RowIndex]; is giving me an error on RowIndexand if I put row instead of e it gives me a error on row. I still don't see any values in the debug. I have spend 5 days on this one and still don't see what is wrong.

                                  protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)
                                  {
                                  using (SqlCommand cmd = new SqlCommand())
                                  {
                                  cmd.Connection = conn;
                                  conn.Open();

                                                             //  foreach (GridViewRow row in gvKeyPersonnel.Rows)
                                                              GridViewRow row = gvKeyPersonnel.Rows\[e.RowIndex\];
                                                              {
                                  
                                                                  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("@id", row.Cells\[1\].Text);
                                                                  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);
                                  
                                  A 1 Reply Last reply
                                  0
                                  • N Norris Chappell

                                    I have the cmd.ExecuteNonQuery(); at the end But by removing the foreach loop and adding GridViewRow row = gvKeyPersonnel.Rows[e.RowIndex]; is giving me an error on RowIndexand if I put row instead of e it gives me a error on row. I still don't see any values in the debug. I have spend 5 days on this one and still don't see what is wrong.

                                    protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)
                                    {
                                    using (SqlCommand cmd = new SqlCommand())
                                    {
                                    cmd.Connection = conn;
                                    conn.Open();

                                                               //  foreach (GridViewRow row in gvKeyPersonnel.Rows)
                                                                GridViewRow row = gvKeyPersonnel.Rows\[e.RowIndex\];
                                                                {
                                    
                                                                    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("@id", row.Cells\[1\].Text);
                                                                    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);
                                    
                                    A Offline
                                    A Offline
                                    Agent__007
                                    wrote on last edited by
                                    #36

                                    Sorry, I should have mentioned this before. If the gvKeyPersonnel_RowUpdating() method/handler is for OnRowUpdating event of the gridview (which looks like is), i.e. if you have the following property for your grid (in .aspx) :

                                    OnRowUpdating="gvKeyPersonnel_RowUpdating"

                                    , then change the method's signature from

                                    protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)

                                    to

                                    protected void gvKeyPersonnel_RowUpdating(object sender, GridViewUpdateEventArgs e)

                                    That should do.

                                    You have just been Sharapova'd.

                                    N 1 Reply Last reply
                                    0
                                    • A Agent__007

                                      Sorry, I should have mentioned this before. If the gvKeyPersonnel_RowUpdating() method/handler is for OnRowUpdating event of the gridview (which looks like is), i.e. if you have the following property for your grid (in .aspx) :

                                      OnRowUpdating="gvKeyPersonnel_RowUpdating"

                                      , then change the method's signature from

                                      protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)

                                      to

                                      protected void gvKeyPersonnel_RowUpdating(object sender, GridViewUpdateEventArgs e)

                                      That should do.

                                      You have just been Sharapova'd.

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

                                      Yes I have OnRowUpdating="gvKeyPersonnel_RowUpdating property for my grid in aspx. Should I still removed foreach (GridViewRow row in gvKeyPersonnel.Rows). By the way how will it transverse through the datagridview with it?

                                      A 1 Reply Last reply
                                      0
                                      • N Norris Chappell

                                        Yes I have OnRowUpdating="gvKeyPersonnel_RowUpdating property for my grid in aspx. Should I still removed foreach (GridViewRow row in gvKeyPersonnel.Rows). By the way how will it transverse through the datagridview with it?

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

                                        Norris Chappell wrote:

                                        Should I still removed foreach (GridViewRow row in gvKeyPersonnel.Rows).

                                        Yes, you should. When you update any of the rows, it will fire up OnRowUpdating event of the grid and gvKeyPersonnel_RowUpdating() event handler will be called. Note that, since the event is firing up "only" for the row you are updating, you should only do the things related to that row (note that I said you "should" :)). Now for your question:

                                        Norris Chappell wrote:

                                        By the way how will it transverse through the datagridview with it?

                                        The answer is simple - you don't want to traverse through every row in the grid since you are updating only one (for which the event is fired) at any given time. Related: If you don't have it already, you may want to have a look at OnRowUpdated[^] event as well.

                                        You have just been Sharapova'd.

                                        N 1 Reply Last reply
                                        0
                                        • A Agent__007

                                          Norris Chappell wrote:

                                          Should I still removed foreach (GridViewRow row in gvKeyPersonnel.Rows).

                                          Yes, you should. When you update any of the rows, it will fire up OnRowUpdating event of the grid and gvKeyPersonnel_RowUpdating() event handler will be called. Note that, since the event is firing up "only" for the row you are updating, you should only do the things related to that row (note that I said you "should" :)). Now for your question:

                                          Norris Chappell wrote:

                                          By the way how will it transverse through the datagridview with it?

                                          The answer is simple - you don't want to traverse through every row in the grid since you are updating only one (for which the event is fired) at any given time. Related: If you don't have it already, you may want to have a look at OnRowUpdated[^] event as well.

                                          You have just been Sharapova'd.

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

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

                                          N A 2 Replies 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