Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio
  4. On UPDATE, Vertical Scroll moves to first row instead of selected row for editing.

On UPDATE, Vertical Scroll moves to first row instead of selected row for editing.

Scheduled Pinned Locked Moved Visual Studio
helpdatabaseannouncement
19 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VSLearner2013
    wrote on last edited by
    #1

    When I edit and press btnUpdate, the scroll bar moves to first row instead of the row which is highlighted for editing. The following is the code that I am using now:

    Private Sub UpdateGrid(ByVal RowIndex As Integer)
    Dim StrSql As String = "Select * from SalesTable"
    Dim dt As New DataTable
    If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
    Me.DataGridView1.DataSource = dt
    If dt Is Nothing Or dt.Rows.Count <= 0 Then
    Exit Sub
    End If
    DataGridView1.Rows(RowIndex).Selected = True
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim dt As New DataTable
    If btnSave.Text = "Save" Then
    Dim StrSql = "INSERT INTO SalesTable(A1,A2,A3,A4) VALUES('" _
    + Me.txt1.Text + "','" _
    + Me.dtp2.Text + "','" _
    + Me.cbo3.Text + "','" _
    + Me.txt4.Text + "')"

            If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
    
        Else
    
            Dim StrSql = "UPDATE SalesTable SET " \_
                            + "A1='" + Me.txt1.Text + "'," \_
                            + "A2='" + Me.dtp2.Text + "'," \_
                            + "A3='" + Me.cbo3.Text + "'," \_
                            + "A4='" + Me.txt4.Text + "' Where \[ID\] =  " & txtID.Text
    
            If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
    
        End If
        btnAdd.Select()
        If btnSave.Text = "Save" Then
            Call UpdateGrid(RecordCount() - 1)
        Else
            Call UpdateGrid(DataGridView1.SelectedRows(0).Index)
        End If
        btnAdd.Select()
    End Sub
    

    Please help me with the right code.

    L Richard DeemingR 2 Replies Last reply
    0
    • V VSLearner2013

      When I edit and press btnUpdate, the scroll bar moves to first row instead of the row which is highlighted for editing. The following is the code that I am using now:

      Private Sub UpdateGrid(ByVal RowIndex As Integer)
      Dim StrSql As String = "Select * from SalesTable"
      Dim dt As New DataTable
      If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
      Me.DataGridView1.DataSource = dt
      If dt Is Nothing Or dt.Rows.Count <= 0 Then
      Exit Sub
      End If
      DataGridView1.Rows(RowIndex).Selected = True
      End Sub

      Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
      Dim dt As New DataTable
      If btnSave.Text = "Save" Then
      Dim StrSql = "INSERT INTO SalesTable(A1,A2,A3,A4) VALUES('" _
      + Me.txt1.Text + "','" _
      + Me.dtp2.Text + "','" _
      + Me.cbo3.Text + "','" _
      + Me.txt4.Text + "')"

              If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
      
          Else
      
              Dim StrSql = "UPDATE SalesTable SET " \_
                              + "A1='" + Me.txt1.Text + "'," \_
                              + "A2='" + Me.dtp2.Text + "'," \_
                              + "A3='" + Me.cbo3.Text + "'," \_
                              + "A4='" + Me.txt4.Text + "' Where \[ID\] =  " & txtID.Text
      
              If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
      
          End If
          btnAdd.Select()
          If btnSave.Text = "Save" Then
              Call UpdateGrid(RecordCount() - 1)
          Else
              Call UpdateGrid(DataGridView1.SelectedRows(0).Index)
          End If
          btnAdd.Select()
      End Sub
      

      Please help me with the right code.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      VSLearner2013 wrote:

      When I edit and press btnUpdate

      1. Where is the code for btnUpdate? 2. This question belongs in the Visual basic forum.

      V 1 Reply Last reply
      0
      • L Lost User

        VSLearner2013 wrote:

        When I edit and press btnUpdate

        1. Where is the code for btnUpdate? 2. This question belongs in the Visual basic forum.

        V Offline
        V Offline
        VSLearner2013
        wrote on last edited by
        #3

        Sorry, it is btnSave and it's text will change to "Update" when I click on btnSave. So, the code for Save & Update both are int the btnSave event.

        L 1 Reply Last reply
        0
        • V VSLearner2013

          Sorry, it is btnSave and it's text will change to "Update" when I click on btnSave. So, the code for Save & Update both are int the btnSave event.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I don't see where the error is supposed to occur. What I do see is some very dangerous SQL statements that leaves your database open to SQL injection attacks. You should learn to use proper parameterised queries in your SQL statements.

          1 Reply Last reply
          0
          • V VSLearner2013

            When I edit and press btnUpdate, the scroll bar moves to first row instead of the row which is highlighted for editing. The following is the code that I am using now:

            Private Sub UpdateGrid(ByVal RowIndex As Integer)
            Dim StrSql As String = "Select * from SalesTable"
            Dim dt As New DataTable
            If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
            Me.DataGridView1.DataSource = dt
            If dt Is Nothing Or dt.Rows.Count <= 0 Then
            Exit Sub
            End If
            DataGridView1.Rows(RowIndex).Selected = True
            End Sub

            Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
            Dim dt As New DataTable
            If btnSave.Text = "Save" Then
            Dim StrSql = "INSERT INTO SalesTable(A1,A2,A3,A4) VALUES('" _
            + Me.txt1.Text + "','" _
            + Me.dtp2.Text + "','" _
            + Me.cbo3.Text + "','" _
            + Me.txt4.Text + "')"

                    If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
            
                Else
            
                    Dim StrSql = "UPDATE SalesTable SET " \_
                                    + "A1='" + Me.txt1.Text + "'," \_
                                    + "A2='" + Me.dtp2.Text + "'," \_
                                    + "A3='" + Me.cbo3.Text + "'," \_
                                    + "A4='" + Me.txt4.Text + "' Where \[ID\] =  " & txtID.Text
            
                    If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Exit Sub
            
                End If
                btnAdd.Select()
                If btnSave.Text = "Save" Then
                    Call UpdateGrid(RecordCount() - 1)
                Else
                    Call UpdateGrid(DataGridView1.SelectedRows(0).Index)
                End If
                btnAdd.Select()
            End Sub
            

            Please help me with the right code.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Your code is vulnerable to SQL Injection. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^] SQL injection attack mechanics | Pluralsight [^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            V 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Your code is vulnerable to SQL Injection. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^] SQL injection attack mechanics | Pluralsight [^]


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              V Offline
              V Offline
              VSLearner2013
              wrote on last edited by
              #6

              How can I parameterized the following query?

              Dim StrSql = "INSERT INTO SalesTable(A1,A2,A3,A4) VALUES('" _
              + Me.txt1.Text + "','" _
              + Me.dtp2.Text + "','" _
              + Me.cbo3.Text + "','" _
              + Me.txt4.Text + "')"

              Please help.

              Richard DeemingR 1 Reply Last reply
              0
              • V VSLearner2013

                How can I parameterized the following query?

                Dim StrSql = "INSERT INTO SalesTable(A1,A2,A3,A4) VALUES('" _
                + Me.txt1.Text + "','" _
                + Me.dtp2.Text + "','" _
                + Me.cbo3.Text + "','" _
                + Me.txt4.Text + "')"

                Please help.

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                You'll need to fix your DAL.ExecuteSql method to accept parameters, or ditch it and use raw ADO.NET or an ORM. The standard approach using raw ADO.NET would be:

                Using connection As New SqlConnection("YOUR CONNNECTION STRING HERE")
                Using command As New SqlCommand("INSERT INTO SalesTable (A1, A2, A3, A4) VALUES (@A1, @A2, @A3, @A4)", connection)
                command.Parameters.AddWithValue("@A1", Me.txt1.Text)
                command.Parameters.AddWithValue("@A2", Me.dtp2.Text)
                command.Parameters.AddWithValue("@A3", Me.cbo3.Text)
                command.Parameters.AddWithValue("@A4", Me.txt4.Text)

                    connection.Open()
                    command.ExecuteNonQuery()
                End Using
                

                End Using

                Also, do yourself a favour and give your controls meaningful names, rather than accepting the default names generated by the designer. You might remember what value txt4 holds now, but when you come back to the code in a few months, you'll have no idea what it means.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                V 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  You'll need to fix your DAL.ExecuteSql method to accept parameters, or ditch it and use raw ADO.NET or an ORM. The standard approach using raw ADO.NET would be:

                  Using connection As New SqlConnection("YOUR CONNNECTION STRING HERE")
                  Using command As New SqlCommand("INSERT INTO SalesTable (A1, A2, A3, A4) VALUES (@A1, @A2, @A3, @A4)", connection)
                  command.Parameters.AddWithValue("@A1", Me.txt1.Text)
                  command.Parameters.AddWithValue("@A2", Me.dtp2.Text)
                  command.Parameters.AddWithValue("@A3", Me.cbo3.Text)
                  command.Parameters.AddWithValue("@A4", Me.txt4.Text)

                      connection.Open()
                      command.ExecuteNonQuery()
                  End Using
                  

                  End Using

                  Also, do yourself a favour and give your controls meaningful names, rather than accepting the default names generated by the designer. You might remember what value txt4 holds now, but when you come back to the code in a few months, you'll have no idea what it means.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  V Offline
                  V Offline
                  VSLearner2013
                  wrote on last edited by
                  #8

                  Thanks for your kind reply. Being a learner, I got this code from other source and using it in my sample project. The following code is the code in the module:

                  Imports System.Data.OleDb
                  Imports System.Data
                  Module DAL
                  Dim OledbCon As OleDbConnection
                  Public Msg As String = String.Empty
                  Public Function GetCon() As OleDbConnection
                  Dim DbFullPath As String = Application.StartupPath + "\"
                  DbFullPath += "MyDataBase.accdb"
                  If OledbCon Is Nothing Then
                  OledbCon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DbFullPath + ";Jet OLEDB:Database Password=123456;")
                  OledbCon.Open()
                  Return OledbCon
                  End If
                  If OledbCon.State = ConnectionState.Open Then Return OledbCon
                  OledbCon.Open()
                  Return OledbCon
                  End Function
                  Public Function ExecuteSql(ByRef dt As DataTable, ByVal StrSql As String, ByRef Msg As String) As Boolean
                  Try
                  Dim Cmd As OleDbCommand = New OleDbCommand(StrSql, GetCon())
                  Cmd.CommandType = CommandType.Text
                  Dim DA As New OleDbDataAdapter
                  DA.SelectCommand = Cmd
                  DA.Fill(dt)
                  Return True
                  Catch ex As Exception
                  Msg = ex.Message.ToString()
                  Return False
                  End Try
                  End Function
                  End Module

                  Will the above code DAL.ExecuteSql which is in module works for me? Or how can I modify the module code accordingly.

                  Richard DeemingR 1 Reply Last reply
                  0
                  • V VSLearner2013

                    Thanks for your kind reply. Being a learner, I got this code from other source and using it in my sample project. The following code is the code in the module:

                    Imports System.Data.OleDb
                    Imports System.Data
                    Module DAL
                    Dim OledbCon As OleDbConnection
                    Public Msg As String = String.Empty
                    Public Function GetCon() As OleDbConnection
                    Dim DbFullPath As String = Application.StartupPath + "\"
                    DbFullPath += "MyDataBase.accdb"
                    If OledbCon Is Nothing Then
                    OledbCon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DbFullPath + ";Jet OLEDB:Database Password=123456;")
                    OledbCon.Open()
                    Return OledbCon
                    End If
                    If OledbCon.State = ConnectionState.Open Then Return OledbCon
                    OledbCon.Open()
                    Return OledbCon
                    End Function
                    Public Function ExecuteSql(ByRef dt As DataTable, ByVal StrSql As String, ByRef Msg As String) As Boolean
                    Try
                    Dim Cmd As OleDbCommand = New OleDbCommand(StrSql, GetCon())
                    Cmd.CommandType = CommandType.Text
                    Dim DA As New OleDbDataAdapter
                    DA.SelectCommand = Cmd
                    DA.Fill(dt)
                    Return True
                    Catch ex As Exception
                    Msg = ex.Message.ToString()
                    Return False
                    End Try
                    End Function
                    End Module

                    Will the above code DAL.ExecuteSql which is in module works for me? Or how can I modify the module code accordingly.

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    That code has a lot of problems. Try something like this instead:

                    Imports System.Data
                    Imports System.Data.OleDb
                    Imports System.Globalization

                    Module DAL
                    Private Function GetConnection() As OleDbConnection
                    Dim result As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MyDataBase.accdb;Jet OLEDB:Database Password=123456;")
                    result.Open()
                    Return result
                    End Function

                    Private Function CreateCommand(ByVal connection As OleDbConnection, ByVal query As String, ByVal ParamArray parameters() As Object) As OleDbCommand
                        Dim command As OleDbCommand = connection.CreateCommand()
                        
                        If parameters IsNot Nothing Then
                            For index = 0 To parameters.Length - 1
                                Dim i As String = index.ToString("D", CultureInfo.InvariantCulture)
                                Dim p As String = "{" + i + "}"
                                Dim name As String = "@p" + i
                                query = query.Replace(p, name)
                                command.Parameters.AddWithValue(name, parameters(index))
                            Next
                        End If
                        
                        command.CommandText = query
                        Return command
                    End Function
                    
                    Public Function LoadData(ByVal query As String, ByVal ParamArray parameters() As Object) As DataSet
                        Using connection As OleDbConnection = GetConnection()
                            Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                Dim result As New DataSet()
                                Dim da As New OleDbDataAdapter(command)
                                da.Fill(result)
                                Return result
                            End Using
                        End Using
                    End Function
                    
                    Public Function ExecuteScalar(ByVal query As String, ByVal ParamArray parameters() As Object) As Object
                        Using connection As OleDbConnection = GetConnection()
                            Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                Return command.ExecuteScalar()
                            End Using
                        End Using
                    End Function
                    
                    Public Sub ExecuteNonQuery(ByVal query As String, ByVal ParamArray parameters() As Object)
                        Using connection As OleDbConnection = GetConnection()
                            Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                command.ExecuteNonQuery()
                            End Using
                        End Using
                    End Sub
                    

                    End Module

                    You have three public methods to call - LoadData when you want to load data into a Da

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    V 2 Replies Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      That code has a lot of problems. Try something like this instead:

                      Imports System.Data
                      Imports System.Data.OleDb
                      Imports System.Globalization

                      Module DAL
                      Private Function GetConnection() As OleDbConnection
                      Dim result As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MyDataBase.accdb;Jet OLEDB:Database Password=123456;")
                      result.Open()
                      Return result
                      End Function

                      Private Function CreateCommand(ByVal connection As OleDbConnection, ByVal query As String, ByVal ParamArray parameters() As Object) As OleDbCommand
                          Dim command As OleDbCommand = connection.CreateCommand()
                          
                          If parameters IsNot Nothing Then
                              For index = 0 To parameters.Length - 1
                                  Dim i As String = index.ToString("D", CultureInfo.InvariantCulture)
                                  Dim p As String = "{" + i + "}"
                                  Dim name As String = "@p" + i
                                  query = query.Replace(p, name)
                                  command.Parameters.AddWithValue(name, parameters(index))
                              Next
                          End If
                          
                          command.CommandText = query
                          Return command
                      End Function
                      
                      Public Function LoadData(ByVal query As String, ByVal ParamArray parameters() As Object) As DataSet
                          Using connection As OleDbConnection = GetConnection()
                              Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                  Dim result As New DataSet()
                                  Dim da As New OleDbDataAdapter(command)
                                  da.Fill(result)
                                  Return result
                              End Using
                          End Using
                      End Function
                      
                      Public Function ExecuteScalar(ByVal query As String, ByVal ParamArray parameters() As Object) As Object
                          Using connection As OleDbConnection = GetConnection()
                              Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                  Return command.ExecuteScalar()
                              End Using
                          End Using
                      End Function
                      
                      Public Sub ExecuteNonQuery(ByVal query As String, ByVal ParamArray parameters() As Object)
                          Using connection As OleDbConnection = GetConnection()
                              Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                  command.ExecuteNonQuery()
                              End Using
                          End Using
                      End Sub
                      

                      End Module

                      You have three public methods to call - LoadData when you want to load data into a Da

                      V Offline
                      V Offline
                      VSLearner2013
                      wrote on last edited by
                      #10

                      Thanks a million to your kind and detail reply. I will try it now in my project and will comeback.

                      1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        That code has a lot of problems. Try something like this instead:

                        Imports System.Data
                        Imports System.Data.OleDb
                        Imports System.Globalization

                        Module DAL
                        Private Function GetConnection() As OleDbConnection
                        Dim result As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MyDataBase.accdb;Jet OLEDB:Database Password=123456;")
                        result.Open()
                        Return result
                        End Function

                        Private Function CreateCommand(ByVal connection As OleDbConnection, ByVal query As String, ByVal ParamArray parameters() As Object) As OleDbCommand
                            Dim command As OleDbCommand = connection.CreateCommand()
                            
                            If parameters IsNot Nothing Then
                                For index = 0 To parameters.Length - 1
                                    Dim i As String = index.ToString("D", CultureInfo.InvariantCulture)
                                    Dim p As String = "{" + i + "}"
                                    Dim name As String = "@p" + i
                                    query = query.Replace(p, name)
                                    command.Parameters.AddWithValue(name, parameters(index))
                                Next
                            End If
                            
                            command.CommandText = query
                            Return command
                        End Function
                        
                        Public Function LoadData(ByVal query As String, ByVal ParamArray parameters() As Object) As DataSet
                            Using connection As OleDbConnection = GetConnection()
                                Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                    Dim result As New DataSet()
                                    Dim da As New OleDbDataAdapter(command)
                                    da.Fill(result)
                                    Return result
                                End Using
                            End Using
                        End Function
                        
                        Public Function ExecuteScalar(ByVal query As String, ByVal ParamArray parameters() As Object) As Object
                            Using connection As OleDbConnection = GetConnection()
                                Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                    Return command.ExecuteScalar()
                                End Using
                            End Using
                        End Function
                        
                        Public Sub ExecuteNonQuery(ByVal query As String, ByVal ParamArray parameters() As Object)
                            Using connection As OleDbConnection = GetConnection()
                                Using command As OleDbCommand = CreateCommand(connection, query, parameters)
                                    command.ExecuteNonQuery()
                                End Using
                            End Using
                        End Sub
                        

                        End Module

                        You have three public methods to call - LoadData when you want to load data into a Da

                        V Offline
                        V Offline
                        VSLearner2013
                        wrote on last edited by
                        #11

                        I am getting the error at the this point of code in UpdateGrid function.

                        Private Sub UpdateGrid(ByVal RowIndex As Integer):

                        Dim dt As DataTable = DAL.LoadData("SELECT * FROM SalesTable")

                        Error 1 Value of type 'System.Data.DataSet' cannot be converted to 'System.Data.DataTable'.>

                        )

                        Richard DeemingR 1 Reply Last reply
                        0
                        • V VSLearner2013

                          I am getting the error at the this point of code in UpdateGrid function.

                          Private Sub UpdateGrid(ByVal RowIndex As Integer):

                          Dim dt As DataTable = DAL.LoadData("SELECT * FROM SalesTable")

                          Error 1 Value of type 'System.Data.DataSet' cannot be converted to 'System.Data.DataTable'.>

                          )

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #12

                          The LoadData method returns a DataSet, in case you want more than one resultset from your query. Try:

                          Dim dt As DataTable = DAL.LoadData("SELECT * FROM SalesTable").Tables(0)


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          V 1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            The LoadData method returns a DataSet, in case you want more than one resultset from your query. Try:

                            Dim dt As DataTable = DAL.LoadData("SELECT * FROM SalesTable").Tables(0)


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            V Offline
                            V Offline
                            VSLearner2013
                            wrote on last edited by
                            #13

                            I have 15 Fields in my MS Access Database Table (The first field is ID and it has AutoNumber property). Getting the following error on Updating a row:

                            Malformed GUID in query expression '{14}'.

                            btnSave.Text = "Update"
                            DAL.ExecuteNonQuery("UPDATE SalesTable SET ID={0},A1={1},A2={2},A3={3},A4={4},A5={5},A6={6},A7={7},A8={8},A9={9},A10={10},A11={11},A12={12},A13={13},A14={14} WHERE [ID] = {0}", _
                            Me.txt1.Text, _
                            Me.dtp2.Text, _
                            Me.cbo3.Text, _
                            Me.txt4.Text, _
                            Me.cbo5.Text, _
                            Me.txt6.Text, _
                            Me.cbo7.Text, _
                            Me.cbo8.Text, _
                            Me.cbo9.Text, _
                            Me.cbo10.Text, _
                            Me.cbo11.Text, _
                            Me.txt12.Text, _
                            Me.txt13.Text, _
                            Me.txt14.Text)
                            End If

                            Richard DeemingR 1 Reply Last reply
                            0
                            • V VSLearner2013

                              I have 15 Fields in my MS Access Database Table (The first field is ID and it has AutoNumber property). Getting the following error on Updating a row:

                              Malformed GUID in query expression '{14}'.

                              btnSave.Text = "Update"
                              DAL.ExecuteNonQuery("UPDATE SalesTable SET ID={0},A1={1},A2={2},A3={3},A4={4},A5={5},A6={6},A7={7},A8={8},A9={9},A10={10},A11={11},A12={12},A13={13},A14={14} WHERE [ID] = {0}", _
                              Me.txt1.Text, _
                              Me.dtp2.Text, _
                              Me.cbo3.Text, _
                              Me.txt4.Text, _
                              Me.cbo5.Text, _
                              Me.txt6.Text, _
                              Me.cbo7.Text, _
                              Me.cbo8.Text, _
                              Me.cbo9.Text, _
                              Me.cbo10.Text, _
                              Me.cbo11.Text, _
                              Me.txt12.Text, _
                              Me.txt13.Text, _
                              Me.txt14.Text)
                              End If

                              Richard DeemingR Offline
                              Richard DeemingR Offline
                              Richard Deeming
                              wrote on last edited by
                              #14

                              You're not passing enough parameters in. You've got 15 placeholders, but you're only passing in 14 parameters.


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                              V 1 Reply Last reply
                              0
                              • Richard DeemingR Richard Deeming

                                You're not passing enough parameters in. You've got 15 placeholders, but you're only passing in 14 parameters.


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                V Offline
                                V Offline
                                VSLearner2013
                                wrote on last edited by
                                #15

                                But when I add the first field ...

                                Me.txtID.Text, _

                                it gives this error:

                                Cannot update 'ID'; field not updateable.

                                Richard DeemingR 1 Reply Last reply
                                0
                                • V VSLearner2013

                                  But when I add the first field ...

                                  Me.txtID.Text, _

                                  it gives this error:

                                  Cannot update 'ID'; field not updateable.

                                  Richard DeemingR Offline
                                  Richard DeemingR Offline
                                  Richard Deeming
                                  wrote on last edited by
                                  #16

                                  Because you're trying to update a field which you can't update. Don't include the ID column in the SET part of the query:

                                  DAL.ExecuteNonQuery("UPDATE SalesTable SET A1={0}, A2={1}, A3={2}, A4={3}, A5={4}, A6={5}, A7={6}, A8={7}, A9={8}, A10={9}, A11={10}, A12={11}, A13={12}, A14={13} WHERE [ID] = {14}", _
                                  Me.txt1.Text, _
                                  Me.dtp2.Text, _
                                  Me.cbo3.Text, _
                                  Me.txt4.Text, _
                                  Me.cbo5.Text, _
                                  Me.txt6.Text, _
                                  Me.cbo7.Text, _
                                  Me.cbo8.Text, _
                                  Me.cbo9.Text, _
                                  Me.cbo10.Text, _
                                  Me.cbo11.Text, _
                                  Me.txt12.Text, _
                                  Me.txt13.Text, _
                                  Me.txt14.Text, _
                                  Me.txtID.Text)

                                  Try to keep the parameters in the same order that they appear in the query. OleDb tends to ignore the parameter names, and depends on the order of the parameters.


                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                  V 1 Reply Last reply
                                  0
                                  • Richard DeemingR Richard Deeming

                                    Because you're trying to update a field which you can't update. Don't include the ID column in the SET part of the query:

                                    DAL.ExecuteNonQuery("UPDATE SalesTable SET A1={0}, A2={1}, A3={2}, A4={3}, A5={4}, A6={5}, A7={6}, A8={7}, A9={8}, A10={9}, A11={10}, A12={11}, A13={12}, A14={13} WHERE [ID] = {14}", _
                                    Me.txt1.Text, _
                                    Me.dtp2.Text, _
                                    Me.cbo3.Text, _
                                    Me.txt4.Text, _
                                    Me.cbo5.Text, _
                                    Me.txt6.Text, _
                                    Me.cbo7.Text, _
                                    Me.cbo8.Text, _
                                    Me.cbo9.Text, _
                                    Me.cbo10.Text, _
                                    Me.cbo11.Text, _
                                    Me.txt12.Text, _
                                    Me.txt13.Text, _
                                    Me.txt14.Text, _
                                    Me.txtID.Text)

                                    Try to keep the parameters in the same order that they appear in the query. OleDb tends to ignore the parameter names, and depends on the order of the parameters.


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    V Offline
                                    V Offline
                                    VSLearner2013
                                    wrote on last edited by
                                    #17

                                    Thanks. But I have a doubt here... In my Database, The first field name is "ID" (Which should be 0) with a primary key and Autonumber. And here in the above code, it is after txt14.text. Is it okay?

                                    Richard DeemingR 1 Reply Last reply
                                    0
                                    • V VSLearner2013

                                      Thanks. But I have a doubt here... In my Database, The first field name is "ID" (Which should be 0) with a primary key and Autonumber. And here in the above code, it is after txt14.text. Is it okay?

                                      Richard DeemingR Offline
                                      Richard DeemingR Offline
                                      Richard Deeming
                                      wrote on last edited by
                                      #18

                                      Yes, because ID is the last parameter in the query:

                                      WHERE [ID] = {14}

                                      The number in the curly-brackets is the zero-based index of the parameter you've passed to the method. It has nothing to do with the column order in the table. As I said, OleDb tends to use positional parameters, so you need to pass the parameters to the method in the same order that they appear in the query. Unfortunately, this means that if you use the same value twice in a single query, you have to pass it to the method twice:

                                      ' This won't work for OleDb:
                                      DAL.Method("SELECT {0}, {1}, {0}", FirstValue, SecondValue)
                                      ' You'd have to use this instead:
                                      DAL.Method("SELECT {0}, {1}, {2}", FirstValue, SecondValue, FirstValue)


                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                      V 1 Reply Last reply
                                      0
                                      • Richard DeemingR Richard Deeming

                                        Yes, because ID is the last parameter in the query:

                                        WHERE [ID] = {14}

                                        The number in the curly-brackets is the zero-based index of the parameter you've passed to the method. It has nothing to do with the column order in the table. As I said, OleDb tends to use positional parameters, so you need to pass the parameters to the method in the same order that they appear in the query. Unfortunately, this means that if you use the same value twice in a single query, you have to pass it to the method twice:

                                        ' This won't work for OleDb:
                                        DAL.Method("SELECT {0}, {1}, {0}", FirstValue, SecondValue)
                                        ' You'd have to use this instead:
                                        DAL.Method("SELECT {0}, {1}, {2}", FirstValue, SecondValue, FirstValue)


                                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                        V Offline
                                        V Offline
                                        VSLearner2013
                                        wrote on last edited by
                                        #19

                                        Thanks a lot. It works fine.

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups