Hey akhter,
Please refer below sample.
HTML
<div>
<asp:GridView runat="server" ID="gvSubDepartMent" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="SubDeptId" HeaderText="SubDeptId" />
<asp:BoundField DataField="SubDeptName" HeaderText="SubDeptName" />
<asp:BoundField DataField="DeptId" HeaderText="DeptId" />
</Columns>
</asp:GridView>
<br />
SubDepartName :<br />
<asp:TextBox runat="server" ID="txtsubdept" />
<asp:DropDownList runat="server" ID="ddlDepartMent">
</asp:DropDownList>
<br />
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
</div>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT DeptId,DeptName FROM TestDepartment", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
ddlDepartMent.DataSource = dt;
ddlDepartMent.DataTextField = "DeptName";
ddlDepartMent.DataValueField = "DeptId";
ddlDepartMent.DataBind();
ddlDepartMent.Items.Insert(0, new ListItem("Select Department", "0"));
}
}
}
}
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM TestSubDepartMent", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
gvSubDepartMent.DataSource = dt;
gvSubDepartMent.DataBind();
}
}
}
}
}
protected void Insert(object sender, EventArgs e)
{
string DeptName = txtsubdept.Text;
string DeptID = ddlDepartMent.SelectedValue;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO TestSubDepartMent VALUES(@SubDeptName,@DeptId)", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SubDeptName", DeptName);
cmd.Parameters.AddWithValue("@DeptId", DeptID);
cmd.ExecuteNonQuery();
con.Close();
this.BindGrid();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT DeptId,DeptName FROM TestDepartment", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
ddlDepartMent.DataSource = dt
ddlDepartMent.DataTextField = "DeptName"
ddlDepartMent.DataValueField = "DeptId"
ddlDepartMent.DataBind()
ddlDepartMent.Items.Insert(0, New ListItem("Select Department", "0"))
End Using
End Using
End Using
End Using
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM TestSubDepartMent", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
gvSubDepartMent.DataSource = dt
gvSubDepartMent.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim DeptName As String = txtsubdept.Text
Dim DeptID As String = ddlDepartMent.SelectedValue
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO TestSubDepartMent VALUES(@SubDeptName,@DeptId)", con)
con.Open()
cmd.Parameters.AddWithValue("@SubDeptName", DeptName)
cmd.Parameters.AddWithValue("@DeptId", DeptID)
cmd.ExecuteNonQuery()
con.Close()
Me.BindGrid()
End Using
End Using
End Sub
Screenshot