0

I recently write a simple program that use database.

I've created an DB1.MDF and get data from it.

When I try to do Select from its Table (i.e. Table1), it gets run time error!

How I should do that?!

One more question:

I've created Data Grid View and put data in that. Now, how I can for example use column[2] of it (e.g. column[2] in my dataGridView is LastName and I want to save all LastNames in one String).

Here is my Code:

private void button1_Click(object sender, EventArgs e)
{

     SqlConnection conn = new SqlConnection();

     conn.ConnectionString = @"Data Source=.\SQLEXPRESS;
           AttachDbFilename=|DataDirectory|\DB1.mdf;
           Integrated Security=True;User Instance=True";

     conn.Open();

     SqlCommand cmd = new SqlCommand();

     cmd.Connection = conn;

     cmd.CommandText = "SELECT ID, Name, LastName from Table1 WHERE Name == Joe ";

     SqlDataAdapter da = new SqlDataAdapter();

     da.SelectCommand = cmd;

     DataTable dt = new DataTable();
     da.Fill(dt);

     dataGridView1.DataSource = dt;
     conn.Close();
 }

 private void button2_Click(object sender, EventArgs e)
 {
   textBox1.Text = dataGridView1.Columns[0].ToString();
 } 
1
  • 5
    What runtime error are you getting? Commented May 21, 2011 at 15:56

3 Answers 3

3

The select query should be like this

cmd.CommandText = "SELECT ID, Name, LastName from Table1 WHERE Name = 'Joe'";

And for your second question, you could use something like this

private void button2_Click(object sender, EventArgs e)
{
     StringBuilder builder = new StringBuilder();
     dataGirdView1.Rows.Cast<DataGridViewRow>().ToList()
         .ForEach(r => builder.Append(r.Cells[2].Value));
     textBox1.Text = builder.ToString();
}
Sign up to request clarification or add additional context in comments.

7 Comments

@Oded Thanks. From all the formatting I've done on stackoverflow, I been using ` instead of ' all over the place.
@ Bala R: thanks for your help...I found my mistake in SELECT command! If I want to use another SELECT right after this SELECT and put that into DataGridView2, I have write all these code agian?
@ Bala R: Can you explain a little about button2_Click codes? Is it only way to get data from specific column of DataGridView?
@ha.M.ed I'm not sure about the question on another select (can you rephrase it?) but the button2_click basically loops through the rows and appends the value in cell[2] for each row to a stringbuilder instance. You can use a for loop if you would like that instead.
@ Bala R - I mean when I showed data in DataGridView1, I write cmd.CommandText = "SELECT ID from Table1 WHERE Name = 'Mick' " and put reuslt in DataGridView2 (use same SqlDataAdapter and DataTable objects), DataGridView1 and DataGridView2 show me all rows without any Selection!
|
0

Bala R has it right, the == operator does not exist in SQL, therefore if your testing for an equality you should use the single = operator. And if you're comparing strings (varchar or nvarchar in SQL) you should put them in single quotes i.e. 'Joe'.

1 Comment

Welcome to StackOverflow, Louis! By your own admission, your answer doesn't add anything to the discussion. Perhaps this would be better as a comment on @Bala's answer?
0

This query seems wrong.

SELECT ID, Name, LastName from Table1 WHERE Name == Joe 

Can you try this?

cmd.CommandText = "SELECT ID, Name, LastName from Table1 WHERE Name = 'Joe'"

Also, please post the details of the error message.

Comments

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.