VB.NET,winform- difficulties in datagridview

i was using .net1.1.. now i am using .net2.0 and sqlserver2005
in my form i have a datagrid and i want view the contents of contents of table how write the code in vb.net 2.0.. i tried the following code but its not working
plz help me

Imports System.Data.SqlClient
Public Class Form1

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

Dim con As New SqlConnection("Data Source=localhost; Initial Catalog=N0C7G6Databasescompany; Integrated Security=SSPI")
Dim ad As New SqlDataAdapter("select * from emp", con)
Dim ds As New DataSet
DataGridView1.DataSource = ds
End Sub


  • seancampbellseancampbell Pennsylvania, USA
    Whats it saying is wrong with that code?

    One thing I noticed is that your recreating a connection and apparently rebinding the dataview on the Cell_Click event. Maybe you want it to create a connection and refresh on Load, so it only does it once. Or maybe on a refresh command that you call from a button or menu item.

    Another thing I thought you could do is add a Statement like this:

    If Con.ConnectionState = ConnectionState.Closed Then 'I am writing this without Visual studio so it may be a bit different syntax


    End If

    The Stop command is like adding a break line in your code. It will stop the execution of the program and allow you to look at variables in there current state inside the program. More importantly, it can be called inside a condition so your program won't pause in all cases, just in the ones you specify. I have found that debugging line for line is a great help in solving issues.

    Post some more info, such as an error message or syntax message on whats causing that not to work. At first glance I would suspect your connection is not getting established, but that is just because I use a connection string that looks different from yours. Also, what version of .Net (or VS) are you using?

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion