Solved

Prepare: CommandText property has not been initialized

Posted on 2009-04-08
7
1,025 Views
Last Modified: 2012-05-06
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
        ds.Tables("Shot").Rows.Add(dsNewRow)
        da.Update(ds, "Shot")

Open in new window

0
Comment
Question by:baz86
  • 5
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
srikanthreddyn143 earned 500 total points
ID: 24101244
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){
               Console.WriteLine(
                  "{0} {1}",
                  row["firstname"].ToString().PadRight(15),
                  row["lastname"].ToString().PadLeft(25));
            }
 
            // 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 {
            conn.Close();
         }
      }  
   }

Open in new window

0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24101261
Or If you use CommandBuilder, it will automatically take care of update statement. Please check the link

http://support.microsoft.com/kb/308055

0
 

Author Comment

by:baz86
ID: 24103060
thanks for the reply
if you check my code snippet i have used commandbuilder..but i still get the same message
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24106886
My Bad. What is the SQL query you are using at command object?

And also do you have to specify shotID?
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24106966
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.
0
 

Author Comment

by:baz86
ID: 24111987
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
        conn.Open()
        Dim sql As String = "SELECT * From tblShot"
        da = New SqlCeDataAdapter(sql, conn)

Open in new window

0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24112137
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.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I have an 11 digit bar code But I want to only need to return a portion of the Barcode 1 32
LINQ - C# to VB convertion 12 56
Gridview selected row 9 44
Disable extension 8 37
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question