Answered by:
ExecuteScalar()

Question
-
Hi,
I'm having trouble getting the return data from the ExecuteScalar() Command.
What conversion do i have to do to display it in say messagebox?
This is what i've got and it doesn't work!
Thanks,
Code Snippetprivate void button1_Click(object sender, EventArgs e)
{
string Connstr;
string sql = "SELECT TOP 1 User_ID FROM CS_Users";
String r = "a";
Connstr = @"Data Source=JON-DB04294BD9C\SQLEXPRESSMAIN;Initial Catalog=Vision_CS_DB;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(Connstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
r = (string)cmd.ExecuteScalar()MessageBox.show(r)
conn.Close();
}
}Sunday, August 10, 2008 11:39 AM
Answers
-
Cool,
Fixed it i just need to convert the value returned from the executescalar to a string
string r = Convert.Tostring(cmd.executescalar() );
messagebox.show(r)
Thanks for your help
Jon
Sunday, August 10, 2008 2:01 PM
All replies
-
What exactly does not work? Do you get an error? Do you have something in the table you wanna select form?Sunday, August 10, 2008 1:26 PM
-
I want to get the return value from the executeScalar command into a variable that i can use somewhere else.
i keep getting the error saying cannot convert into stringSunday, August 10, 2008 1:42 PM -
Jon1s wrote: I want to get the return value from the executeScalar command into a variable that i can use somewhere else.
i keep getting the error saying cannot convert into string- Proposed as answer by Tiago César Oliveira Friday, February 24, 2012 2:08 AM
Sunday, August 10, 2008 1:45 PM -
Cool,
Fixed it i just need to convert the value returned from the executescalar to a string
string r = Convert.Tostring(cmd.executescalar() );
messagebox.show(r)
Thanks for your help
Jon
Sunday, August 10, 2008 2:01 PM -
Jon1s wrote: Cool,
Fixed it i just need to convert the value returned from the executescalar to a string
string r = Convert.Tostring(cmd.executescalar() );
messagebox.show(r)
Thanks for your help
Jon
It was my pleasure. Thank you for marking your own post as answer.
Sunday, August 10, 2008 2:25 PM