data grid update

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KevinMGore

    #1

    data grid update

    The following code:
    Try
    da.Update(ds.Ta bles(dg1Name))
    Catch ex As Exception
    Console.WriteLi ne(ex.ToString)
    MessageBox.Show (ex.ToString)
    End Try

    causes this error:

    System.InvalidO perationExcepti on: Update requires a valid UpdateCommand when
    passed DataRow collection with modified rows.
    at System.Data.Com mon.DbDataAdapt er.Update(DataR ow[] dataRows,
    DataTableMappin g tableMapping)
    at System.Data.Com mon.DbDataAdapt er.Update(DataT able dataTable)
    at comments.Form1. Button1_Click(O bject sender, EventArgs e) in
    G:\Shared\VBSha re\dotnet\Kevin \comments\Form1 .vb:line 161

    The code is behiend a button on the form called Update. If I change the data
    in the grid I get the error when I press the update button. Any advice will
    be much appreciated.

    Kevin

  • Cor Ligthert

    #2
    Re: data grid update

    Kevin,

    How did you create your update command?

    You have as far as I know 3 possibilities
    - by hand yourself
    - using an AdapterWizard
    - using an commandbuilder.

    Now your error can be everything.

    Cor


    Comment

    • KevinMGore

      #3
      Re: data grid update

      Cor,
      I did not create an update command, I used the following code to fill the
      grid, changed one of the records and thought that the dataset and data
      adaptor took care of the rest?

      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      Dim commands As String = Microsoft.Visua lBasic.Command
      Dim strDatabase, strSQL As String

      strDatabase = Mid(commands, 1, 7)
      gstrCustomer = Mid(commands, 9, 12)
      gstrOrder = Mid(commands, 22, 8)
      gstrOrdType = Mid(commands, 31, 1)

      Call connectSQL(strD atabase)

      'fill the grid
      strSQL = "select '" & "Y" & "' as Selected, seqNo, comment from
      sql_oe_comments where custNo = '" & gstrCustomer & "'"
      cb = New SqlCommandBuild er(da)

      ds = New DataSet()

      da = New SqlDataAdapter( strSQL, sqlConn)

      da.Fill(ds, dg1Name)

      With DG1 'this is the datagrid on the form
      .DataSource = ds.Tables(dg1Na me)
      .AllowSorting = True
      .AlternatingBac kColor = System.Drawing. Color.Bisque
      End With

      'format the grid
      Call formatGridDG1()
      End Sub
      "Cor Ligthert" wrote:
      [color=blue]
      > Kevin,
      >
      > How did you create your update command?
      >
      > You have as far as I know 3 possibilities
      > - by hand yourself
      > - using an AdapterWizard
      > - using an commandbuilder.
      >
      > Now your error can be everything.
      >
      > Cor
      >
      >
      >[/color]

      Comment

      • Cor Ligthert

        #4
        Re: data grid update

        Kevin,

        There is a commandbuilder in the code you showed and that I asked for.

        By the way, are you sure that this works[color=blue]
        > da.Fill(ds, dg1Name)[/color]
        And not just this
        da.Fill(ds)

        Let us assume that you have a button on your form on what you click and in
        that click event you set this code

        BindingContext( ds.Tables(0)).E ndCurrentEdit()
        Try
        da.Update(ds.Ta bles(0))
        Catch ex As Exception
        Console.WriteLi ne(ex.ToString)
        MessageBox.Show (ex.ToString)
        End Try

        Than it is bad code, however it will probably work and when you are a newbie
        is that one of the first things that is important.

        I hope this helps

        Cor.



        Comment

        • KevinMGore

          #5
          Re: data grid update

          Cor,
          Thanks for the reply. dg1Name is a public constant. I found (by trial and
          error) that if I don't use it in these three places: da.Fill(ds, dg1Name),
          ..DataSource = ds.Tables(dg1Na me), ts.MappingName = dg1Name I get an error on
          this line of code: Me.DG1.TableSty les(0).GridColu mnStyles(2).Hea derText =
          "COMMENTS"

          I took out the grid foramtting and used ds.fill(0) as you suggested adding
          the binding code and received the same error originally reported about the
          command builder? If there is a better way to do this please let me know, the
          book I have suggests that it should work, obviously I must be doing something
          wrong.

          Thanks,

          Kevin
          [color=blue]
          > Kevin,
          >
          > There is a commandbuilder in the code you showed and that I asked for.
          >
          > By the way, are you sure that this works[color=green]
          > > da.Fill(ds, dg1Name)[/color]
          > And not just this
          > da.Fill(ds)
          >
          > Let us assume that you have a button on your form on what you click and in
          > that click event you set this code
          >
          > BindingContext( ds.Tables(0)).E ndCurrentEdit()
          > Try
          > da.Update(ds.Ta bles(0))
          > Catch ex As Exception
          > Console.WriteLi ne(ex.ToString)
          > MessageBox.Show (ex.ToString)
          > End Try
          >
          > Than it is bad code, however it will probably work and when you are a newbie
          > is that one of the first things that is important.
          >
          > I hope this helps
          >
          > Cor.
          >
          >
          >
          >[/color]

          Comment

          • Cor Ligthert

            #6
            Re: data grid update

            Kevin,

            Your select statement looks not simple for me to translate for the
            commandbuilder, can you not first try it with a simple one as

            "Select * from MyTable"

            Cor


            Comment

            • KevinMGore

              #7
              Re: data grid update

              Cor,
              I tried a simple statement and got the error: "update requires a valid
              update command..." I have a command builder object but have not structured
              any update statements for it? Is there a repository of code samples that show
              how this is done? The book I have isn't clear about how the update works.

              Thanks,

              Kevin

              "Cor Ligthert" wrote:
              [color=blue]
              > Kevin,
              >
              > Your select statement looks not simple for me to translate for the
              > commandbuilder, can you not first try it with a simple one as
              >
              > "Select * from MyTable"
              >
              > Cor
              >
              >
              >[/color]

              Comment

              • Cor Ligthert

                #8
                Re: data grid update

                Kevin,

                Can you show us a part of your code as you have it now, the commandbuilder
                needs in most cases like yours, nothing extra than that it is called.

                Cor


                Comment

                • KevinMGore

                  #9
                  Re: data grid update

                  Cor,
                  Here's the code I'm currently using to fill the grid:
                  Public ds As New Data.DataSet()
                  Public da As SqlClient.SqlDa taAdapter
                  Public cmd As SqlClient.SqlCo mmandBuilder()
                  Private Sub Form Load
                  strConn = "Initial Catalog=data_05 ;Data Source=kevin;Us er
                  ID=test;passwor d=test;"
                  strSQL = "select * from sql_oe_comments "
                  da = New SqlClient.SqlDa taAdapter(strSQ L, strConn)
                  da.Fill(ds)
                  dg1.DataSource = ds.Tables(0)

                  End Sub
                  Then I modify data in the grid and click the update button which has the
                  following code:
                  Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                  System.EventArg s) Handles Button1.Click
                  Try
                  dg1.Update()
                  'da.update(ds.t ables(0))

                  Catch ex As Exception
                  MessageBox.Show (ex.ToString)
                  End Try

                  End Sub
                  With this code I do not get an error when I press the button but the sql
                  data does not change in the database? If I uncomment the da.update... then I
                  get an error that a valid update command is required?
                  "Cor Ligthert" wrote:
                  [color=blue]
                  > Kevin,
                  >
                  > Can you show us a part of your code as you have it now, the commandbuilder
                  > needs in most cases like yours, nothing extra than that it is called.
                  >
                  > Cor
                  >
                  >
                  >[/color]

                  Comment

                  • Cor Ligthert

                    #10
                    Re: data grid update

                    Kevin,

                    I am glad I asked it to show some code, you don't use a commandbuilder, you
                    only create a placeholder for it. Can you try the changes I wrote in line. I
                    did it in this message so watch typos.
                    [color=blue]
                    > Public ds As New Data.DataSet()
                    > Public da As SqlClient.SqlDa taAdapter[/color]
                    ............[color=blue]
                    > Private Sub Form Load
                    > strConn = "Initial Catalog=data_05 ;Data Source=kevin;Us er
                    > ID=test;passwor d=test;"
                    > strSQL = "select * from sql_oe_comments "
                    > da = New SqlClient.SqlDa taAdapter(strSQ L, strConn)
                    > da.Fill(ds)[/color]

                    dim cmd as new sqlClient.SqlCo mmandBuilder(da )
                    dg1.DataSource = ds.Tables(0).de faultview
                    [color=blue]
                    > End Sub[/color]

                    [color=blue]
                    > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                    > System.EventArg s) Handles Button1.Click[/color]
                    Me.BindingConte xt(dg1.DataSour ce).EndCurrentE dit()
                    [color=blue]
                    > Try[/color]
                    da.update(ds.ta bles(0))[color=blue]
                    > Catch ex As Exception
                    > MessageBox.Show (ex.ToString)
                    > End Try
                    >
                    > End Sub[/color]

                    I hope this helps,

                    Cor


                    Comment

                    • KevinMGore

                      #11
                      Re: data grid update

                      Thank you very much - it works now.

                      "Cor Ligthert" wrote:
                      [color=blue]
                      > Kevin,
                      >
                      > I am glad I asked it to show some code, you don't use a commandbuilder, you
                      > only create a placeholder for it. Can you try the changes I wrote in line. I
                      > did it in this message so watch typos.
                      >[color=green]
                      > > Public ds As New Data.DataSet()
                      > > Public da As SqlClient.SqlDa taAdapter[/color]
                      > ............[color=green]
                      > > Private Sub Form Load
                      > > strConn = "Initial Catalog=data_05 ;Data Source=kevin;Us er
                      > > ID=test;passwor d=test;"
                      > > strSQL = "select * from sql_oe_comments "
                      > > da = New SqlClient.SqlDa taAdapter(strSQ L, strConn)
                      > > da.Fill(ds)[/color]
                      >
                      > dim cmd as new sqlClient.SqlCo mmandBuilder(da )
                      > dg1.DataSource = ds.Tables(0).de faultview
                      >[color=green]
                      > > End Sub[/color]
                      >
                      >[color=green]
                      > > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                      > > System.EventArg s) Handles Button1.Click[/color]
                      > Me.BindingConte xt(dg1.DataSour ce).EndCurrentE dit()
                      >[color=green]
                      > > Try[/color]
                      > da.update(ds.ta bles(0))[color=green]
                      > > Catch ex As Exception
                      > > MessageBox.Show (ex.ToString)
                      > > End Try
                      > >
                      > > End Sub[/color]
                      >
                      > I hope this helps,
                      >
                      > Cor
                      >
                      >
                      >[/color]

                      Comment

                      Working...