Something is amiss with my connection string
I've been using a SQLCE database for some of my smaller projects, but decided to use a normal SQL database for a new project that is going to contain a large amount of information.
Since I realize that the connection strings for a SQLCE database and a SQL database, I decided to go to connectionstrings.com to find out what needs to be used for SQL.
I found this:
SQL Code:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Although it does give me the proper string, it doesn't really give a good explanation of how to apply it.
Originally, I tried setting the Data Source to the location of my database, which is local for now. Then I set the Initial Catalog to my database name. There isn't a password or user ID for the database so I didn't include those:
VB.NET Code:
Dim connection As New SqlConnection("Server=C:\Users\Ernest\Desktop\; Data Source=dbCredentials.mdf;")
Any ideas on what I'm doing wrong?
Thanks
Re: Something is amiss with my connection string
I didn't realize that VS automatically created connection string for me. It turns out that it is this:
VB.NET Code:
Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ernest\Desktop\dbCredentials.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Now that the connection error is gone, I have another error. The code below is being used from a post JMC made a little while back to validate login credentials:
vb Code:
Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ernest\Desktop\dbCredentials.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Dim command As New SqlCommand("SELECT COUNT(*) FROM Users WHERE UserID = @UserID AND Password = @Password", _
connection)
With command.Parameters
.AddWithValue("@UserID", Me.txtUserName)
.AddWithValue("@Password", Me.txtPasssword)
End With
connection.Open()
If CInt(command.ExecuteScalar()) = 0 Then
MessageBox.Show("Invalid credentials!", "ISTme! Login", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Me.DialogResult = Windows.Forms.DialogResult.OK
End If
connection.Close()
When it gets to the ExecuteScalar line, it throws an exception:
Quote:
No mapping exists from object type System.Windows.Forms.TextBox to a known managed provider native type.
Does this mean I have to save the credentials to a variable first and then pass it as a a parameter?
I don't have time to check it out at the moment.
Any information is appreciated.
Thanks
Re: Something is amiss with my connection string
I feel silly. I assigned the TextBox to the Username/Password, but not the text property.