0

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.

1
  • Look up docs for SqlCommand, and TextBox.Text on MSDN. Have a go, come back if you have a specific question about your code. Commented Feb 15, 2012 at 15:39

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have a question about your code above, how to place the comparison result in this code if (textBox1.Text.Equals(value)) { //show comparison result in datagrid here } into datagridview? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.