Ronak Keslikar
Ronak Keslikar

Reputation: 31

How to compare textbox value with the sql database value in c#?

How to compare textbox value with the sql database value in c#?

im a beginner n i have to make a project. i only know how to connect sql database with the c# project. and also tell me any tutorial, link or anything that can help me.

Upvotes: 0

Views: 14668

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67948

Here is a code sample that will assist you with this. Of course you can embelish on this as much as necessary but it will provide you the basics -- given the data that I have from your question.

        if (string.IsNullOrEmpty(textBox1.Text))
        {
            MessageBox.Show("Please enter a value into the text box.");
            this.textBox1.Focus();
            return;
        }

        SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
        connectionStringBuilder.DataSource = ".";
        connectionStringBuilder.InitialCatalog = "TEMP";
        connectionStringBuilder.IntegratedSecurity = true;

        SqlConnection connection = new SqlConnection(connectionStringBuilder.ToString());
        SqlCommand command = new SqlCommand("SELECT Column1 FROM TableA WHERE PKColumn = 1", connection);
        connection.Open();
        string value = command.ExecuteScalar() as string;
        connection.Close();

        if (textBox1.Text.Equals(value))
        {
            MessageBox.Show("The values are equal!");
        }
        else
        {
            MessageBox.Show("The values are not equal!");
        }

If you have some other specifics regarding this question I can probably give you a more specific example.

Upvotes: 2

Related Questions