Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5928945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:21:20+00:00 2026-05-22T14:21:20+00:00

I recently write a simple program that use database. I’ve created an DB1.MDF and

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:21:21+00:00Added an answer on May 22, 2026 at 2:21 pm

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote a program that used a simple producer/consumer pattern. It initially had
I've been playing with OpenCL recently, and I'm able to write simple kernels that
I have a simple SQLite database I use to track invoices. I recently decided
My wife recently bought a nano, and I wanted to write a simple program
I have been looking at Reflection.Emit recently. I wrote a simple program that generates
I have recently started to learn Objective-C and write my tests using OCUnit that
Recently for a programming class, we were given the assignment to write a program
I'm at a beginner level in FORTRAN. Recently, I tried designing a simple program
I've recently been given a chance to write a simple tcp-client for a project
recently I had to write a little script that parsed VMs in XenServer and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.