Click here to Skip to main content
15,885,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Idea is to : Edit,Update, add new row in gridview.

I have written the command for edit button but went wrong in update query.
And also how to add new next line in gridview.

below I have mentioned what I have tried so far. I'm getting execution error in
protected void GridView2_RowUpdating

What I have tried:

Program so far:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            addins();
        }
    }

    protected void addins()
    {
        SqlConnection cn = new SqlConnection("Server=WINDOWS-PC\\SQLEXPRESS;database=payroll;Trusted_Connection=Yes;");
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from amount";
        cmd.Connection = cn;
        cn.Open();


        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        int count = ds.Tables[0].Rows.Count;
        cn.Close();

        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView2.DataSource = ds;
            GridView2.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView2.DataSource = ds;
            GridView2.DataBind();
            int cc = GridView2.Rows[0].Cells.Count;

            Label1.Text = "No Record";
        }
    }

    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView2.EditIndex = e.NewEditIndex;
        addins();
    }

    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int sno = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
       // TextBox sno = (TextBox)GridView2.Rows[e.RowIndex].FindControl("sno");
        TextBox particulars = (TextBox)GridView2.Rows[e.RowIndex].FindControl("part");
        TextBox quantity = (TextBox)GridView2.Rows[e.RowIndex].FindControl("qty");
        TextBox rate = (TextBox)GridView2.Rows[e.RowIndex].FindControl("rate");
        TextBox amount = (TextBox)GridView2.Rows[e.RowIndex].FindControl("amt");
        GridView2.EditIndex = -1;

        SqlConnection cn = new SqlConnection("Server=WINDOWS-PC\\SQLEXPRESS;database=payroll;Trusted_Connection=Yes;");
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "update amount set part='" + particulars.Text + "', qty = '" + quantity.Text + "', rate = '" + rate.Text + "', amt = '" + amount.Text + "' where  sno ='" + sno + "',cn)";
        cmd.Connection = cn;
        cn.Open();
        
        cmd.ExecuteNonQuery();
        cn.Close();
        addins();
    }

    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;
            addins();
    }
Posted
Updated 11-Mar-16 23:34pm
v3
Comments
[no name] 12-Mar-16 4:31am    
At which line you are getting error and what is error details?
Sermaraj 14-Mar-16 0:25am    
error message:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index//

==
in program:
error in this line:
int sno = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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