Skip to content Skip to sidebar Skip to footer

Vb.net Mysql Datagridview Is Empty?

When I click on search button, this code below will run. There are no errors on the code but datagridview shows only the column name.. Is there a mistake for the query? mySqlCo

Solution 1:

If your intention is to be able to ignore criteria if the user leaves a field empty then you actually have to pass a NULL value to the query in that case. Just as String.Empty and Nothing are not the same thing in VB, so an empty string and NULL are not the same thing in SQL. You would have to do something like this:

Dim sql=<sql>SELECT*FROM MyTable
              WHERE (@Column1ISNULLOR Column1 =@Column1)
              AND (@Column2ISNULLOR Column2 =@Column2)
          </sql>

myCommand.CommandText = sql.Value

Dim column1 = TextBox1.Text.Trim()
Dim column2 = TextBox2.Text.Trim()

With myCommand.Parameters
    .Add("@Column1", SqlDbType.VarChar).Value= If(column1 = String.Empty, CObj(DBNull.Value), column1)
    .Add("@Column2", SqlDbType.VarChar).Value= If(column2 = String.Empty, CObj(DBNull.Value), column2)
EndWith

Note that the parameters are added using Add rather than AddWithValue, because a data type cannot be inferred from DBNull.Value

Post a Comment for "Vb.net Mysql Datagridview Is Empty?"