C# .NET - id - Asked By vivek shah on 11-Feb-11 02:01 AM

hello guys

i want to retrieve  data from tabel by validating ID p_k
but It Is giving me an error that data type mis matched in criteria expression

what should i do ???? please give me suggestion
 
try
        {
          OleDbConnection oc = new OleDbConnection(conString);
          oc.Open();
          OleDbCommand db = new OleDbCommand("Select Purchased_Amount from PAYMENT where ID = '" + label4.Text + "'", oc);
          label56.Text = Convert.ToString(db.ExecuteScalar());
          oc.Close();
        }
        catch (OleDbException ex)
        {
          MessageBox.Show(ex.Message);
        }

here is the code guys which i am using
Reena Jain replied to vivek shah on 11-Feb-11 02:17 AM
hi,

This because your id is of integer type and you are putting it in where clause as string. here is the correct code for you

try
{
if(label4.Tex!="")
{    
OleDbConnection oc = new OleDbConnection(conString);
oc.Open();
OleDbCommand db = new OleDbCommand("Select Purchased_Amount from PAYMENT where ID = " + Convert.ToInt32(label4.Text) + "", oc);
label56.Text = Convert.ToString(db.ExecuteScalar());
 oc.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
}


hope this will help you
Mash B replied to vivek shah on 11-Feb-11 04:28 AM
I would suggest to pass value as parameter as shown below
 
try
    {
      OleDbConnection oc = new OleDbConnection(conString);
      oc.Open();
      OleDbCommand db = new OleDbCommand("Select Purchased_Amount from PAYMENT where ID = @id", oc);
      db.Parameters.AddWithValue("id",Convert.ToInt64(label4.Text)); // adding as parameter
      label56.Text = Convert.ToString(db.ExecuteScalar());
      oc.Close();
    }
    catch (OleDbException ex)
    {
      MessageBox.Show(ex.Message);
    }