Monday 24 June 2013

  • How to increment textbox value comes from database in asp.net ?

    Hello everyone,here i am going to publish very interesting topic which used by each and every website almost.I have already published code about how to search data from database ? Now,the problem is about how to deal with database values fetched in text box ?

    Sometimes,we want to do some user friendly actions in our website and text box value increment is also like that.We have seen many times in websites which provide registration page and ask to enter information,when we submit that form we get one user id and it's unique for everyone so unique id should be increment by itself.


    So,inbuilt Sql server provides auto increment facility,but problem with it is whenever you delete any one record from database managing auto increment becomes difficult because it is unchanged,and sometimes we need to maintain sequence of records in database so manual coding is needed.

    However,here is program to increment text box value comes from database automatically.

    So,first of all we will create one database table called student.




    Then we will create one function for increment value of text box when data will be stored.Here i have created txtincr() function.

    Then we will call txtincr() function when page load event will occur.Then we need to call again that function when all information saved in database and page being refreshed with cleartext()function.

     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;  //don't forget to add
     using System.Data;  // don't forget to add

     public partial class Default2 : System.Web.UI.Page
     {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;
    Integrated Security=True;User Instance=True");

     protected void Page_Load(object sender, EventArgs e)
     {
            txtincr(); 
     }
        

     protected void Button1_Click(object sender, EventArgs e)
     {
            if (con.State == 0)
            {
                con.Open();
            }
            
    string sqlstr="insert into student values("+Convert.ToInt32(txtrollid.Text)+",
    '"+txtname.Text+"',"+Convert.ToInt32(txtage.Text)+",'"+txtmailid.Text+"')";

            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.Connection = con;
            sqlcmd.CommandText = sqlstr;
            sqlcmd.ExecuteNonQuery();
            Label1.Visible = true;
            Label1.Text = "Record stored";
            cleartext();
            con.Close();
     }
        

     private void txtincr()
     {
            int a;

            if (txtrollid.Text == "")
            {
                
                if (con.State == 0)
                {
                    con.Open();
                }
                string sqlstr = "select * from student";
                SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
                DataSet dst = new DataSet();
                sda.Fill(dst);
                

                if (dst.Tables[0].Rows.Count != 0)
                {
                    a = dst.Tables[0].Rows.Count;
                    a = a + 1;
                    txtrollid.Text = Convert.ToString(a);
                    txtname.Focus();
                }
                else
                {
                    txtrollid.Text = "1";
                    txtname.Focus();
                }
                con.Close();
            }

     }



     private void cleartext()
        {
            txtrollid.Text = "";
            txtname.Text = "";
            txtage.Text = "";
            txtmailid.Text = "";
            txtincr();
        }

    }

          

    That's it.Whenever you will enter any information and click on save button,data will be saved and unique roll id of student will be automatically increment.


    The output of above program is like as below.



    If you want to download demo code of this program then click on the below download link.


  • 0 comments:

    Post a Comment

    Copyright @ 2013 Programming Languages.