Click here to Skip to main content
15,896,278 members

How to Update, Insert, Gridview Values to SQl DB

Sreeraam_d asked:

Open original thread
Hi,

Am unable to Update and Insert the values from Gridview, so please anybody can help me to solve this problem.

Here is My Source C# Code, and i will post my Source code in nex page.

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        var EmpNo = GridView1.DataKeys[e.RowIndex].Value;
        //GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
        //TextBox EmpNo = GridView1.FooterRow.FindControl("txtEmpNo") as TextBox;
        TextBox EmpName = GridView1.FooterRow.FindControl("txtEmpName") as TextBox;
        DropDownList Designation = GridView1.FooterRow.FindControl("dropDesg") as DropDownList;
        DropDownList Department = GridView1.FooterRow.FindControl("ddlDep") as DropDownList;
        TextBox Salary = GridView1.FooterRow.FindControl("txtSal") as TextBox;
        int test = 0;
        int.TryParse(Salary.Text, out test);
        RadioButtonList Gender = GridView1.FooterRow.FindControl("RBLGender") as RadioButtonList;
        CheckBoxList Preference = GridView1.FooterRow.FindControl("CheckBoxList1") as CheckBoxList;

        SqlConnection con = new SqlConnection("Server=****;uid=sa;password=*****; database=sreeramdasari;");
        //con.Open();
        SqlCommand cmd = new SqlCommand("Update EmployeeDetails set EmpName=@EmpName, Designation=@Designation,Department=@Department,Salary=@Salary,Gender=@Gender,Preference=@Preference where EmpNo=@EmpNo");

        cmd.Parameters.AddWithValue("@EmpNo", EmpNo);
        cmd.Parameters.AddWithValue("@EmpName", EmpName.Text.Trim());
        cmd.Parameters.AddWithValue("@Designation", Designation.Text.Trim());
        cmd.Parameters.AddWithValue("@Department", Department.Text.Trim());
        cmd.Parameters.AddWithValue("@Salary", Salary.Text.Trim());
        cmd.Parameters.AddWithValue("@Gender", Gender.Text.Trim());
        cmd.Parameters.AddWithValue("@Preference", Preference.Text.Trim());
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();




        lbl1.Text = (" Record Updated Successfully");
        //Label6.Enabled = true;
        GridView1.EditIndex = -1;
   
   

        con.Close();
        this.BindGridView();
   

    }
Tags: ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900