-1

When I click on this button, I face with this error:

executenonquery commandtext property has not been initialized

private void button_FirstStep_Click(object sender, EventArgs e)
{
    SqlConnection Conn = new SqlConnection(Yahya.strcon);
    Conn.Open();
    int CurrentCount = Convert.ToInt32(label_CurrentCount.Text);
    string strcom1 = "select * from vm1 where count = '" + (CurrentCount - 1) + "' and benchmarkid = '" + Structure.BenchmarkID + "' ";
    SqlCommand cmd = new SqlCommand(strcom1, Conn);
    SqlDataReader reader = cmd.ExecuteReader();
    string strcom = "";
    while (reader.Read())
    {
        if (reader["vmid"].ToString() != "")
        {
            string vmid = reader["vmid"].ToString();
            strcom += "update vm1 set pmid = (select pmid from vm1 as VM2  where   benchmarkid = '" + Structure.BenchmarkID + "' and  vm2.count ='" + (CurrentCount - 1) + "' and vm2.vmid ='" + vmid + "' ) where count  =  '" + CurrentCount + "'  and vmid = '" + vmid + "' and  benchmarkid = '" + Structure.BenchmarkID + "'  \n";
        }
    }//end of while
    reader.Close();
    cmd.CommandText = strcom;
    cmd.ExecuteNonQuery();
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
Yahya Zahedi
  • 21
  • 1
  • 1
  • 1
  • 2
    Did you debug it? How many rows were returned from by `ExecuteReader`? If there were no results, `reader.Read` was never `true` so `strcom` is still empty. – René Vogt Feb 12 '16 at 11:36
  • [Bobby Tables](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work) is coming to this party and he says that he brings lot of friends – Steve Feb 12 '16 at 11:38
  • also, you make a point of casting currentCount to an int, then treat it as a varchar in the query by surrounding it in quotes - is Count in vb1 numerical? If so, drop the quotes (or better still parameterise the queries) – NDJ Feb 12 '16 at 11:43

2 Answers2

1

Rene is quite right about his comment, looks like your reader.Read() returns false and that's why your code never goes into your while loop and your CommandText is assigned to "", that's why ExecuteNonQuery throws

ExecuteNonQuery: CommandText property has not been initialized

You can check your strcom is empty string or not to solve your problem but I see more wrong things in your code other than that..

  • Looks like your count column is numeric value but you supplied your CurrentCount - 1 as a character with single quotes. If it is not numeric, it should. Read: Bad habits to kick : choosing the wrong data type
  • Based on it's name, benchmarkid should(?) be numeric types as well.
  • You can solve this two problem with using parameterized queries because this kind of string concatenations are open for SQL Injection attacks.
  • Use using statement to dispose your connection, command and reader automatically instead of calling Close or Dispose methods manually.
  • Open your connection just before you execute your command.
Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

You could solve this by simply debugging before asking.

The reason for this error is presumably that your first request returns zero results.
So reader.Read() is always false and strcom stays empty. You set an empty string as cmd.CommandText before the call to ExecuteNonQuery().

To solve this, simply check if the string is empty and execute the last query only if it is not empty:

...
reader.Close();
if (!string.IsNullOrEmpty(strcom))
{
    cmd.CommandText = strcom;
    cmd.ExecuteNonQuery();
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99