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"

You should close first rdr.close() before the insert into command.

tried that too but still doesnt work :(

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.