1

My code:

DataTable dt = new DataTable();
mySqlDataAdapter.Fill(dt);
int socot = dt.Rows.Count;
for (int i = 0; i < socot; i++)
{
    String dn = dt.Rows[i][1].ToString();
    String gduan = "SELECT tennha FROM duannha WHERE id=@id";
    MySqlCommand cmd = new MySqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = gduan;
    cmd.Parameters.AddWithValue("@id", dn);
    String gtennha = cmd.ExecuteScalar().ToString();
    dt.Rows[i][1] = gtennha;
}

When I run code, I received an error. Please help me fix it.

error

4
  • You want to put String into Int32 variable. Then convert it first using Int32.Parse. Commented Jul 23, 2016 at 14:56
  • If you look at the error message you will notice that the return value of the ExecuteScalar is not something that could be assigned to a column of type integer. So something is really broken here. Or you have choosen an invalid column from your datatable (the column at index 1) or you should get a different value from your query or you are trying to assign the return value of your query at the wrong column (still the column at index 1) Commented Jul 23, 2016 at 15:07
  • Dont forget the using block for cmd Commented Jul 23, 2016 at 22:55
  • This shouldn't be a separate query in the first place. This is what JOINs are for. Commented Jul 24, 2016 at 3:53

3 Answers 3

1

Expected Type as Int32

If gtennha contains a number, you can parse it to an int. With more safeguards:

var gtennha = cmd.ExecuteScalar();
try 
{
   if(gtennha != null)
      dt.Rows[i][1] = int.Parse(gtennha.ToString());
   else 
      Console.WriteLine("Error: Query didn't return a result.");
} 
catch(FormatException ex)
{
   Console.WriteLine("Error: Couldn't parse 'gtennha' to a number.");
   /* more error handling */
}
Sign up to request clarification or add additional context in comments.

1 Comment

try parse more safer than parse
1

Parse string to int

DataTable dt = new DataTable();
        mySqlDataAdapter.Fill(dt);
        int socot = dt.Rows.Count;
        for (int i = 0; i < socot; i++)
        {
            String dn = dt.Rows[i][1].ToString();
            String gduan = "SELECT tennha FROM duannha WHERE id=@id";
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = gduan;
            cmd.Parameters.AddWithValue("@id", dn);
            String gtennha = cmd.ExecuteScalar().ToString();
            int a;
            int.TryParse(gtennha, out a);
            dt.Rows[i][1] = a;
        }

Update

if gtennha always start with VIOFFICE or a word something like that then use this way split string

DataTable dt = new DataTable();
        mySqlDataAdapter.Fill(dt);
        int socot = dt.Rows.Count;
        for (int i = 0; i < socot; i++)
        {
            String dn = dt.Rows[i][1].ToString();
            String gduan = "SELECT tennha FROM duannha WHERE id=@id";
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = gduan;
            cmd.Parameters.AddWithValue("@id", dn);
            String gtennha = cmd.ExecuteScalar().ToString();
            int a;
            int.TryParse(gtennha.Split(' ')[1], out a);
            dt.Rows[i][1] = a;
        }

4 Comments

But "gtennha" had value "VIOFFICE 124".
@ngocuong always begin with VIOFFICE ?
Format of column duan. INT32 to String :)
This conversion String dn = dt.Rows[i][1].ToString() no need but String gtennha = cmd.ExecuteScalar().ToString(); needed because its contain string with number
0

I think you have choosen the wrong column to assign the return value of your ExecuteScalar query. From your error message it is clear that the return value is a string "VI BUILDING 29BD" (or something like that) and you are trying to assing that value to the same column dt.Rows[i][1] that you have used as the source for the ID parameter. This of course cannot be correct.

So you need to identify what is the column that you want to assign the return value of the ExecuteScalar and take extra care on handling that return value that could be NULL if there is no match on the WHERE clause

DataTable dt = new DataTable();
mySqlDataAdapter.Fill(dt);
int socot = dt.Rows.Count;
for (int i = 0; i < socot; i++)
{
    String dn = dt.Rows[i][1].ToString();
    String gduan = "SELECT tennha FROM duannha WHERE id=@id";
    MySqlCommand cmd = new MySqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = gduan;
    cmd.Parameters.AddWithValue("@id", dn);
    object result = cmd.ExecuteScalar();

    // SUPPOSE THAT YOU WANT TO ASSIGN AT COLUMN 2 NOT STILL AT COLUMN 1
    if(result != null)
         dt.Rows[i][2] = gtennha;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.