ExecuteScalar erro

{

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




Answer this question

ExecuteScalar erro

  • J.W.

    the problem continue...

    reagards


  • scottmac

    hi,

    did u find the solution for the problem.

    i was also facing the same problem.



  • vb6newbie

    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



  • JohnYG

    Hi

    I think you forgot to put the number if characters from your command.

    sqlComm.Parameters.Add("@user", SqlDbType.VarChar, 10 );

    Hope this helps.



  • thompsop

    i put the true values and false end i see "no result", never enter ther int i = Convert.ToInt32(o);
    reagards


  • tsensenbach

    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();

    }



  • Forch

    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.


  • virgy

    You don't need to execute twice the same command. What you want to read from that table One column, one row or what If you want to read on column then use only ExecuteScalar and put only that column (not *) in select statement. If you want to read one row or more rows then use only ExecuteReader.
    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.

  • scottcable

    That is not mandatory. The problem is, that your query will probably return no results. Maybe it is helpfull to execute the query in Query Analzyer, and see what it returns. I guess you can work around this problem by first checking if the object that is returned by executescalar is not null or does not contain DBNull.Value. If this is not the case, convert it to an integer. object o = theCommand.ExecuteScalar(); if( o != null && o != DBNull.Value ) { int i = Convert.ToInt32(o); } else { MessageBox.Show ("no result"); }

  • JeffGraves

    What does executescalar return It returns only one object (the first column of the first row).
    Are you sure that this is an integer Have checked if the object that is returned is not null and is not dbnull.value


  • Virtuss

    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



  • ExecuteScalar erro