none
Perform SQL Command from Datagridview data RRS feed

  • Question

  • Hello,

    I'm using VB.net 2017 and MS SQL server 2008

    in my app i have a Temporary Table that get filled every time i use it

    What i need is :

    1- use the texts in my Datagridview  as Parameters to  Execute SQL Commands that created by Query builder in VB.net

    e.g. Commanded made by vb.net for Table "Temp_A" which have 3 Columns

    Temp_ATableAdapter.UpdateDouble(Core.Text, Quantity.Text, RT.Text)

    so technically i need to know how to use  Datagridview 's Text in first row first column as Parameters  instead of Core.Text for example

    2- I need to Execute SQL Commanded by the number of rows which Datagridview have

    e.g. Datagridview have 4 rows so i need to Execute some commands that  is repeated 4 time (for some reason i think that will involve For Next or For Each loops)

    Thanks in advance.

    Sunday, June 9, 2019 6:25 AM

Answers

  • The best you may be able to do is look at a C# example, becuase it's hard to find examples in VB.NET.

    I can tell you that a grid has events, and a event is how you address the grid's row and cell value to get or manipulate the grid.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview?view=netframework-4.8#events

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/selected-cells-rows-and-columns-datagridview

    So what I would say to you is look for C# examples using the datagridview control unbound. And if you find something you like, then copy the code into a C# to VB converter and convert it over to VB and look at it in VB and use the code if possible.

    • Marked as answer by AbanoubZak Saturday, June 22, 2019 9:05 AM
    Sunday, June 9, 2019 11:50 PM

All replies

  • The tutorial shows how to do it.

    http://vb.net-informations.com/datagridview/vb.net_datagridview_database.htm

    Sunday, June 9, 2019 10:37 AM
  • The tutorial shows how to do it.

    http://vb.net-informations.com/datagridview/vb.net_datagridview_database.htm

    Thanks for your reply.

    I meant I need to use the data grid view to Execute Commands on another table not the Bending source 

    I have 4 Tables in my DB also in my Data set in vb.net and i use the Table Adapter and Table Manger I need to Insert Delete and Update Data in each Table by the data in my Datagridview.

    I'm trying to avoid the old way to create the "Command" manually and "open connection"

    like the following (Just e.g. not my work an not the way I need to learn)

     Dim sCommand As SqlCommand
        Dim sAdapter As SqlDataAdapter
        Dim sBuilder As SqlCommandBuilder
        Dim sDs As DataSet
        Dim sTable As DataTable
    
        Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
            Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
            Dim sql As String = "SELECT * FROM Stores"
            Dim connection As New SqlConnection(connectionString)
            connection.Open()
            sCommand = New SqlCommand(sql, connection)
            sAdapter = New SqlDataAdapter(sCommand)
            sBuilder = New SqlCommandBuilder(sAdapter)
            sDs = New DataSet()
            sAdapter.Fill(sDs, "Stores")
            sTable = sDs.Tables("Stores")
            connection.Close()
            DataGridView1.DataSource = sDs.Tables("Stores")
            DataGridView1.ReadOnly = True
            save_btn.Enabled = False
            DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    
        End Sub

    and use the new commands I created like for e.g.

    and the commanded be like :

    If Username.Text.Length > 0 And Password.Text.Length > 0 Then
    
                If LoginUsersTableAdapter.logincheck(Username.Text, Password.Text) IsNot Nothing Then
    
                    Me.Hide()
                    Main.Show()
    
                    Try
                        Main.LoginUsersTableAdapter.FillByLoggedin(Main.AZFTSACCDataSet.LoginUsers, Username.Text)
                    Catch ex As Exception
                        'MessageBox.Show(ex.ToString)
                    End Try
    
                    Main.BringToFront()
                    MessageBox.Show("Login sucessfully")
    
                Else
                    MessageBox.Show("Invalid User or Password!")
                End If
    
            End If


    • Edited by AbanoubZak Sunday, June 9, 2019 5:34 PM
    Sunday, June 9, 2019 5:32 PM
  • I have 4 Tables in my DB also in my Data set in vb.net and i use the Table Adapter and Table Manger I need to Insert Delete and Update Data in each Table by the data in my Datagridview.

    Myself personally, I have not used some of the technology you are talking about in decades, like dataset and datatable nor have I ever used a table manger.

    I am more into using ADO.NET, MS SQL Server Command objects, parametrized inline T-SQL a data reader,  custom objects in a collection and doing things that way, or using an ORM like the ADO.NET Entity Framework. 

    So I can't help you with the technology you're wanting to use.

    Sunday, June 9, 2019 10:05 PM
  • I have 4 Tables in my DB also in my Data set in vb.net and i use the Table Adapter and Table Manger I need to Insert Delete and Update Data in each Table by the data in my Datagridview.

    Myself personally, I have not used some of the technology you are talking about in decades, like dataset and datatable nor have I ever used a table manger.

    I am more into using ADO.NET, MS SQL Server Command objects, parametrized inline T-SQL a data reader,  custom objects in a collection and doing things that way, or using an ORM like the ADO.NET Entity Framework. 

    So I can't help you with the technology you're wanting to use.

    I really appreciation your efforts to help me, thank you.

    I just don't know why there is no enough information on the Internet about this way.

    By the way if you have any idea on how to use the Data or Value displayed on my Datagridview  like the line code for it (e.g. to get that value in the first column first row) will be much appreciated. 

    Sunday, June 9, 2019 10:45 PM
  • The best you may be able to do is look at a C# example, becuase it's hard to find examples in VB.NET.

    I can tell you that a grid has events, and a event is how you address the grid's row and cell value to get or manipulate the grid.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview?view=netframework-4.8#events

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/selected-cells-rows-and-columns-datagridview

    So what I would say to you is look for C# examples using the datagridview control unbound. And if you find something you like, then copy the code into a C# to VB converter and convert it over to VB and look at it in VB and use the code if possible.

    • Marked as answer by AbanoubZak Saturday, June 22, 2019 9:05 AM
    Sunday, June 9, 2019 11:50 PM