Click here to Skip to main content
15,997,776 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am unable to to connect to the SQL SERVER database.

The machine setup:
Windows 7
VS2005 Standard
SQLEXPRESS 2005

My code :

Imports System.Data
Imports System.Data.SqlClient
Public Class Dosen
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim reader As SqlDataReader
    Dim constring As String
    Dim SqlString As String
    Dim numAffected As Integer

    Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Clic
        SqlString = "Insert Into Dosen values ('" & id.Text.Trim & "','" & nama.Text.Trim & ")"
        cmd = New SqlCommand(SqlString, con)
        numAffected = cmd.ExecuteNonQuery
        con.Close()
        MessageBox.Show("Data dosen berhasil disimpan", "SUKSES", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub Dosen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the '_FKIP_UNILAKDataSet.Dosen' table. You can move, or remove it, as needed.
        Me.DosenTableAdapter.Fill(Me._FKIP_UNILAKDataSet.Dosen)
        constring = "Data source=ragaz-pc\sqlexpress.FKIP-UNILAK.dbo;Initial catalog=FKIP-UNILAK; Integrated security=true"
        con = New SqlConnection(constring)
        con.Open()
    End Sub
End Class

The error message was :
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

[edit]Tags, Code block added, "Ignore HTML in text" option disabled - OriginalGriff[/edit]
Posted
Updated 20-Feb-11 3:12am
v2

Try this

VB
Private Sub page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cn = New OleDbConnection("your connection string")
        cn.Open()
        cmd = New OleDbCommand("Select * from tablename", cn)
        cmd.ExecuteNonQuery()
        adp = New OleDbDataAdapter(cmd)
        adp.Fill(ds, "tablename")
    End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnins.Click
     cmd = New OleDbCommand("insert into tablename values" & "(" & TextBox1.Text & "," & "'" & TextBox2.Text & "'" & "," & "'" & TextBox3.Text & "'" & "," & TextBox4.Text & "," & TextBox5.Text & ")", cn)
cmd.ExecuteNonQuery()
End Sub
 
Share this answer
 
VB
Dim tbl As New DataTable
Private dvw As DataView
Private b As Boolean
Private SqlCon As New System.Data.SqlClient.SqlConnection("server=Morteza-PC\sql2005;user id=sa;pwd=sqlmaster;data source=Morteza-PC\sql2005;database=ParsContract")
Private cmd As New SqlClient.SqlCommand("SELECT * FROM Personalinfo", SQL.sqlcon)

SQL.sqlcon.Open()
Dim sdr As SqlClient.SqlDataReader = cmd.ExecuteReader
Dim fc As Integer
While (sdr.Read)
    'populating columns
    If Not b Then
        For fc = 0 To sdr.FieldCount - 1
            tbl.Columns.Add(sdr.GetName(fc))
        Next
        b = True
    End If
    'populating rows
    Dim row As DataRow = tbl.NewRow
    For fc = 0 To sdr.FieldCount - 1
        row(fc) = sdr(fc)
    Next
    tbl.Rows.Add(row)
End While
dvw = New DataView(tbl)
Me.dgrPersonalInfo.DataSource = dvw
SQL.sqlcon.Close()
 
Share this answer
 
It might be surface area configuration error or sql service configuration issue.
Have a look at this thread for potential reasons and resolutions: Resolving A network-related or instance-specific error occurred while establishing a connection to SQL Server[^]

If needed, See steps for setting up Surface area configuration here..

If this does not resolve your issue, have a look at these thread:
http://forums.asp.net/t/1334649.aspx[^]
SQL SERVER FIX ERROR (provider: Named Pipes [^]
Quite few possible reasons and resolutions are proposed here too.
 
Share this answer
 
Comments
Espen Harlinn 20-Feb-11 17:03pm    
Good advice :)
Abdi tombang 20-Feb-11 21:34pm    
Thanks a lot for help :D
I'll give you 5 rating
Firstly, don't do your connection that way: Create and open a new connection when you need it, close and dispose the connection immediately you have finished. Connections are valuable and scarce resources, which should not be tied up waiting for your user to press a button!

Secondly, check your connection string: I don't think you need the ".FKIP-UNILAK.dbo" in the data source. Look in the Server explorer in VS - right click the DB and select "properties". The details you need should be at the top of the Properties pane under "Connection String"
 
Share this answer
 
Comments
Espen Harlinn 20-Feb-11 9:21am    
Good reply :)
Abdi tombang 20-Feb-11 21:34pm    
Thanks a lot for help :D
I'll give you 5 rating

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