SQL Script for Student Table
This is a sample table for storing the student information
USE [DB Name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Stud_Name] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Stud_Age] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Stud_Addr] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Stud_Gender] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Stud_Qualification] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
.Aspx code Here just a DropDownList for firing a SelectedIndexChanged event as you said you want to create textboxes on button click am using this event...
<div>
Select Id: <asp:dropdownlist id="DropDownList1" runat="server" autopostback="True" appenddatabounditems="true" xmlns:asp="#unknown">
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:dropdownlist>
</div>
web.confog Code this is for connecting sql fill your information...
<connectionstrings>
<add name="ConString" connectionstring="Data Source=[SQL serverName];Initial Catalog=[DB name];Persist Security Info=True;User ID=sa;Password=[*****]" providername="System.Data.SqlClient" />
</connectionstrings>
--aspx.cs code
here on selected changed event name is displaying in a single char text box which is dynamically creating so please check it out
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;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
if (!IsPostBack)
{
ddl();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
cmd = new SqlCommand("Select Stud_Name from student where Id='" + DropDownList1.SelectedItem + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
string s = ds.Tables[0].Rows[0][0].ToString();
for (int i = 0; i < s.Length; i++)
{
string id = "txt" + i.ToString();
TextBox txt = new TextBox();
txt.ID = id;
txt.Text = s[i].ToString();
txt.Width = 20;
txt.MaxLength = 1;
txt.ReadOnly = true;
form1.Controls.Add(txt);
}
}
catch
{
}
}
private void ddl()
{
try
{
cmd = new SqlCommand("Select Id from student ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ds.Clear();
DropDownList1.Items.Add("Select...");
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "Id";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
}
catch
{
}
}
}
I hope it will help you :)
Thanks for your time...
-Mahesh