Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i want to create the auto generated text_id. it should be a sequence number and should be unique. i need the code for that. pls help.
for example if the table is empty means the text_id should be 0001. if the entities are there means should add 1 to the last value.
Ms Sql database
con.Open();
SqlCommand cmd = new SqlCommand("count(text_id), sum(text_id) from text_data", con);
SqlDataReader r1 = default(SqlDataReader);
DataSet ds =new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
// float a = 1;
float increment_id;
r1 = cmd.ExecuteReader();
r1.Read();

//da = r1.Read();

if(r1[0]==0)
{

increment_id = 1;
}
else
{

//r1.MoveLast;
// r1.NextResult();

increment_id = int.parse(dr[0].toString()) + 1;
}
this s my code what is the change i want to do.
Posted
Updated 8-Mar-10 1:51am
v3

is database coming into action do reply
 
Share this answer
 
use following instructions

count(text_id), sum(text_id) from tablename

execute this query you will get 1 row with values

now check if ...
if dr[0]==o then 
increment_id=1
else
increment_id= int.parse(dr[1].toString())+1;


and then comes your insert code

insert into tablename values(increment_id,.....)

Happy coding
 
Share this answer
 
your code is correct just make some change

remove

C#
r1.Read();
//da = r1.Read();
if(r1[0]==0)
{
increment_id = 1;
}
else
{
//r1.MoveLast;
// r1.NextResult();
increment_id = int.parse(dr[0].toString()) + 1;
}


and add

if r1.hasrows();
while (r1.read())
{
    if (r1[0].tostring()=="0")
    {
      increment_id=1
    }
    else
    {
        int.parse(r1[1].toString()) + 1;
    }
}


Now, there will not be error, but still any error occurs then give deals about error....
 
Share this answer
 

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