Hello everyone.I am new to vb.net 2008 and i am in trouble.The problem for me is that i am making a windows form for a company and i am in a mess in getting my database connection to my datagridview by clicking a button and my data should been shown in datagridview.pls give me suggestion about datasets and connection strings.and even i want to print the document.so pls help me out.

what database you are using then?

Connection String Sample :

here for access : Access
here for sql : Sql
here for MySql : MySql

in ButtonClick Event do this :

Try
            Using conn As New MySqlConnection("your string connection")
                conn.Open()
                Dim adapter As New MySqlDataAdapter("select * from table_name", conn)
                Dim dt As New DataTable
                adapter.Fill(dt)
                DataGridView1.DataSource = dt
                adapter.Dispose()
                conn.Close()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

i use MySql for sample.

@artemix22 my database is an access file.See i am having two windows form i.e 1st is Login Page and 2nd is my Document Page.Now in my doucment page i have three buttons and a datagridview.now 1st button is (Browse),2nd is Print and 3rd is cancel.now main concern is that i cannot open or get my connection string using a dataset in datagridview.and one more thing...i don't want to open my database on form_load_events.so help me with this also.

call this code on ur button click to display ur records in datagridview

'open connection
Try
   Dim myCommand As New OleDbCommand
   With myCommand
        .CommandText = "SELECT * FROM tablename"
        .CommandType = CommandType.Text
        .Connection = Connection
   End With
   Dim dt As New DataTable
   dt.Load(myCommand.ExecuteReader)
   With DatagridView1
        .AutoGenerateColumns = True
        .DataSource = dt
   End With
Catch ex As Exception
   MsgBox("Error in select query: " + ex.Message)
End Try
'close connection

well thank you @poojavb for helping me.i will try this code and let u know my reply.and one more thing,my database is opening directly on form_load events.so how can i stop that.

Instead of opening ur database in form load event try to open it in ur click events....

Did you salve the problem yet?

Btw:

and i am in a mess in getting my database connection to my datagridview

What should that suppose to mean exaclty?
Do you have problems with connection string, or with getting data from database to dgv?

Mark thread as solved if its done....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.