Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

namespace EmpGrid
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        static DataTable ds = new DataTable();
        private SqlConnection conn = new SqlConnection(@"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=task;User ID=sa;Password=ups");  

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (!ds.Columns.Contains("id"))
                    
                          
                   

                ds.Columns.Add("id");
                if (!ds.Columns.Contains("name"))
                ds.Columns.Add("name");
                if (!ds.Columns.Contains("salary"))
                ds.Columns.Add("salary");

             
                // GridView1.DataSource = dt;
                // GridView1.DataBind();

                gvbind();

            }
            }
            protected void gvbind() {  
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * from empdet", conn);  
        SqlDataAdapter da = new SqlDataAdapter(cmd);  
        DataSet ds = new DataSet();  
        da.Fill(ds);  
        conn.Close();  
        if (ds.Tables[0].Rows.Count > 0) {  
            GridView1.DataSource = ds;  
            GridView1.DataBind();  
        } else {  
            //ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = new String[] { };
            GridView1.DataBind();  
            
          
        }  
        }

            protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
                Label lbldeleteid = (Label)row.FindControl("lblID");
                conn.Open();
                SqlCommand cmd = new SqlCommand("delete FROM empdet where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                gvbind();
            }

            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {

                GridView1.EditIndex = e.NewEditIndex;
                gvbind();
            }
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

                GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
                Label lblID = (Label)row.FindControl("lblID");

                TextBox textName = (TextBox)row.Cells[0].Controls[0];
                TextBox textadd = (TextBox)row.Cells[1].Controls[0];
               // TextBox textc = (TextBox)row.Cells[2].Controls[0];



                GridView1.EditIndex = -1;
                GridView1.DataBind();
                conn.Open();

                SqlCommand cmd = new SqlCommand("update empdet set name='" + textName.Text + "',salary='" + textadd.Text + "'where id='" + id + "'", conn);


                cmd.ExecuteNonQuery();

                conn.Close();
                gvbind();
            }

            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GridView1.PageIndex = e.NewPageIndex;
                gvbind();
            }
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView1.EditIndex = -1;
                gvbind();



            }

        protected void Add_Click(object sender, EventArgs e)
        {
            GridView1.Visible = true;
            //createnewrow();
            ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);

            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";

            GridView1.DataSource = ds;
            GridView1.DataBind();
            GridView1.DataSource = null;

        }

        protected void Save_Click(object sender, EventArgs e)
        {
            //   foreach (DataGridViewRow row in GridView1.Rows)
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string constring = @"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=task;User ID=sa;Password=ups";
                using (SqlConnection con = new SqlConnection(constring))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO empdet VALUES( @id,NEWID(),@name, @salary,getdate())", con))
                    {
                        cmd.Parameters.AddWithValue("@id", GridView1.Rows[i].Cells[0].Text);
                        cmd.Parameters.AddWithValue("@name", GridView1.Rows[i].Cells[1].Text);
                        cmd.Parameters.AddWithValue("@salary", GridView1.Rows[i].Cells[2].Text);
                        //cmd.Parameters.AddWithValue("@Name", row.Cells["Name"].Value);
                        //cmd.Parameters.AddWithValue("@Address", row.Cells["Address"].Value);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }


            }
        }
    }
}


What I have tried:

i try to edit the row and update,delete. but am getting error while editing and deleting errors encountered. please help me to solve the issue
Posted
Updated 30-Jan-18 21:26pm
Comments
Laxmidhar tatwa technologies 31-Jan-18 5:29am    
ds is taken both global as well as local vsriable .global change to dt that is for data table
Member 13650918 31-Jan-18 5:44am    
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;

namespace EmpGrid
{
public partial class WebForm1 : System.Web.UI.Page
{
DataTable dt = new DataTable();
private SqlConnection conn = new SqlConnection(@"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=task;User ID=sa;Password=ups");

protected void Page_Load(object sender, EventArgs e)
{
dt.Columns.Add("id");
// if (!ds.Columns.Contains("name"))
dt.Columns.Add("name");
// if (!ds.Columns.Contains("salary"))
dt.Columns.Add("salary");

if (!this.IsPostBack)
{
// if (!ds.Columns.Contains("id"))


//dt.Columns.Add("id");
//// if (!ds.Columns.Contains("name"))
//dt.Columns.Add("name");
//// if (!ds.Columns.Contains("salary"))
//dt.Columns.Add("salary");




GridView1.DataSource = dt;
GridView1.DataBind();

// gvbind();

}


}
protected void gvbind() {
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from empdet", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0) {
GridView1.DataSource = ds;
GridView1.DataBind();
} else {
//ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = new String[] { };
GridView1.DataBind();


}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
conn.Open();
SqlCommand cmd = new SqlCommand("delete FROM empdet where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");

TextBox textName = (TextBox)row.Cells[1].Controls[0];
TextBox textadd = (TextBox)row.Cells[2].Controls[0];
// TextBox textc = (TextBox)row.Cells[2].Controls[0];



GridView1.EditIndex = -1;
GridView1.DataBind();
conn.Open();

SqlCommand cmd = new SqlCommand("update empdet set name='" + textName.Text + "',salary='" + textadd.Text + "'where id='" + id + "'", conn);


cmd.ExecuteNonQuery();

conn.Close();
gvbind();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gvbind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
gvbind();

Member 13650918 31-Jan-18 5:45am    
now a value is added. but only single row is added. i just want to add more than one record and store it in a Database
Laxmidhar tatwa technologies 31-Jan-18 5:59am    
In row_deleting function change
Convert.toint32(lbldeleteid.text)
Laxmidhar tatwa technologies 31-Jan-18 6:03am    
Take outside the grid three textboxes
With a button and in button clic event
Add a row to the dt the global and bind the grid

1 solution

C#
   TextBox textName = (TextBox)row.Cells[0].Controls[0];
   TextBox textadd = (TextBox)row.Cells[1].Controls[0];//change index

 //after code updated
TextBox textName = (TextBox)row.Cells[1].Controls[0];
   TextBox textadd = (TextBox)row.Cells[2].Controls[0];
 
Share this answer
 

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