Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi dear coders my problem is connecting my vb.net project to the database tables in sql server so can any one help me please the code is ok and there is no syntax erros i declared the conection code in seperate class and wana use it from there have look at this ....this my code

The Declartion part in Class one
VB
Imports System.Data.SqlClient

Public Class Class1

    Public Shared sqlcon As New SqlConnection

End Class

and this is the code in form login
VB
Imports System.Data.SqlClient
Public Class frmLogin

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Class1.sqlcon.ConnectionString = "Data Source=TOSHIBA-PC\SQLEXPRESS;initial catalog=Employes;integrated security=true"
            MsgBox("تم الاتصال بنجاح", MsgBoxStyle.Exclamation)

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If txtID.Text = "" Then
            MsgBox("ادخل رقم المستخدم", MsgBoxStyle.Exclamation, "حقل إجباري")
            txtID.Focus()
            Exit Sub

        End If
        If txtpassword.Text = "" Then
            MsgBox("لابد من ادخال كلمة السر", MsgBoxStyle.Exclamation, "جقل إجباري")
            txtpassword.Focus()

            Exit Sub

        End If
        Try
            Dim str As String = "select ID,userPassword from  Users where ID='" & txtID.Text & "' and userPassword='" & txtpassword.Text & "'"
            Dim sda As New SqlDataAdapter(str, Class1.sqlcon)
            Dim ds As New DataSet
            sda.Fill(ds)
            If ds.Tables(0).Rows.Count > 0 Then
                Me.Hide()
                frmMain.Show()
            Else
                MsgBox("خطأ في البيانات المدخلة", MsgBoxStyle.Critical, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

End class

when ever i want to connect it to table login gives me this message
((((cannot open database employes requested by the login the login failed login failed for user 'Toshiba-PC\Toshiba'.))))))))
Posted
Updated 2-Mar-13 20:35pm
v2
Comments
Noor Ahmad Feroozi 3-Mar-13 4:57am    
Can you log into your SQL server.

1 solution

The error message "the login failed login failed for user" is pretty clear - your login fialed, so you cannot access any datbases.

Probably, your connection string is wrong.
Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
 
Share this answer
 
Comments
Maciej Los 3-Mar-13 5:51am    
5!
[no name] 3-Mar-13 7:41am    
thank you for ur repply mr permalink ...i found another connection code which is working.

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