Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
Herewith I am developing C# application with SQL. My question is how to get maximum serial number from sql Database and store it in a variable in C#

Thanks
Indrajit Dasgupta
Posted
Comments
Maciej Los 26-Feb-14 1:42am    
Serial number is numeric value? What format?
Bernhard Hiller 26-Feb-14 3:21am    
Being a member of CP since more than three years, you ought to know some basics of asking questions and adding comments: What did you try? Where are you stuck? And provide error messages instead of saying "showing error". That is the reason for my vote of 1.

1 solution

C#
SqlConnection xconn=new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ToString());

SqlCommand xcmd=new SqlCommand("select max(serialNum) from dbo.YourTable",xconn);
int num=0;
try
{
xconn.Open();
num=xcmd.ExecuteScalar();
Console.WriteLine("Max serial number is {0}",num);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
xconn.Close();
}


The above code should get you started.

Regards
Anurag
 
Share this answer
 
Comments
IndrajitDasgupat 26-Feb-14 2:43am    
I am trying that but showing error
Anurag Sinha V 26-Feb-14 2:55am    
what error?
and at what line of code?
edit the question and post your code

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