Click here to Skip to main content
15,887,776 members
Home / Discussions / C#
   

C#

 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell11-May-15 20:09
Norris Chappell11-May-15 20:09 
GeneralRe: Update in C# using SQL Server Database. Pin
Agent__00711-May-15 20:30
professionalAgent__00711-May-15 20:30 
GeneralRe: Update in C# using SQL Server Database. Pin
Mycroft Holmes11-May-15 22:32
professionalMycroft Holmes11-May-15 22:32 
GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon12-May-15 0:26
mvePete O'Hanlon12-May-15 0:26 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell12-May-15 4:27
Norris Chappell12-May-15 4:27 
GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon12-May-15 4:49
mvePete O'Hanlon12-May-15 4:49 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell12-May-15 11:36
Norris Chappell12-May-15 11:36 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell12-May-15 11:55
Norris Chappell12-May-15 11:55 
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.
C#
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;
                gvKeyPersonnel.DataBind();
                if (gvKeyPersonnel.Columns.Count > 0)
                    gvKeyPersonnel.Columns[0].Visible = false;
                else
                {
                    gvKeyPersonnel.HeaderRow.Cells[0].Visible = false;
                    foreach (GridViewRow gvr in gvKeyPersonnel.Rows)
                    {
                        gvr.Cells[0].Visible = false;
                    }
                }
                conn.Close();
            }
        }
    //    //protected void SubmitButton_Click(object sender, EventArgs e)
    //    //{
    //    //    SqlCommand cmd = new SqlCommand();
    //    //    cmd.Connection = conn;
    //    //    conn.Open();

    //    //    foreach (GridViewRow row in gvKeyPersonnel.Rows)
    //    //    {

    //    //        cmd.CommandText = " UPDATE CMS_Key_Personnel (Name, VDCIDIQ, VDCFFS, VDCHIM, VDCWEBHOSTING, VDCCWF) " + " VALUES ('" + ((TextBox)row.FindControl("txtName")).Text + "', '"
    //    //            + ((TextBox)row.FindControl("txtVDCIDIQ")).Text + "', '" + ((TextBox)row.FindControl("txtVDCFFS")).Text + "', '" + ((TextBox)row.FindControl("txtVDCHIM")).Text + "', '" +
    //    //       ((TextBox)row.FindControl("txtVDCWEBHOSTING")).Text + "', '" + ((TextBox)row.FindControl("txtVDCCWF")).Text + "')";

    //    //        int numRegs = cmd.ExecuteNonQuery();
    //    //    }
        //    //    conn.Close();



        //    //    gvKeyPersonnel.EditIndex = -1;// no row in edit mode
        protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
               cmd.Connection = conn;
                conn.Open();
                // SqlDataReader myReader = null;
                // myReader = cmd.ExecuteReader();
                
                
                foreach (GridViewRow row in gvKeyPersonnel.Rows)
                {
               
                  SqlDataAdapter da = new SqlDataAdapter("", conn);
                       
                   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.ExecuteNonQuery();
                }
                conn.Close();
            }
        }


    }

}

GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon12-May-15 13:45
mvePete O'Hanlon12-May-15 13:45 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell12-May-15 13:51
Norris Chappell12-May-15 13:51 
GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon12-May-15 19:57
mvePete O'Hanlon12-May-15 19:57 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell12-May-15 20:02
Norris Chappell12-May-15 20:02 
GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon12-May-15 21:00
mvePete O'Hanlon12-May-15 21:00 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell12-May-15 21:55
Norris Chappell12-May-15 21:55 
GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon12-May-15 22:17
mvePete O'Hanlon12-May-15 22:17 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 3:48
Norris Chappell13-May-15 3:48 
GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon13-May-15 4:11
mvePete O'Hanlon13-May-15 4:11 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 5:17
Norris Chappell13-May-15 5:17 
GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon13-May-15 5:57
mvePete O'Hanlon13-May-15 5:57 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 6:43
Norris Chappell13-May-15 6:43 
GeneralRe: Update in C# using SQL Server Database. Pin
Pete O'Hanlon13-May-15 7:15
mvePete O'Hanlon13-May-15 7:15 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 8:28
Norris Chappell13-May-15 8:28 
GeneralRe: Update in C# using SQL Server Database. Pin
Agent__00713-May-15 17:02
professionalAgent__00713-May-15 17:02 
GeneralRe: Update in C# using SQL Server Database. Pin
Norris Chappell13-May-15 18:01
Norris Chappell13-May-15 18:01 
GeneralRe: Update in C# using SQL Server Database. Pin
Agent__00713-May-15 18:11
professionalAgent__00713-May-15 18:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.