Hi Star I made all the things for you check and place comment if necessary.And Adapt to your solution little bit customiztion may required or no customiztion needed.
In Aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="AddNewStudentWebForm.aspx.cs" Inherits="WebApplicationUpdatePanel.AddNewStudentWebForm" %>
<asp:content id="Content1" contentplaceholderid="HeadContent" runat="server" >
</asp:content>
<asp:content id="Content2" contentplaceholderid="MainContent" runat="server" >
<asp:scriptmanager id="ScriptManager1" runat="server">
</asp:scriptmanager>
<asp:updatepanel id="UpdatePanel1" runat="server">
<contenttemplate>
<asp:gridview id="GridView1" runat="server">
</asp:gridview>
<hr />
Select a Department:
<asp:dropdownlist runat="server" id="ddlDept" autopostback="true" onselectedindexchanged="ddlDept_SelectedIndexChanged"></asp:dropdownlist>
<h3>Insert Panel</h3>
Selected Dept ID: <asp:label id="lblDeptID" font-bold="true" runat="server"></asp:label><br />
Auto Incremented StudentID:
<asp:textbox id="txtAutoId" readonly="true" runat="server"></asp:textbox><br />
Enter Name:
<asp:textbox id="txtName" runat="server"></asp:textbox><br />
</contenttemplate>
<triggers>
<asp:asyncpostbacktrigger controlid="btnAdd" eventname="Click" />
<asp:asyncpostbacktrigger controlid="ddlDept" eventname="SelectedIndexChanged" />
</triggers>
</asp:updatepanel>
<asp:button id="btnAdd" text="Register" onclick="btnAdd_Click" runat="server" />
</asp:content>
In Code Behind .cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplicationUpdatePanel
{
public partial class AddNewStudentWebForm : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(@"Data Source=ADDIE-ANIS\ANISSQLSERVER12;Initial Catalog=BooksDB;Persist Security Info=True;User ID=sa;Password=Sa123");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
bindDropDown();
}
}
void bindgrid()
{
string sqlSelectCmd = "SELECT [StudentID],Registration.[Name],Registration.[DId],Department.[Name]FROM Registration inner join Department on Registration.[DId]=Department.[DId] order by Registration.[DId]";
SqlCommand cmd = new SqlCommand(sqlSelectCmd, conn);
SqlDataAdapter dap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dap.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
void bindDropDown()
{
conn.Open();
string sqlSelectCmd="SELECT [DId],[Name] FROM [BooksDB].[dbo].[Department]";
SqlCommand cmd = new SqlCommand(sqlSelectCmd, conn);
SqlDataAdapter dap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dap.Fill(ds);
ddlDept.DataSource = ds;
ddlDept.DataTextField = "Name";
ddlDept.DataValueField = "DId";
ddlDept.DataBind();
conn.Close();
}
protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
{
lblDeptID.Text = ddlDept.SelectedItem.Value;
if (lblDeptID.Text != "")
{
txtAutoId.Text= NextStudentID(Convert.ToInt32(lblDeptID.Text));
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
}
string NextStudentID(int DeptID)
{
string sqlCmd = "SELECT MAX(cast([StudentID] as int)) from Registration where [DId]=" + DeptID;
conn.Open();
SqlCommand cmd = new SqlCommand(sqlCmd, conn);
object nextrollObj = cmd.ExecuteScalar();
int Incremented = 0;
Incremented = Convert.ToInt32(nextrollObj.ToString()) + 1; ;
string Nextroll = "0" + Incremented.ToString();
return Nextroll;
}
}
}
Full Solution is <a href="https://drive.google.com/file/d/0B2Hxs8nJ709CSkJhR2Nud3lPTjA/view?usp=sharing">Here</a> You Can download
Database Schema and sample data
CREATE TABLE [dbo].[Department](
[DId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nchar](10) NULL,
CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED
(
[DId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Registration](
[StudentID] [nvarchar](10) NOT NULL,
[Name] [nvarchar](50) NULL,
[DId] [int] NOT NULL,
CONSTRAINT [PK_Registration] PRIMARY KEY CLUSTERED
(
[StudentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Registration] WITH CHECK ADD CONSTRAINT [FK_Registration_Registration] FOREIGN KEY([StudentID])
REFERENCES [dbo].[Registration] ([StudentID])
GO
ALTER TABLE [dbo].[Registration] CHECK CONSTRAINT [FK_Registration_Registration]
GO
SET IDENTITY_INSERT [dbo].[Department] ON
INSERT [dbo].[Department] ([DId], [Name]) VALUES (1, N'ICE ')
INSERT [dbo].[Department] ([DId], [Name]) VALUES (2, N'CSE ')
INSERT [dbo].[Department] ([DId], [Name]) VALUES (3, N'AECE ')
SET IDENTITY_INSERT [dbo].[Department] OFF
INSERT [dbo].[Registration] ([StudentID], [Name], [DId]) VALUES (N'0405088', N'Esha', 3)
INSERT [dbo].[Registration] ([StudentID], [Name], [DId]) VALUES (N'0705043', N'Neha', 2)
INSERT [dbo].[Registration] ([StudentID], [Name], [DId]) VALUES (N'0705044', N'Tamim', 2)
INSERT [dbo].[Registration] ([StudentID], [Name], [DId]) VALUES (N'0718004', N'Anis', 1)
INSERT [dbo].[Registration] ([StudentID], [Name], [DId]) VALUES (N'0718005', N'Toma', 1)
You can download full soluion here
Download here
Then before test using .sql file create tables and insert some sample data.Change Connection string.happy coding