Hi ,
I need help with select staement.I am trying to to look for a word in column word .If i find that word then then show a message box that this word exist in databse.Can anyone help me with this.my code is something like this

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex == -1) return; // column header clicked - do nothing    // get the row just clicked    
            DataRowView drv = dataGridView2.Rows[e.RowIndex].DataBoundItem as DataRowView;

            if (drv != null)
            {
                //convert row to strongly typed row from dataset 
                DataRow row = drv.Row as DataRow;
                string myString;
                string mystring2;
                SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");

                SqlDataAdapter da = new SqlDataAdapter();
                SqlCommandBuilder cmdBuilder;

                DataSet ds = new DataSet();
                
                cn.Open();
                mystring2 = @"SELECT word FROM wordsTbl where word = ('"+(string)row["word"]+"')";
                SqlCommand myCmd = new SqlCommand(mystring2, cn);
                myCmd.ExecuteNonQuery();
                cn.Close();
                if (myCmd == row["word"])
                {
                    MessageBox.Show(row["word"] + " already exist in your personal dictionary");
                }
                else
                {
                    cn.Open();
                    myString = @"INSERT INTO wordsTbl(word)  Values('" + (string)row["word"] + "')";
                    SqlCommand myCmd2 = new SqlCommand(myString, cn);
                    myCmd2.ExecuteNonQuery();

                    cn.Close();
                    MessageBox.Show(row["word"] + "is added to your personal dictionary");
                    richTextBox1.Text = "Word:" + row["word"];

                    //}    
                    //}

                }

Hello again,

myCmd.ExecuteNonQuery(); //<-- this executes the query but you do not get any data back
cn.Close();
if (myCmd == row["word"])  //<-- this is always false and probably generates a type mismatch at compile/run time
{
    MessageBox.Show(row["word"] + " already exist in your personal dictionary");
}

Try changing the command to "SELECT COUNT(word) ...

Then use object result = myCmd.ExecuteScalar(); The value returned is probably a datatable but i cannot remember just now.
Inspect it in debug and let me know what you get.

I am trying to debug.What happens is that not i am trying the statement if(result>1)
then blah blah .. , but i get error tht i cannot have '>' infrom of result.Thanks

it worked, now my code looks like this

mystring2 = @"SELECT word FROM wordTbl where word = ('"+(string)row["word"]+"')";
                SqlCommand myCmd = new SqlCommand(mystring2, cn);
                //myCmd.ExecuteNonQuery();
                object result = myCmd.ExecuteScalar();
                cn.Close();
                if (result.Equals(row["word"]) )
                {
                    MessageBox.Show(row["word"] + " already exist in your personal dictionary");
                }

I have another question for you though , ih i have a text in a rich text box and i want to apply image to it how can we do that. So suppose if i have a word horse in rich text box and i want to apply image to the hor part of it and another image to se part.how can we do this.Thanks

Sorry, no idea. Try posting as a new thread.

and the last question on this thread.How can a populate combobox list from database.What i am trying to do is to add to combobox list during run time and insert that to databse.Now when u ll run your application again it should pull that inserted list item and display in the combo box.Thanks

That should also be a new post and there have been many similar questions answered recently. Try searching the forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.