开发者

C# Database Insert (ASP.NET) - ExecuteNonQuery: CommandText property has not been initialized

first time I'm doing an insert from ASP.NET/C# and I'm having a little issue. I keep getting the following error every time this code runs: " ExecuteNonQuery: CommandText property has not been initialized" Does anyone know what this means and how I fix it?

Thanks in advance!

string sqlQuery = "INSERT INTO ATI_LOG_IO (Date, Connect_Time, Disconnect_Time, ATI_Rep, Reason_For_Access, Property_Contact, Case_Number, Comments, Property_ID)";
sqlQuery += "VALUES (@Today, @Connect, @Disconnect, @Rep, @Reason, @Contact, @CaseNum, @Comments, @PropertyID)";
using (SqlConnection dataConnection = new SqlConnection(connectionString))
{
    using (SqlCommand dataCommand = dataConnection.CreateCommand())
    {
        dataConnection.Open();
        dataCommand.CommandType = CommandType.Text;
        dataCommand.CommandText = sqlQuery;
        dataCommand.Parameters.Add("@Today", DateTime.T开发者_如何学Gooday.ToString());
        dataCommand.Parameters.Add("@Connect", txtInDate.Text + " " + fromHrs.Text + ":" + fromMins.Text + ":00");
        dataCommand.Parameters.Add("@Disconnect", txtOutdate.Text + " " + toHrs.Text + ":" + fromMins.Text + ":00");
        dataCommand.Parameters.Add("@Rep", repID);
        dataCommand.Parameters.Add("@Reason", txtReason.Text);
        dataCommand.Parameters.Add("@Contact", txtContact.Text);
        dataCommand.Parameters.Add("@CaseNum", txtCaseNum.Text);
        dataCommand.Parameters.Add("@Comments", txtComments.Text);
        dataCommand.Parameters.Add("@PropertyID", lstProperties.SelectedValue);
        dataCommand.ExecuteNonQuery();
        dataConnection.Close();
    }
}


string sqlQuery = "INSERT INTO ATI_LOG_IO (Date, Connect_Time, Disconnect_Time, ATI_Rep, Reason_For_Access, Property_Contact, Case_Number, Comments, Property_ID)";
sqlQuery += " VALUES (@Today, @Connect, @Disconnect, @Rep, @Reason, @Contact, @CaseNum, @Comments, @PropertyID)";
using (SqlConnection dataConnection = new SqlConnection(connectionString))
{
    using (SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection))
    {
        dataCommand.Parameters.AddWithValue("Today", DateTime.Today.ToString());
        dataCommand.Parameters.AddWithValue("Connect", txtInDate.Text + " " + fromHrs.Text + ":" + fromMins.Text + ":00");
        dataCommand.Parameters.AddWithValue("Disconnect", txtOutdate.Text + " " + toHrs.Text + ":" + fromMins.Text + ":00");
        dataCommand.Parameters.AddWithValue("Rep", repID);
        dataCommand.Parameters.AddWithValue("Reason", txtReason.Text);
        dataCommand.Parameters.AddWithValue("Contact", txtContact.Text);
        dataCommand.Parameters.AddWithValue("CaseNum", txtCaseNum.Text);
        dataCommand.Parameters.AddWithValue("Comments", txtComments.Text);
        dataCommand.Parameters.AddWithValue("PropertyID", lstProperties.SelectedValue);

        dataConnection.Open();
        dataCommand.ExecuteNonQuery();
        dataConnection.Close();
    }
}

Copy-paste should do the trick


This usually means you haven't set the CommandText property, but in your case, you have.

You should try testing that the sqlQuery string is actually not empty at this line:

dataCommand.CommandText = sqlQuery;

P.S. As a "best practice", you may want to consider opening the connection AFTER setting up the SqlCommand object, to minimize the time spent with an open connection:

    dataCommand.CommandType = CommandType.Text;
    dataCommand.CommandText = sqlQuery;
    dataCommand.Parameters.Add("@Today", DateTime.Today.ToString());
    //...
    dataConnection.Open();
    dataCommand.ExecuteNonQuery();
    dataConnection.Close();


Looking at your string sql query, you're not leaving a space between the "INTO" part and "VALUES" part.

...............Property_ID)";
sqlQuery += "VALUES (@Today, ..............

SHOULD BE:

...............Property_ID)";
sqlQuery += " VALUES (@Today, ..............
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜