Example : How to increment serial no. automatically fetching data from Sql Server database in Asp .net using C#.
Write code in 'Trial.aspx.cs' (C# Sharp File)
protected void Page_Load(object sender, EventArgs e)
{
string cs = "data source =.; database= SITEDB; integrated security=SSPI";
//string cs = @"Data Source =.; Initial Catalog=SITEDB;";
SqlConnection con = new SqlConnection(cs);
Response.Write("Data Base Connected...");
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select Max(Slno) from Traildb", con);
SqlDataReader sdr = cmd.ExecuteReader();
string data1 ="";
while (sdr.Read())
{
data1 = sdr.GetString(0);
//Response.Write(data1);
}
int x = Convert.ToInt32(data1)+1;
TxtSlno.Text = Convert.ToString(x);
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}
0 Comments