|
| |||||||||
| Tags: datagrid view, query, sql server, vbnet, visual studio |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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 Quote:
How to create a query on datagrid view in vb.net? I work with visual studio 2005 + sql server in an application windowsform |
|
#2
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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 |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to create a query on datagrid view in vb.net" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to Create view based on table-join query | Preetish | Windows Software | 3 | 12-08-2009 02:44 PM |
| How to turn off View State of Datagrid | Preetish | Windows Software | 3 | 12-08-2009 01:21 PM |
| How to create loop query sql ? | Beltran | Software Development | 2 | 13-07-2009 02:42 PM |
| VB6 - Query against Textbox/Datagrid(ADO) | ProgrammerMike | Software Development | 1 | 13-06-2009 09:47 AM |
| How to create 7 days query in MySql | Patrickboy | Software Development | 2 | 18-05-2009 03:02 PM |