Click here to Skip to main content
15,116,654 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new in c sharp, working on windows application where i need to increase student id automatically in database. And it should come directly in combo box when i open the particular form to add data, related to student.


thanks in advance.
Posted

Your database management software should be able to set the ID column to AutoIncrement. You can then create a new data row and let the database provide the ID. After that, you re-read and change what is necessary but leave ID untouched at all times.
   
Comments
mkcm2011 3-Nov-11 4:35am
   
i am working on sql server 2005 and AutoIncrement is not available there.
C#
public string SlNo(string tblName, string colName, string srchCodn, Page form)
    {
        string functionReturnValue = null;
        try
        {
            string sqlQuery = "select max(" + colName + ") + 1 from " + tblName;
            if (srchCodn != "")
                sqlQuery += " " + srchCodn;
            adp = new SqlDataAdapter(sqlQuery, conn);
            tbl = new DataTable();
            adp.Fill(tbl);
            if (tbl.Rows.Count == 0 | tbl.Rows[0][0].ToString() == "")
                functionReturnValue = Convert.ToString(1);
            else
                functionReturnValue = tbl.Rows[0][0].ToString();
            tbl.Dispose();
            adp.Dispose();
        }
        catch (Exception ex)
        {
            MsgBox("Serial No Error: " + ex.Message, form);
            return "";
        }
        return functionReturnValue;
    }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900