none
Button for checking if a textbox value already exist in database. RRS feed

  • Question

  • Hello guys I' m creating a desktop app with c# winform and i want your help. I want the user to check with a button if the text in a specific textbox already exist in Sql Server Database. I don' t know if i have explained it well. I would appreciate it if you could help me.

    Thank you !!!

    Saturday, December 7, 2019 7:10 PM

All replies

  • Hi TakPag, 
    You can use the text of textbox as a condition to search for the data, then use DataTable.Rows.Count to determine if data exists.
    Here is a code example you can refer to.
    SqlDesigner sql= new SqlDesigner();
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    private void button1_Click(object sender, EventArgs e)
    {
        ds = sql.ExecuteDataSet("select * from test where Quantity='" + textBox1.Text + "'");
        dt = ds.Tables[0];
        if (dt.Rows.Count ==0)
        {
        MessageBox.Show("nonexistent");
        }
        else 
        {
        MessageBox.Show("existing");
        }
    }
    class SqlDesigner
    {
    private string connStr = @"ConnectString";
    public DataSet ExecuteDataSet(string sql)
    {
        using (SqlConnection xonn = new SqlConnection(connStr))
        {
            xonn.Open();
            using (SqlCommand cmd = xonn.CreateCommand())
            {
                cmd.CommandText = sql;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet dataset = new DataSet();
                adapter.Fill(dataset);
                return dataset;
            }
        }
    }
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 9, 2019 2:37 AM
  • Thank you!!!
    Monday, December 9, 2019 9:56 AM
  • This may look good but it promotes poor programming.

    ds = sql.ExecuteDataSet("select * from test where Quantity='" + textBox1.Text + "'");

    Suppose textBox1.Text has an apostrophe in it? What will happen is a runtime exception thrown where if you were to use a parameter entering say De'wayne a parameter will properly escape the apostrophe e.g. 'De''wayne' while as mentioned without 'De'wayne' that causes an exception.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 10, 2019 3:26 AM
    Moderator
  • Thanks!!!
    Tuesday, December 10, 2019 10:54 AM
  • Hi TakPag,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 24, 2019 9:18 AM