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();
            }  
         }

Loading

Categories: ASP .Net

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.