Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.NET
Imports System.Data.SqlClient
Imports System.Linq
Imports System.Data




Public Class Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim rd As SqlDataReader
        conn.ConnectionString = "Data Source=localhost;Initial Catalog=user;Integrated security=true"
        cmd.Connection = conn
        conn.Open()
        cmd.CommandText = "Select login,password from user where login='" & TextBox1.Text & "'and password='" & TextBox2.Text & "'"
        rd = cmd.ExecuteReader
        If rd.HasRows Then
            Welcome.Show()
        Else
            MsgBox("Invalid Login or Password")
        End If

    End Sub
End Class
Posted
Comments
Kornfeld Eliyahu Peter 24-Jan-16 5:37am    
Use debugger to find what is null, than decide what to do with that...
Member 11910983 24-Jan-16 5:39am    
it is showing the error at connection string parameters "integrated security=true"

You declared conn and cmd as variables but you didn't initialize them with instances of SqlConnection / SqlCommand. You need to either create an instance with the keyword New or use the ProviderFactory[^]-class to get an instance of them.

The latter (using the ProviderFactory) is recommended because then, if you also use either the abstract Db******-base-classes (like DbConnection, DbCommand, ..) or the interfaces (IDbConnection, IDbCommand, ..) instead of the concrete and SQL-Server-dependent classes (SqlConnection, SqlCommand, ..) you'll make your code independent of a concrete database system and will be able to change from SQL Server to some other DB system easily or at least more easily.

Also, you shouldn't use string concatenation for building SQL-statements. Your code becomes susceptible to SQL-injection and less readable. Use Parameters instead: SqlCommand.Parameters Property (System.Data.SqlClient)[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 24-Jan-16 14:15pm    
5ed.
—SA
After the Dim conn As SqlConnection statement, conn is still null (same for cmd and rd). You need to give conn and cmd a value by calling their constructor.
VB.NET
Dim conn As New SqlConnection()
Dim cmd As New SqlCommand()

Note: you do not have to do this for rd, because SqlDataReader has no constructor and you get the reader from ExecuteReader anyway.
 
Share this answer
 
Comments
Member 11910983 24-Jan-16 6:14am    
now i'm getting a new exception "SQLException" .But, i've already added a connection to the database
Thomas Daniels 24-Jan-16 6:15am    
What's the full exception message? Note that this exception is actually not related to the NullReferenceException but to the ways you communicate with the database. I'll take a look at the error, but I cannot promise that I can find it.
Member 11910983 24-Jan-16 6:19am    
Exception message:
Cannot open the database "user" requested by the login.The login failed.
Thomas Daniels 24-Jan-16 6:22am    
Looks like you have to fix your connection string.

Also (unrelated), I saw in your code that you use string concatenation to create queries. Don't do this. You are vulnerable to SQL Injection. Use parameterized queries instead: http://www.dotnetperls.com/sqlparameter.
Sergey Alexandrovich Kryukov 24-Jan-16 14:15pm    
5ed.
—SA
You have to start initializing your objects...
VB.NET
Dim conn As SqlConnection

This line creates a 'placeholder' for an object of type SqlConnection, but no real object created so the 'placeholder' is empty - null!!!
VB.NET
Dim conn As New SqlConnection

For more info: SqlConnection Constructor (String) (System.Data.SqlClient)[^]
 
Share this answer
 

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