使用递增的列值自动填充文本框 [英] Auto populating textbox with incremented column value

查看:74
本文介绍了使用递增的列值自动填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ID = ddlDept的下拉列表,它显示了名为DId的列中的部门和名为Department的表。在所选择的部门下,有一个学生,他们来自名为StudentID的列和名为registration的表,我已通过内部联接操作完成。现在我想通过从ddlDept中选择,使用递增的StudentID自动填充aspx webform中的文本框。



选择ddlDept之前假设我有02的ddlDept和最后的studentID在那个02部门是0705044.现在,如果我从ddlDept中选择02,则StudentID的文本框必须填充值0705045



如果学生ID为0405089然后通过选择ddlDept它必须填充0405089.



这意味着增量仅为最后2位数。



我很困难。请提示示例代码。提前完成

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

推荐答案

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




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


这篇关于使用递增的列值自动填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆