0

Hello , i kinda need some help here.

The below code is to check if a category already exists before adding it to the table category.
If ever i insert a category that already exists, if gives me the message that it already exists.
The thing is that if it does not exists, it is not executing the insert query.
Can anyone help please

        Dim Constr As String = WebConfigurationManager.ConnectionStrings("2k17_Connection").ConnectionString
        Dim connect As New SqlConnection
        Dim cmd As New SqlCommand
        Dim txtcontent As String
        txtcontent = Me.txtCategory.Text
        Dim x As Integer
        Dim query1 As String
        Dim query2 As String
        query1 = "Select * from tblcategory where categoryname='" & txtcontent & "'"
        query2 = "insert into tblcategory values('" & txtCategory.Text & "','" & txtDescription.Text & "')"

            connect.ConnectionString = Constr

        connect.Open()
            cmd = New SqlCommand(query1, connect)
            Dim rdr As SqlDataReader = cmd.ExecuteReader
            If rdr.Read() Then

                Dim category As String = rdr("categoryname")

                If txtcontent = category Then
                    lblResult.Text = "The category" & txtcontent & "already exists"
                txtCategory.Text = ""
                txtDescription.Text = ""
                txtCategory.Focus()
            ElseIf txtcontent <> category Then
                cmd = New SqlCommand(query2, connect)
                x = cmd.ExecuteNonQuery
                lblResult.Text = "New category added"
            End If
            End If
        rdr.Close()

        connect.Close()

i also tried to modify the

 ElseIf txtcontent <> category Then
                    cmd = New SqlCommand(query2, connect)
                    x = cmd.ExecuteNonQuery
                    lblResult.Text = "New category added"

by the following but it still doesnt work

 Else
                    cmd = New SqlCommand(query2, connect)
                    x = cmd.ExecuteNonQuery
                    lblResult.Text = "New category added"

Edited by Alv45525

2
Contributors
4
Replies
23
Views
3 Months
Discussion Span
Last Post by Alv45525
1

I hope this helps:

            Dim rdr As SqlDataReader = cmd.ExecuteReader
            Dim bFound As Boolean = False
            If rdr.Read() Then
                Dim category As String = rdr("categoryname")
                If txtcontent = category Then
                    bFound = True
                End If
            End If
            rdr.Close()
            If Not bFound Then
                cmd = New SqlCommand(query2, connect)
                x = cmd.ExecuteNonQuery
                lblResult.Text = "New category added"
            End If
0

hi xrj,

it works perfectly thanks. However i used a count in my SQL and i got it working too.
Thank you for your precious help though.

God bless.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.