Click here to Skip to main content
15,915,813 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ASPX.CS code
C#
int[] no = new int[100];
           int i = 0;
           DataTable dt = bl.BL_AutogerateKey(txt_req_form_id.Text);
           if (dt.Rows.Count == 0)
           {
               txt_req_form_id.Text = "1000001";
           }
           else
           {
               foreach (DataRow dr in dt.Rows)
               {                
                   no[i] = Convert.ToInt32(dr["FORM_ID"].ToString());
                   i++;
               }
              int auto = no.Max();
              txt_req_form_id.Text = "100000" + Convert.ToString(auto + 1);

DataLayer

C#
String str = "select substring(FORM_ID,5,50) as FORM_ID from PREV_REQUEST_TO_RECRUIT";
        SqlDataAdapter da = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        return dt;


I am doing auto generation of form_id. but the value "100000" is storing as string in DB whereas I want this to be stored as int. what alteration should I do in C# code to change it as integer?
Posted

You should change the database storage type - it's out of your applications hands when it's sent to SQL's processing - and the field type would seem to be a char type instead of an int in the database (I am relying upon your description of the problem).

SQL will then return the object as an int.




ASIDE: SQL has the ability to promote int's sent in as strings if required for storage - if they're convertible.


 
Share this answer
 
Comments
sathish kumar 11-Nov-13 9:26am    
I have changed the DB to numeric(10,0). but instead of 1000001,1000002 adding like this the code adds 10000011000002 . Please help me
W Balboos, GHB 11-Nov-13 9:42am    
From your input, above:
txt_req_form_id.Text = "100000" + Convert.ToString(auto + 1);

You're "adding" text to text. In C#, the '+' sign is overloaded to concatenate to strings.
Also, I note that you seem to be working only with integers: make your field of type INT not NUMERIC - good up to about 2*10^9


This being said, I think you need to step back, take a breath, and consider what you want to do and what you're actually doing.

Also - if you're using these INT values as UNIQUE keys in your table (a primary key) - why not just declare the field IDENTITY and let SQL number it for you?
(If you don't want to start with 1, you can initialize the starting value to whatever you wish).

But try to get straight in your code/plan how you wish to handle these values - as numbers or strings, and stick to it except where absolutely necessary to cast/convert.
Replace
txt_req_form_id.Text = "100000" + Convert.ToString(auto + 1);

with
txt_req_form_id.Text = Convert.ToString((100000 + auto + 1));
 
Share this answer
 
Change the column type in DB table column as int
 
Share this answer
 
v2

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