Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to connect sql server through app.config with vb.net. I don't know why it is not connecting. My code seems ok.

app.config as follows
XML
<connectionStrings>
  <add 
        name = "DBCS"
        connectionString="Data Source=USER1-PC\sqlexpress;Initial Catalog=Test;Integrated Security=True"
        providerName="System.Data.SqlClient" />
< /connectionStrings >


my form as follows,
VB
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data.Sql

Public Class Form1
  Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("DBCS").ConnectionString)
  
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Try
      con.Open()

      Dim cmd As String
      cmd = "select * from tblemp"

      Dim da As New SqlDataAdapter(cmd, con)
      Dim ds As New DataSet

      da.Fill(ds, "tblemp")
      con.Close()
      DataGridView1.DataSource = ds.Tables("tblemp")

    Catch ex As Exception
      MessageBox.Show(ex.Message)
    End Try
  End Sub
End Class
Posted
Updated 9-Jun-15 21:43pm
v4
Comments
Kornfeld Eliyahu Peter 9-Jun-15 5:44am    
Any errors?
Member 11749706 9-Jun-15 14:02pm    
please see error message below and kindly help me
ZurdoDev 9-Jun-15 8:04am    
The error will tell you the problem exactly.
Member 11749706 9-Jun-15 14:02pm    
pls see the error message below
Member 11749706 9-Jun-15 8:04am    
yes ..error which marked on "con"... use the new keyword to create an object instance

and immediate window shows...
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
A first chance exception of type 'System.NullReferenceException' occurred in codeprojects.exe
A first chance exception of type 'System.NullReferenceException' occurred in codeprojects.exe

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900