1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

The Type Initializer For 'system.data.sqlclient.sqlconnection' Threw An Exception.

Discussion in 'C#' started by alamlinks, Feb 2, 2013.

  1. #1
    Hello Friends,
    I am writing a method for connection named "connect" in vb.Net, the Connection String is OK. But giving exception. The Codes are below:



    Public Function Connect() As Integer
    con = New SqlClient.SqlConnection-------------------->
    Dim Servername As String
    Dim UserID As String
    Dim Password As String
    Dim Databasename As String
    Dim DB As Integer

    Environment.CurrentDirectory = Application.StartupPath
    Servername = ReadfromServerIni(Environment.CurrentDirectory & "\ServerConfig.ini", "ServerName", "Server")
    UserID = ReadfromServerIni(Environment.CurrentDirectory & "\ServerConfig.ini", "UserName", "ID")
    Password = ReadfromServerIni(Environment.CurrentDirectory & "\ServerConfig.ini", "UserName", "Pass")
    Databasename = ReadfromServerIni(Environment.CurrentDirectory & "\ServerConfig.ini", "DatabaseName", "Name")

    con.ConnectionString = ("Data source=" & Servername & ";uid=" & UserID & ";pwd=" & Password & ";Initial Catalog=" & Databasename & ";TimeOut=500;") ' Through Local Conecctivity

    Try
    If DB = 1 Then
    con.Open()
    Else
    IsRestoreDB = MsgBox("Database not Found! " & vbNewLine & "Would you like to restore the database.", MsgBoxStyle.YesNo)
    If IsRestoreDB = 6 Then
    frmDatabaseRestore.Show()
    Else
    IsRestoreDB = 0
    End
    End If
    End If
    Catch ex As Exception
    MsgBox("A Problem occured due to make connection with Database", MsgBoxStyle.Information)
    End Try
    End Function

    Connect String is below:


    <connectionStrings>
    <addname="NGO.My.MySettings.NGOConnectionString"connectionString="Data Source=Noor-PC\SQLExpress;Initial Catalog=NGO;User ID=sa;Password=noor123"providerName="System.Data.SqlClient"/>
    </connectionStrings>

    Please help me if any knowledge about it.

    Thanks & Regards
    SEMrush
     
    alamlinks, Feb 2, 2013 IP
    SEMrush
  2. kajakske

    kajakske Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    150
    #2
    Use the example code from MSDN:

    Using connection As New SqlConnection(connectionString)
    Dim command As New SqlCommand(queryString, connection)
    command.Connection.Open()
    command.ExecuteNonQuery()
    End Using
     
    kajakske, Feb 5, 2013 IP