Results 1 to 4 of 4

Thread: How to create a query on datagrid view in vb.net

  1. #1
    Join Date
    Nov 2009
    Posts
    39

    How to create a query on datagrid view in vb.net

    I want to create a query that selects few lines of table but I have a problem that I could do with the datagrid view, select all that. It said there was a total display of the table and once I add in the same Query datagrid view
    it gives me
    the shema returns by the new request is different from that of the base request
    Though I sought on msdn but I did not understand the stages to follow to create a datagrid view to a particular query! I have to create the dataset and tableadaptor! But is there anyone who can help me to understand exactly how!

    How to create a query on datagrid view in vb.net? I work with visual studio 2005 + sql server in an application windowsform

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: How to create a query on datagrid view in vb.net

    Hoping that I understand it properly, you must add a DataGridView on your form, then using a DataSet you must use as a data source of DataGridView. Here is some lines of code, you can insert it in the Form_Load.
    Code:
    Dim Dataset1 As New DataSet
    Dim Conn As New SqlClient.SqlConnection("My connection string")
    Dim Dadap As New SqlClient.SqlDataAdapter("Request sql", Conn)
    Try
        Dadap.Fill(Dataset1, "My table")
        DataGridView1.DataSource = Dataset1
    Catch ex As Exception
        MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK)
    End Try

  3. #3
    Join Date
    Nov 2009
    Posts
    39

    Re: How to create a query on datagrid view in vb.net

    Well I dropped the idea of working with the visual studio toolbox and I started to do this using the code. It is now my code but it does not works

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim connectString As String = "Server=ALI-582A65A96CB\SQLEXPRESS;Database=ONE;Trusted_Connection=True;"
            Dim connection As New System.Data.SqlClient.SqlConnection(connectString)
            connection.Open()
            Dim command As New System.Data.SqlClient.SqlCommand("SELECT * FROM myTable where  testbo='" & textBox1.text & "' ", connection)
            Dim dt As DataTable
            Dim adpt As New Data.SqlClient.SqlDataAdapter(command)
            Dim DataGridView1 As New DataGridView
            Dim ana As New DataSet
            Try
                adpt.Fill(dt)
                DataGridView1.DataSource = ana
                DataGridView1.Refresh()
                connection.Close()
            End Try
        End Sub
    End Class

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to create a query on datagrid view in vb.net

    Normally, I would have said no. Because "it does not work" is a bit lighter as a description of the problem.

    1. The DataTable dt is not initialized and is therefore null (Nothing in VB), which planted the call adpt.Fill
    2. The DataSet ana is not initialized either and is therefore null
    3. The table dt (nonexistent) is not the DataSet ana (which do not exist)
    4. The DataGridView that you use is not displayed on the form because you did not add. Moreover, needless to create the code, I guess you've already created with the designer.
    5. When you specify a DataSet as the DataSource, you must also indicate the name of the DataTable as DataMember. In case you did not give a name to your table, so you can not. The simplest is to direct the DataSource as DataTable, without using a DataSet
    6. Call for DataGridView1.Refresh () serves no purpose: firstly, it is not on the form, and secondly, the Refresh method is used only to refresh the display by forcing the control to redraw (Paint event). It does not alter the data that are taken into account for display, however this aspect is handled automatically, you can not force
    7. If you put the connection.Close () try the following in the code, it will not run if the Fill throws an exception, then find yourself with an open connection "lost" you shall have no way of finding it, it will therefore wait until the garbage collector destroys it or restart the application. To be sure that this instruction is executed, it must be placed in a Finally block

    On the other hand, it would be best to put this code in another method, "ChargerData" for example. It is better to give the method a meaningful name that indicates what it does (upload data) rather than when it is executed (at the click Button1). In this way the code is easily reusable in other places (at startup, when clicking on a menu, etc ...)

    In your final code should be something like this:

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            ChargerData()
        End Sub
        Private Sub ChargerData()     
            Dim connectString As String = My.Settings.StringConnection
            Dim connection As New System.Data.SqlClient.SqlConnection(connectString)
            Dim command As New System.Data.SqlClient.SqlCommand("SELECT * FROM myTable where  testbo='" & textBox1.text & "' ", connection)
            Dim dt As New DataTable
            Dim adpt As New Data.SqlClient.SqlDataAdapter(command)
            Try
                connection.Open()
                adpt.Fill(dt)
                DataGridView1.DataSource = dt
            Catch ex As SqlException
                MessageBox.Show("An error occurred while loading data !" & vbCrLf & ex.ToString()
            Finally
                connection.Close()
            End Try
        End Sub
    End Class

Similar Threads

  1. How to Create view based on table-join query
    By Preetish in forum Windows Software
    Replies: 3
    Last Post: 12-08-2009, 01:44 PM
  2. How to turn off View State of Datagrid
    By Preetish in forum Windows Software
    Replies: 3
    Last Post: 12-08-2009, 12:21 PM
  3. How to create loop query sql ?
    By Beltran in forum Software Development
    Replies: 2
    Last Post: 13-07-2009, 01:42 PM
  4. VB6 - Query against Textbox/Datagrid(ADO)
    By ProgrammerMike in forum Software Development
    Replies: 1
    Last Post: 13-06-2009, 08:47 AM
  5. How to create 7 days query in MySql
    By Patrickboy in forum Software Development
    Replies: 2
    Last Post: 18-05-2009, 02:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,996,464.22470 seconds with 17 queries