Reputation: 11
I'm trying to retrieve a radioButton value from database from SQL Server Compact
But my code doesn't work and an error message shows (unable to cast object of type 'System.Data.SqlServerCe Command' to type 'System.IConvertible
) at line (genderval = Convert.ToBoolean(gendercmnd);
)
.. what is wrong?
thanks
if (con.State != ConnectionState.Open)
con.Open();
Boolean genderval = false;
SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_info WHERE Std_id='" + TextBox1.Text + "'", con);
genderval = Convert.ToBoolean(gendercmnd);
if (genderval == true)
Convert.ToBoolean(radioButton1.Checked);
else
Convert.ToBoolean(radioButton2.Checked);
con.Close();
Upvotes: 1
Views: 3029
Reputation: 11
the final solution..!
//........ retrieving radioButton value .......
if (conn.State != ConnectionState.Open)
conn.Open();
try {
Boolean genderval = SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_Info WHERE Std_id='" + TextBox1.Text + "'", con);
genderval = (Boolean)gendercmnd.ExecuteScalar();
if (genderval == true)
Convert.ToBoolean(radioButton1.Checked=true);
else
Convert.ToBoolean(radioButton2.Checked = true);
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
thank you all for your help
Upvotes: 0
Reputation:
Try executing the command
genderval = (Boolean)gendercmnd.ExecuteScalar();
Also note that your query should be parameterized. Here is a complete example (using SqlConnection
but if you replace the Sql
classes with your provider classes it should work for you)
bool genderval;
using (SqlConnection conn = new SqlConnection("youeConnStr"))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Gender FROM Std_info WHERE Id = @id", conn))
{
int id = Convert.ToInt32(TextBox1.Text);
cmd.Parameters.Add(new SqlParameter("@id", id));
genderval = (bool)cmd.ExecuteScalar();
}
}
Upvotes: 2
Reputation: 562
What you're trying to do is to get a result from the SQL server, and cast it as a boolean.
What you're actually doing is trying to convert your SQL command into a boolean.
What you need to do is actually open the connection to the SQL server and execute your query. But it's a bit more complex than that...
if (con.State != ConnectionState.Open)
con.Open();
Boolean genderval = false;
SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_info WHERE Std_id='" + TextBox1.Text + "'", con);
gendercmnd.Connection.Open(); // Open the connection using the gendercmd string.
sqlReader = gendercmnd.ExecuteReader(); // Execute the read, which returns an SqlCEDataReader object that we can use to access the data.
genderval = Convert.ToBoolean(gendercmndResult);
if (genderval == true)
Convert.ToBoolean(radioButton1.Checked);
else
Convert.ToBoolean(radioButton2.Checked);
con.Close();
Now we need to take the value from the reader, and cast that as a boolean.
if (con.State != ConnectionState.Open)
con.Open();
Boolean genderval = false;
String gendercmndResult = null;
SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_info WHERE Std_id='" + TextBox1.Text + "'", con);
gendercmnd.Connection.Open(); // Open the connection using the gendercmd string.
sqlReader = gendercmnd.ExecuteReader(); returns an SqlCEDataReader object that we can use to access the data.
while (sqlReader.Read(){ //While the reader is reading...
gendercmndResult = (sqlReader.GetString(0)); //Assign the returned value from the SQL server to a string called gendercmndResult
}
genderval = Convert.ToBoolean(gendercmndResult); //Now we can cast the string as a bool
if (genderval == true)
Convert.ToBoolean(radioButton1.Checked);
else
Convert.ToBoolean(radioButton2.Checked);
con.Close();
Upvotes: 0
Reputation: 864
Are you executing the command against the db and getting results? as that code doesn't look like it is doing it. I think you need to call ExecuteScalar() on the sqlcecommand.
Upvotes: 0
Reputation: 406
Looks as though you haven't actually queried the database yet, but are attempting to convert the actual command rather than the results gathered.
I'm not sure if the same works for SQL Server Compact but you need to either ExecuteNonScalar or Execute the command itself, retrieve the results and then run your comparison on that.
Upvotes: 0