Как использовать многократную проверку существующих данных ?
Ну я сделал страницу регистрации для факультетов с помощью VB.net и SQL 2008 express. Где пользователи должны ввести свои данные для регистрации. Но я пытаюсь проверить наличие существующих данных перед вставкой в базу данных SQL.
Но я получаю ошибку. Ошибка: "DataReader уже открыт с помощью команды, которая должна быть закрыта первой".
Я добавляю код. Пожалуйста, проверьте и поправьте меня.
Спасибо codeproject.
Что я уже пробовал:
Try Try Try OpenConnection() query = "SELECT * from [faculty] WHERE id='" & TextBox1.Text & "'" command = New SqlCommand(query, connection) reader = command.ExecuteReader If reader.HasRows Then Label25.Text = "* Badge ID already exists, try another." Label25.ForeColor = Color.Red Else Label25.Text = "* Mendatory fields cannot be left blank." Label25.ForeColor = Color.Black End If connection.Close() Catch ex As Exception MsgBox(ex.Message) Finally command.Dispose() End Try OpenConnection() query = "SELECT * from [faculty] WHERE username='" & TextBox10.Text & "'" command = New SqlCommand(query, connection) reader = command.ExecuteReader If reader.HasRows Then Label25.Text = "* Username already registered, try another." Label25.ForeColor = Color.Red Else Label25.Text = "* Mendatory fields cannot be left blank." Label25.ForeColor = Color.Black End If connection.Close() Catch ex As Exception MsgBox(ex.Message) Finally command.Dispose() End Try Try OpenConnection() Dim defText As String = "Not Modified".ToString Dim ms As New System.IO.MemoryStream Dim command As New SqlCommand("INSERT into [faculty](id,name,dob,sex,addr,city,state,pin,country,nat,mobile,email,dept,sub,username,password,security,answer,avatar,authentic,created,modified)values(@id,@name,@dob,@sex,@addr,@city,@state,@pin,@country,@nat,@mobile,@email,@dept,@sub,@username,@password,@security,@answer,@avatar,@authentic,@created,@modified)", connection) PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat) command.Parameters.Add("@id", SqlDbType.VarChar).Value = TextBox1.Text command.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox2.Text command.Parameters.Add("@dob", SqlDbType.Date).Value = DateTimePicker1.Value.Date Dim gender As String Dim value As Integer gender = ComboBox1.Text If gender = "Male" Then value = 1 ElseIf gender = "Female" Then value = 2 ElseIf gender = "Others" Then value = 3 End If command.Parameters.Add("@sex", SqlDbType.Int).Value = value command.Parameters.Add("@addr", SqlDbType.VarChar).Value = TextBox3.Text command.Parameters.Add("@city", SqlDbType.VarChar).Value = TextBox4.Text command.Parameters.Add("@state", SqlDbType.VarChar).Value = TextBox5.Text command.Parameters.Add("@pin", SqlDbType.Int).Value = TextBox14.Text command.Parameters.Add("@country", SqlDbType.VarChar).Value = TextBox6.Text command.Parameters.Add("@nat", SqlDbType.VarChar).Value = TextBox7.Text command.Parameters.Add("@mobile", SqlDbType.VarChar).Value = TextBox8.Text command.Parameters.Add("@email", SqlDbType.VarChar).Value = TextBox9.Text command.Parameters.Add("@dept", SqlDbType.VarChar).Value = ComboBox3.Text command.Parameters.Add("@sub", SqlDbType.VarChar).Value = ComboBox4.Text command.Parameters.Add("@username", SqlDbType.VarChar).Value = TextBox10.Text command.Parameters.AddWithValue("@password", Encrypt(TextBox11.Text.Trim())) Dim question As String Dim data As Integer question = ComboBox2.Text If question = "Whats your nickname ?" Then data = 5 ElseIf question = "Whats your pet name ?" Then data = 4 ElseIf question = "Whats your birth place ?" Then data = 3 ElseIf question = "Whats your middle name ?" Then data = 2 ElseIf question = "Whos your childhood hero ?" Then data = 1 End If command.Parameters.Add("@security", SqlDbType.Int).Value = data command.Parameters.AddWithValue("@answer", Encrypt(TextBox13.Text.Trim())) command.Parameters.Add("@created", SqlDbType.VarChar).Value = Date.Today command.Parameters.Add("@modified", SqlDbType.VarChar).Value = defText command.Parameters.Add("@avatar", SqlDbType.Image).Value = ms.ToArray command.Parameters.Add("@authentic", SqlDbType.Int).Value = 1 If command.ExecuteNonQuery() = 1 Then MsgBox("A new faculty has been successfully registered.") Else MsgBox("We're unable to accept the registration. Please check the form that you've submitted.") End If connection.Close() Catch ex As Exception Label25.Text = ("* Profile image cannot be left blank.") Finally command.Dispose() End Try Catch ex As Exception MsgBox(ex.Message) End Try