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");
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 {
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];
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;
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)
{
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);
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