Question Locked

How to use Datagrid in VB.net

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello,
I am about to configure the MySQL Records to the VB.net form having DataGrid.
I am using Microsoft Visual Basic 2008 Express Edition; Database- MySQL 5.0; Form1 having DataGrid in it.
Can anybody provide me detailed Step by Step procedure to get MySQL Database output in the VB.net DataGrid from beginning to end??

Any suggestion or guidance will be helpful.
Thanks in advance.

Rashid

11 replies

by jiminpa

Do you have any code to work with? What your asking is pretty simple straight forward stuff but we can't write all your code for you. Conceptually you would need to place the datagrid on your page, in an event handler write a connection string, a query, and bind it to the datagrid.

For connectionstrings look here.

http://www.connectionstrings.com/?carrier=mysql

If you found this post useful please remember to mark as helpful so that others in the community may benefit from the solution.

by mrashidkhan

Thanks a lot Jimmy.
The link that you have provided is helpful for me.
As i am beginner for using Datagrid in VB.net can you please provide me some set of coding so that i can use it in my test-project.
I know it is a bigger one but i need some detail. Please help.

Thanks again.

Rashid

by jiminpa

Are you connecting directly or using an ODBC connection?

by shellbot

The datagrid is just another control..
don't mean to sound mean here, but your asking for someone to provide you with the code to do your project for you.

The more sensible way to help is to step out what you've done and where you are stuck.

Most of it is simple point and click..link in your server, set up a connection to the database and link your datagrid into it..set the properties yadayada..its not really too hard IF you know a bit of VB.net and how to use the IDE.

If you use VB.NET help it will tell you a good bit about it, Google it..you will find a lot of info on how to do it.

After you attempt it, then get back to us and tell us whats happening, and let us see your code and we can then help..

We were all begginers once, we do understand..but simply asking us to do you work for you is not on.

by mrashidkhan

I am connecting through MySQL by VB Express Edition and it got Connected Successfully.

Please see following test-code of my task and help me.

Imports System.Data.SqlClient
Imports System.Data
Public Class Form1
Private myConnString As String
Public WriteOnly Property connectionString() As String
Set(ByVal value As String)
myConnString = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myConnString As String
Dim conn As New SqlClient.SqlConnection
myConnString = "server=localhost;user=root;password=ra45;database=aurum"
conn.ConnectionString = myConnString
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
Dim myAdapter As New SqlClient.SqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
SQL = "SELECT login_id,psswd FROM login"
conn.ConnectionString = myConnString
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData) <-- {{{At this point i have error as "The ConnectionString property has not been initialized."}}}
DataGridView1.DataSource = myData
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
End Sub
End Class

Thanx for your support and sorry for ur time flush.

Rashid

by shellbot

-->Start Your code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
Dim myAdapter As New SqlClient.SqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
SQL = "SELECT login_id,psswd FROM login"
conn.ConnectionString = myConnString
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData) <-- {{{At this point i have error as "The ConnectionString property has not been initialized."}}}
DataGridView1.DataSource = myData
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
End Sub
End Class
-->End your code.

Now, my .NET is a bit rusty..haven't used it in a bit..try opening your connection.
So after this line
--> conn.ConnectionString = myConnString
add:
conn.open()

then after this line:
--> DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

add:
conn.close()

You MUST close your connection when your done with it, or you will end having problems further along.

Let me know if that does anything, or what errors you get.

by mrashidkhan

i have added conn.open() and conn.close() at the place and now at run time error comes at conn.open() as ----"The ConnectionString property has not been initialized."

Remember: I am going to connect application with "MySQL 3.51" from the Visual Studio Express Edition connection tool {which comes at top of datagridview to connect with Database}

Thanks for your support.

Rashid

by shellbot

oops..your using MySQL, so you can't use SQL Client.

so you need to use:
system.data.odbc.odbcconnection

and need to use the command and client classes from the system.data.odbc namespace.

You will probably have to provide the driver/provider in the connection string.

Might be easier to get working initially using a DSN?

Go here and see how they are doing it and play around with it.
www.connectionstrings.com/?carrier=mysql

by mrashidkhan

Thanks a lot Shell.
I have connected successfully with MySQL and the datagrid provides me the number of Rows that i have in Database.
But all the Columns and Rows are empty in the datagrid {Means without any data}

After connection i have following codes to insert query output into DataGrid:-
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
DataGridView1.DataSource = myData
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
conn.Close()

Please help me on this.
Thanks a lot
Rashid

by shellbot

you need to find out at what point this is failing.

Put a breakpoint in at your Fill and then debug and go examine to see if anything is there.

Unfortunatly I don't have mySQL, so not sure if there are any other quirky bits there to deal with.

This discussion is closed to new replies.