question

KarwanKh-5392 avatar image
0 Votes"
KarwanKh-5392 asked JackJJun-MSFT commented

what is wrong with these codes ?

whenever I want to create a multi user loginform this error pops up and gives me this notification
(System.NullReferenceException: 'Object variable or With block variable not set.' )


Imports System.Data.SqlClient
Public Class Form1
Private Sub Btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
Dim con As New SqlConnection("Data Source=Karwan;Initial Catalog=login;Integrated Security=True")
Dim txtuser As Object = Nothing
Dim txtpass As Object = Nothing
Dim cmbusertype As Object = Nothing
Dim cmd As SqlCommand = New SqlCommand(" select * from login where username='" & txtuser.Text & "'and password='" & txtpass.Text & "' and usertype= '" & cmbusertype.SelectedItem & "'", con)
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
If (dt.Rows.Count > 0) Then
MessageBox.Show("چوونە ژوورەوە سەرکەوتوو بوو" + dt.Rows(0)(2))

         If (cmbusertype.selectedindex = 0) Then
             Dim a As New Admin
             a.Show()
             Me.Hide()
         Else
             Dim u As New User
             u.Show()
             Me.Hide()

         End If
     Else
         MessageBox.Show("زانیاریەکانت هەڵەیە")
     End If
 End Sub

End Class

windows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KarwanKh-5392 , have you tried the solution by Viorel-1? Please let me know if you have any question.

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

Probably you must remove these lines:

 Dim txtuser As Object = Nothing
 Dim txtpass As Object = Nothing
 Dim cmbusertype As Object = Nothing

Assuming that the form contains such controls. Or use the name of the corresponding controls.

Note that it is possible to enter a special password without knowing the real correct password. To solve this vulnerability, consider "Parameterised queries".



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.