Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dropdownlist of ID=ddlDept which shows the department from a column named DId and table named Department. Under that selected department there are students with StudentID from the column named StudentID and table named registration which i have done by inner join operation. Now I want to auto populate a textbox in a aspx webform with the incremented StudentID by selecting from ddlDept.

Before selecting ddlDept Suppose i had ddlDept of 02 and the last studentID in that "02" department is 0705044. Now if I select "02" from ddlDept then the textbox for StudentID must populate with the value of 0705045

Also if the Student ID is 0405089 then by selecting the ddlDept it must populate with 0405089.

That means increment will be of the last 2 digits only.

I am very stuck.Suggest with sample code.Thnx in advance
Posted
Updated 11-Feb-15 2:50am
v2

1 solution

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

ASP.NET
<%@ 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


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.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

SQL
--Department Table
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]

--Registration Table
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

-- Data insert Before Testing

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
 
Share this answer
 
v3
Comments
star_tasneem 12-Feb-15 17:45pm    
u r a great guy vai..

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;


return Nextroll;
}
this function is showing error
Incremented = Convert.ToInt32(nextrollObj.ToString()) + 1; ;
string Nextroll = "0" + Incremented.ToString();
these line showing exception
plz reply
Anisuzzaman Sumon 13-Feb-15 3:32am    
Have you insert sample data before tesing!set a break point and tell me what the exception says

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