null error

if you look at the code below i get this error Object reference not set to an instance of an object.

i get the error when this statment runs

result = (int)cmd.ExecuteScalar();

however as you can see i did use the new operator

public void RunSQL(ref string sqlstring, ref string err)

{

string errlocal = "No DataBase Errors";

int result;

err = errlocal;

string Connectionstring;

try

{

Connectionstring = GetAccessstring();

using (OleDbConnection OLECONN = new OleDbConnection(Connectionstring))

{

OleDbCommand cmd = new OleDbCommand(sqlstring, OLECONN);

OLECONN.Open();

result = (int)cmd.ExecuteScalar();

OLECONN.Close();

}

}

catch (OleDbException ex)

{

err = ex.ToString();

Cisco

}



Answer this question

null error

  • mbelew

     Sibusiso wrote:
    Please show me the sql Code

    this is mine


    private void button1_Click(object sender, EventArgs e)

    {

       string Number_t = textBox1.Text;

      int result;

     

      sqlConnection1.Open(); //Open My Connection

      SqlCommand cmd_Num = new SqlCommand("Select number From Numbers where num=@Number_t", sqlConnection1);

      cmd_Num.Parameters.Add("@Number_t", SqlDbType.Int);

      cmd_Num.Parameters["@Number_t"].Value = Number_t;

      result = (int)cmd_Num.ExecuteScalar();

      sqlConnection1.Close();

    }


    Hope this Helps



  • EdgarGustavoPerezGonzalez

    Please show me the sql Code

  • aze

    Sibusiso wrote:

  • computed_mind

    Ensure your CommandText return single result.

    I suppose your query do not return result

    hope this helps



  • null error