hey everyone i am using a select query and showing input string is not in correct format ...plz help me......
here is my query.....

int id = Convert.ToInt32(String.Format(@"SELECT Id FROM Student WHERE RegNo ='{0}' ", cmbRegNo.Text));

Recommended Answers

All 7 Replies

Sure!!
What you trying to do is to convert "SELECT Id FROM Student WHERE RegNo ='value' to Integral value!!
Just tel me what you need to do.
I expect you need to execute this query and take the result in the ID
So

string selectStatement = String.Format("SELECT Id FROM Student WHERE RegNo ='{0}' ", cmbRegNo.Text);
SqlCommand cmd = new SqlCommand(selectStatement, connection);
int id = (int)cmd.ExecuteScalar();

Sure!!
What you trying to do is to convert "SELECT Id FROM Student WHERE RegNo ='value' to Integral value!!
Just tel me what you need to do.
I expect you need to execute this query and take the result in the ID
So

string selectStatement = String.Format("SELECT Id FROM Student WHERE RegNo ='{0}' ", cmbRegNo.Text);
SqlCommand cmd = new SqlCommand(selectStatement, connection);
int id = (int)cmd.ExecuteScalar();

thanx 4 ur reply
i hv done dis nd its shoing the following error

String connectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

string Query = String.Format("SELECT RegNo FROM Student WHERE RegNo ='{0}' ", cmbRegNo.Text);

OleDbCommand cmd=new OleDbCommand(Query,connectionString);
int id =(int)cmd.ExecuteScalar();

Error 6 The best overloaded method match for 'System.Data.OleDb.OleDbCommand.OleDbCommand(string, System.Data.OleDb.OleDbConnection)' has some invalid arguments C:\Users\Manak\Desktop\smsfinal\StudentManagementSystem\StudentStatus.cs 166 34 StudentManagementSystem

Please use Code Tag :)

String connectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

string Query = String.Format("SELECT RegNo FROM Student WHERE RegNo ='{0}' ", cmbRegNo.Text);

OleDbCommand cmd=new OleDbCommand(Query,new OleDbConnection (connectionString));

int id =(int)cmd.ExecuteScalar();

Please use Code Tag :)

String connectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

string Query = String.Format("SELECT RegNo FROM Student WHERE RegNo ='{0}' ", cmbRegNo.Text);

OleDbCommand cmd=new OleDbCommand(Query,new OleDbConnection (connectionString));

int id =(int)cmd.ExecuteScalar();

now its showing to execute executescalar you need to open the connection and its closed

Little correction to Ramy's code.

String connectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

string Query = String.Format("SELECT RegNo FROM Student WHERE RegNo ='{0}' ", cmbRegNo.Text);

OleDbCommand cmd=new OleDbCommand(Query,new OleDbConnection (connectionString));
cmd.Connection.Open();
int id =(int)cmd.ExecuteScalar();
cm.Connection.Close();

Very good adatapost, I forgot to open connection!

u all rockkkkkkk
nd this site also rockss
thanku for helping me..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.