{
SqlConnection conn = new SqlConnection();conn.ConnectionString =
"Data Source=(local);" + "Initial Catalog=bd;" + "Integrated Security=SSPI;"; SqlCommand sqlComm = new SqlCommand("SELECT * FROM utilizador WHERE user=@user;", conn);sqlComm.Parameters.Add(
"@user", SqlDbType.VarChar);sqlComm.Parameters[
"@user"].Value = user.Text;conn.Open();
Int32 teste = 0;teste = ((
Int32) sqlComm.ExecuteScalar()); --> ERROR {"Object reference not set to an instance of an object."}conn.Close();
}
whats wrong

ExecuteScalar erro
Babalicious
Hi Watt
I dont know whats a problem here with your code
, here I've attached mine and it works fine 
int i = 0;
int temp_Code = 0;
ArrayList Positions = new ArrayList();
sqlConnection5.Open();
SqlCommand cmd_form = new SqlCommand("SELECT Position FROM Data_Channels WHERE [Customer Code]=@Code", sqlConnection5);
cmd_form.Parameters.Add("@Code", SqlDbType.NVarChar, 6);
cmd_form.Parameters["@Code"].Value = Code;
temp_Code = (int)cmd_form.ExecuteScalar();
SqlDataReader form_read = cmd_form.ExecuteReader();
while (form_read.Read())
{
Positions.Add(Convert.ToInt32(form_read.GetValue(i)));
i = i + 1 - 1;
}
form_read.Close();
sqlConnection5.Close();
Hope this helps
Curious George
Hi
I think you forgot to put the number if characters from your command.
sqlComm.Parameters.Add("@user", SqlDbType.VarChar, 10 );
Hope this helps.
FDS
Hi
1. Your SQL statement will return will return the row.
2. At the end of your sql statement you have put a semicolon see,
SqlCommand cmd_form = new SqlCommand("SELECT * FROM utilizador WHERE user=@user;", conn);
3. cmd_form.Parameters["@user"].Value = user.Text; // user.Text is not an integer it is a string, that means string temp_Code = (string)cmd_form.ExecuteScalar();
Hope this is goung to help you to trace your code
codetale
reagards
niallhannon
If you use ExecuteScalar then check if returned object is not null.
If you use ExecuteReader then check if datareader HasRows or check if you can Read() from it.
dclements
temp_Code = (int)cmd_form.ExecuteScalar(); --> {"Object reference not set to an instance of an object."}
i have this error in this line...
My code:
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=(local);" +
"Initial Catalog=bd;" +
"Integrated Security=SSPI;";
int i = 0;
int temp_Code = 0;
conn.Open();
SqlCommand cmd_form = new SqlCommand("SELECT * FROM utilizador WHERE user=@user;", conn);
cmd_form.Parameters.Add("@user", SqlDbType.NVarChar, 6);
cmd_form.Parameters["@user"].Value = user.Text;
temp_Code = (int)cmd_form.ExecuteScalar();
SqlDataReader form_read = cmd_form.ExecuteReader();
Int32 teste = 0;
teste = (Convert.ToInt32(form_read.GetValue(i)));
conn.Close();
}
PerPixel
You don' tneed the ; and the result will be the data type of the first column of the first dataset not the same as the parameter.
I can reprodcue your problem if I use ExecuteScalar on a query that returns nothing. Try your query in Query Analyser and make sure it returns something for the value you are entering in user.Text.
If you can't tell before time if it will return a row or not put the result of ExecuteScalar into an Object and check if that is != null. If it isn't equal to null then cast it to an int.
This of cause assumes that the first column in the dataset returned by your query is an int.
Dion.
simonyee
Are you sure that this is an integer Have checked if the object that is returned is not null and is not dbnull.value
Freddo
hi,
did u find the solution for the problem.
i was also facing the same problem.
mikalush
reagards
gdrivas232096