Click here to Skip to main content
15,887,442 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hey guys can anyone help me to check if a text exist in database
thats what im writting at this moment
<
connstring = "SERVER=188.121.43.9;PORT=3306;DATABASE=mydbname;UID=mydbusername;PASSWORD=s1996;";
        MySqlConnection conn = new MySqlConnection(connstring);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "Select all from dbtable";
>

this is how my DB looks like

<
uname     upass
test      1900
test2     1999
>
i have textbox1
what i want is
for example :
chek if textbox1.text exist at column uname show MessageBox.Show("exist")

What I have tried:

hey guys can anyone help me to check if a text exist in database
thats what im writting at this moment
<
connstring = "SERVER=188.121.43.9;PORT=3306;DATABASE=mydbname;UID=mydbusername;PASSWORD=s1996;";
        MySqlConnection conn = new MySqlConnection(connstring);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "Select all from dbtable";
>

this is how my DB looks like

<
uname     upass
test      1900
test2     1999
>
i have textbox1
what i want is
for example : 
chek if textbox1.text exist at column uname show MessageBox.Show("exist")
Posted
Updated 8-May-17 20:52pm
Comments
ChauhanAjay 7-May-17 19:47pm    
change your query to "Select count(*) from dbtable WHERE uname like '%"+textbox1.text.toString()+"%'". If the result of the query is greater than zero show your message box.
CHill60 8-May-17 7:19am    
Do not do this!
That query is vulnerable to SQL Injection attack. Never concatenate user input into your SQL commands.
[no name] 8-May-17 10:00am    
Your code would not compile.
If you managed to get it to compile, what possible benefit do you think you get if you call ToString on a string?

I would use ExecuteScalar to be more efficient... like this:
C#
private void btnNext_Click(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == -1) return;
    using (var conn = new SqlConnection(connstring))
    {
        conn.Open();
        var cmd = new SqlCommand
        {
            CommandText = "select count(*) from dbtable where UPPER([name])=@name",
            Connection = conn
        };
        cmd.Parameters.AddWithValue("@name", comboBox1.SelectedItem.ToString());
        var res = (int)cmd.ExecuteScalar();
        if (res > 0) 
            MessageBox.Show("exist");
    }
}
Things to note:
- I've used using see Establishing the Connection[^]
Quote:
The Using block in Visual Basic or C# automatically disposes of the connection when the code exits the block, even in the case of an unhandled exception.

- I've used the ExecuteScalar method as it requires less code and fewer resources - see SqlCommand.ExecuteScalar Method (System.Data.SqlClient)[^]
- I check to ensure there is something actually selected on the comboBox before attempting to use the value.
 
Share this answer
 
C#
<pre>String connstring = "SERVER=188.121.43.9;PORT=3306;DATABASE=mydbname;UID=mydbusername;PASSWORD=s1996;";
            SqlConnection conn = new SqlConnection(connstring);
            SqlCommand command = conn.CreateCommand();
            command.CommandText = "Select * from dbtable where name=@name";
            command.Parameters.AddWithValue("@name", comboBox1.SelectedValue);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("exist");
            }
 
Share this answer
 
v3
Comments
CHill60 8-May-17 7:43am    
I voted 4 only, up points for using a Parameterized query but not maximum because you are using a SqlDataAdapter and filling a DataTable. Single value only is required and is more efficient.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900