In a Class change console message for textbox message yes or no, VB2017 Vb.Net

MannySo 25 Reputation points
2025-01-18T03:51:01.44+00:00

I want to delete a table in a database if it exists, with a textbox (yes or no) to create a new one programmatically. If the table exists "delete it" with the condition (yes), if this table does no exists with option no create a new one. For now I have the message trough Console but, i want to pop up the message into a textbox (yes or no). I need an example code please. If the table does not exists create a new one. Thank you in advance.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,067 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,768 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 33,196 Reputation points Microsoft Vendor
    2025-01-20T02:16:39.83+00:00

    Hi @MannySo ,

    Not sure if I understood your question correctly, if there is a misunderstanding please point it out.

    Delete Table: If the table exists and the user input is "yes", the table is deleted.

    Create Table: If the table does not exist and the user input is "no", a new table is created.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim connectionString As String = "Your_Connection_String_Here"
            Dim tableName As String = "YourTableName"
            Dim userInput As String = TextBox1.Text.ToLower()
    
            Using connection As New SqlConnection(connectionString)
                connection.Open()
    
                Dim tableExists As Boolean = False
                Dim checkTableQuery As String = $"IF OBJECT_ID('{tableName}', 'U') IS NOT NULL SELECT 1 ELSE SELECT 0"
                Using checkCommand As New SqlCommand(checkTableQuery, connection)
                    tableExists = Convert.ToBoolean(checkCommand.ExecuteScalar())
                End Using
    
                If tableExists AndAlso userInput = "yes" Then
                    Dim dropTableQuery As String = $"DROP TABLE {tableName}"
                    Using dropCommand As New SqlCommand(dropTableQuery, connection)
                        dropCommand.ExecuteNonQuery()
                    End Using
                    MessageBox.Show("Table deleted.")
                ElseIf Not tableExists AndAlso userInput = "no" Then
                    Dim createTableQuery As String = $"CREATE TABLE {tableName} (ID INT PRIMARY KEY, Name NVARCHAR(50))"
                    Using createCommand As New SqlCommand(createTableQuery, connection)
                        createCommand.ExecuteNonQuery()
                    End Using
                    MessageBox.Show("Table created.")
                Else
                    MessageBox.Show("No action taken.")
                End If
            End Using
        End Sub
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.