Prepare: CommandText property has not been initialized

Hi ,
i keep getting this message(InvalidOperationException was unhandled.....Prepare: CommandText property has not been initialized) during debugging at ......da.Update(ds, "Shot") ....

can someone please explain what it means and how i can rectify it?

many thanks
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
        Dim da As New SqlCeDataAdapter(sql, conn)
        Dim cb As New SqlCeCommandBuilder(da)
        dsNewRow = ds.Tables("Shot").NewRow()
        dsNewRow.Item("ShotID") = scorecount + 1
        dsNewRow.Item("Club") = ComboBox2.Text
        dsNewRow.Item("ActDist") = TextBox30.Text
        dsNewRow.Item("EndLie") = ComboBox6.Text
        dsNewRow.Item("PuttDist") = ComboBox9.Text
        dsNewRow.Item("WindStrength") = WeatherCond.ComboBox3.Text
        dsNewRow.Item("Assist") = WeatherCond.ComboBox5.Text
        dsNewRow.Item("Crosswind") = WeatherCond.ComboBox7.Text
        dsNewRow.Item("HoleNo") = Label5.Text
        dsNewRow.Item("RoundID") = maxroundid()
        dsNewRow.Item("BallFlight") = ComboBox1.Text
        da.Update(ds, "Shot")

Open in new window

Who is Participating?
srikanthreddyn143Connect With a Mentor Commented:
You have to specify update command for the dataadapter.Just look into attached code.I got it from Internet.
using System;
using System.Data;
using System.Data.SqlClient;
   class PropagateChanges {
      static void Main(){
         string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
         string qry = @"select * from employee ";
         string upd = @"update employee set firstname = @firstname where id = @id";
         SqlConnection conn = new SqlConnection(connString);
         try {
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand(qry, conn);
            DataSet ds = new DataSet();   
            da.Fill(ds, "employee");
            DataTable dt = ds.Tables["employee"];
            dt.Rows[0]["firstname"] = "W";
            foreach (DataRow row in dt.Rows){
                  "{0} {1}",
            // Update employees
            SqlCommand cmd = new SqlCommand(upd, conn);
            cmd.Parameters.Add("@firstname",SqlDbType.NVarChar,15, "firstname");
            SqlParameter parm = cmd.Parameters.Add("@id",SqlDbType.Int,4,"id");
            parm.SourceVersion = DataRowVersion.Original;
            da.UpdateCommand = cmd;
            da.Update(ds, "employee");
         } catch(Exception e) {
            Console.WriteLine("Error: " + e);
         } finally {

Open in new window

Or If you use CommandBuilder, it will automatically take care of update statement. Please check the link

baz86Author Commented:
thanks for the reply
if you check my code snippet i have used commandbuilder..but i still get the same message
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

My Bad. What is the SQL query you are using at command object?

And also do you have to specify shotID?
Is SHOTID Primary Key?It seems commandbuilder works only when there is unique key values. Please look into your ShotID values during run time and also look into Update statement using
cmdBuilder.GetUpdateCommand.CommandText to know what is the update command that is getting.
baz86Author Commented:
this is the sql statement i run at form load
ShotID is quiet misleading here (sorry about that), it isnt a primary key  but just an increment of the shots taken.

conn.ConnectionString = ConnStr
        Dim sql As String = "SELECT * From tblShot"
        da = New SqlCeDataAdapter(sql, conn)

Open in new window

Then CommandBuilder doesnt work for it. You have to manually give the update statement for the adapter with parameters like the code I have given on the top.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.