Hijochk12345,
Refer below sample code.
<div style="background-color: Yellow; height: 500pt">
<div align="center" style="margin-left: 30px; margin-top: 20px; width: 75%; height: 300pt;">
<table align="center" style="border-collapse: collapse; width: 500px;">
<tr>
<th align="center" colspan="4" style="color: Black; border: 1px solid #86ef12; padding: 5px 10px;">Add Employees Details</th>
</tr>
<tr>
<td style="border: 1px solid #86ef12; padding: 5px 10px;">Employ ID :</td>
<td style="border: 1px solid #86ef12;">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="border: 1px solid #86ef12; padding: 5px 10px;">Employ Name :</td>
<td style="border: 1px solid #86ef12;">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="border: 1px solid #86ef12; padding: 5px 10px;">Employee Place :</td>
<td style="border: 1px solid #86ef12;">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="border: 1px solid #86ef12; padding: 5px 10px;">Employ Age :</td>
<td style="border: 1px solid #86ef12;">
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="border: 1px solid #86ef12; padding: 5px 10px;">Employ Image :</td>
<td style="border: 1px solid #86ef12;">
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
</tr>
<tr>
<td colspan="4" style="padding: 5px 10px; text-align: center; border: 1px solid #86ef12;">
<asp:Button ID="btn_add" runat="server" Text=" ADD " OnClick="btn_add_Click" />
<asp:Button ID="Buttondb" runat="server" Text="Add to DB" OnClick="Buttondb_Click" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False" DataKeyNames="id"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="CheckHead" runat="server" OnCheckedChanged="CheckHead_CheckedChanged" AutoPostBack="true" />
<asp:Button ID="Button2" runat="server" Text="delete" CommandName="delete" OnClick="Button2_Click" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBody" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Edit1" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Delete1" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Update1" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="Cancel1" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="ID" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:BoundField DataField="place" HeaderText="place" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:TemplateField HeaderText="image">
<EditItemTemplate>
<asp:FileUpload ID="FileuploadImage" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="62px" ImageUrl='<%# Eval("Image") %>' Width="89px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
maketable();
bind();
}
}
protected void bind()
{
GridView1.DataSource = (DataTable)ViewState["dt"];
GridView1.DataBind();
}
protected void maketable()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5]{
new DataColumn("Id",typeof(string)),
new DataColumn("name",typeof(string)),
new DataColumn("place",typeof(string)),
new DataColumn("Age",typeof(int)),
new DataColumn("Image",typeof(string)) });
ViewState["dt"] = dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void Buttondb_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
string Name = gvrow.Cells[3].Text;
string Place = gvrow.Cells[4].Text;
int Age = Convert.ToInt32(gvrow.Cells[5].Text);
string Image = (gvrow.FindControl("Image1") as Image).ImageUrl;
insertData(Name, Place, Age, Image);
}
}
public void insertData(string Name, string Place, int Age, string Image)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con11"].ConnectionString);
string sql = "insert into Registration(name,age,place,image) values(@name,@age,@place,@image)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@name", Name);
cmd.Parameters.AddWithValue("@place", Place);
cmd.Parameters.AddWithValue("@age", Age);
cmd.Parameters.AddWithValue("@image", Image);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
protected void btn_add_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
if (FileUpload1.HasFile)
{
string path = "~/pics/" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath(path));
dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox5.Text, path);
}
else
{
dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox5.Text, "");
}
ViewState["dt"] = dt;
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvrow = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox t1 = (TextBox)gvrow.Cells[2].Controls[0];
TextBox t2 = (TextBox)gvrow.Cells[3].Controls[0];
TextBox t3 = (TextBox)gvrow.Cells[4].Controls[0];
TextBox t4 = (TextBox)gvrow.Cells[5].Controls[0];
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows[gvrow.RowIndex]["id"] = Convert.ToInt32(t1.Text);
dt.Rows[gvrow.RowIndex]["name"] = t2.Text;
dt.Rows[gvrow.RowIndex]["place"] = t3.Text;
dt.Rows[gvrow.RowIndex]["Age"] = Convert.ToInt32(t4.Text);
FileUpload f1 = (FileUpload)gvrow.FindControl("FileuploadImage");
if (f1.HasFile)
{
f1.SaveAs(Server.MapPath("~/pics/") + f1.FileName);
dt.Rows[gvrow.RowIndex]["Image"] = "~/pics/" + f1.FileName;
}
ViewState["dt"] = dt;
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows[index].Delete();
ViewState["dt"] = dt;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void CheckHead_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkheader = (CheckBox)GridView1.HeaderRow.FindControl("CheckHead");
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox chkrows = (CheckBox)gvrow.FindControl("CheckBody");
if (chkheader.Checked == true)
{
chkrows.Checked = true;
}
else
{
chkrows.Checked = false;
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
for (int i = GridView1.Rows.Count - 1; i >= 0; i--)
{
GridViewRow gvrow = GridView1.Rows[i];
CheckBox chkbody = (CheckBox)gvrow.FindControl("CheckBody");
if (chkbody.Checked)
{
dt.Rows[i].Delete();
}
}
ViewState["dt"] = dt;
bind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
maketable()
bind()
End If
End Sub
Protected Sub bind()
GridView1.DataSource = CType(ViewState("dt"), DataTable)
GridView1.DataBind()
End Sub
Protected Sub maketable()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(4) {
New DataColumn("Id", GetType(String)),
New DataColumn("name", GetType(String)),
New DataColumn("place", GetType(String)),
New DataColumn("Age", GetType(Integer)),
New DataColumn("Image", GetType(String))})
ViewState("dt") = dt
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub Buttondb_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each gvrow As GridViewRow In GridView1.Rows
Dim Name As String = gvrow.Cells(3).Text
Dim Place As String = gvrow.Cells(4).Text
Dim Age As Integer = Convert.ToInt32(gvrow.Cells(5).Text)
Dim Image As String = (TryCast(gvrow.FindControl("Image1"), Image)).ImageUrl
insertData(Name, Place, Age, Image)
Next
End Sub
Public Sub insertData(ByVal Name As String, ByVal Place As String, ByVal Age As Integer, ByVal Image As String)
Dim con As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("con11").ConnectionString)
Dim sql As String = "insert into Registration(name,age,place,image) values(@name,@age,@place,@image)"
Dim cmd As SqlCommand = New SqlCommand(sql, con)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@name", Name)
cmd.Parameters.AddWithValue("@place", Place)
cmd.Parameters.AddWithValue("@age", Age)
cmd.Parameters.AddWithValue("@image", Image)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Protected Sub btn_add_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = CType(ViewState("dt"), DataTable)
If FileUpload1.HasFile Then
Dim path As String = "~/pics/" & System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(Server.MapPath(path))
dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox5.Text, path)
Else
dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox5.Text, "")
End If
ViewState("dt") = dt
bind()
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
bind()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim gvrow As GridViewRow = CType(GridView1.Rows(e.RowIndex), GridViewRow)
Dim t1 As TextBox = CType(gvrow.Cells(2).Controls(0), TextBox)
Dim t2 As TextBox = CType(gvrow.Cells(3).Controls(0), TextBox)
Dim t3 As TextBox = CType(gvrow.Cells(4).Controls(0), TextBox)
Dim t4 As TextBox = CType(gvrow.Cells(5).Controls(0), TextBox)
Dim dt As DataTable = CType(ViewState("dt"), DataTable)
dt.Rows(gvrow.RowIndex)("id") = Convert.ToInt32(t1.Text)
dt.Rows(gvrow.RowIndex)("name") = t2.Text
dt.Rows(gvrow.RowIndex)("place") = t3.Text
dt.Rows(gvrow.RowIndex)("Age") = Convert.ToInt32(t4.Text)
Dim f1 As FileUpload = CType(gvrow.FindControl("FileuploadImage"), FileUpload)
If f1.HasFile Then
f1.SaveAs(Server.MapPath("~/pics/") + f1.FileName)
dt.Rows(gvrow.RowIndex)("Image") = "~/pics/" & f1.FileName
End If
ViewState("dt") = dt
GridView1.EditIndex = -1
bind()
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim index As Integer = Convert.ToInt32(e.RowIndex)
Dim dt As DataTable = CType(ViewState("dt"), DataTable)
dt.Rows(index).Delete()
ViewState("dt") = dt
bind()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = -1
bind()
End Sub
Protected Sub CheckHead_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim chkheader As CheckBox = CType(GridView1.HeaderRow.FindControl("CheckHead"), CheckBox)
For Each gvrow As GridViewRow In GridView1.Rows
Dim chkrows As CheckBox = CType(gvrow.FindControl("CheckBody"), CheckBox)
If chkheader.Checked = True Then
chkrows.Checked = True
Else
chkrows.Checked = False
End If
Next
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = CType(ViewState("dt"), DataTable)
For i As Integer = GridView1.Rows.Count - 1 To 0
Dim gvrow As GridViewRow = GridView1.Rows(i)
Dim chkbody As CheckBox = CType(gvrow.FindControl("CheckBody"), CheckBox)
If chkbody.Checked Then
dt.Rows(i).Delete()
End If
Next
ViewState("dt") = dt
bind()
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
End Sub