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

  • Home
  • SEARCH
  • 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 6601641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:46:49+00:00 2026-05-25T18:46:49+00:00

I took an video from you tube to Retrieving Data from SQL Server using

  • 0

I took an video from you tube to Retrieving Data from SQL Server using C# and ADO.Net

http://www.youtube.com/watch?v=4kBXLv4h2ig&feature=related

I Do the same as him in the video…
I want to show data from an sql database in a DataGridView.

I get an error whit

da.Fill(dg);
dg.DataSource = dg.Tables[0];

I name my DataGridView dg…

Complete code

using System.Data.SqlClient;

namespace SQLconnection
 {
 public partial class Form1 : Form
{
    SqlConnection cs = new SqlConnection("Data Source=FRANK-PC\\SQLEXPRESS; Initial Catalog=Forc#; Integrated Security=TRUE");
    SqlDataAdapter da = new SqlDataAdapter();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        da.InsertCommand= new SqlCommand ("INSERT INTO tblContacts VALUES (@FirstName,@LastName)",    cs );
        da.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
        da.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastname.Text;

        cs.Open();
        da.InsertCommand.ExecuteNonQuery();
        cs.Close();
    }

    // Display data in dg
    private void button2_Click(object sender, EventArgs e)
    {
        da.SelectCommand = new SqlCommand("SELECT * FROM tblContacts", cs);
        da.Fill(dg);


        dg.DataSource = dg.Tables[0];
    }


}

}

  • 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-25T18:46:50+00:00Added an answer on May 25, 2026 at 6:46 pm

    The Fill method open/close connection implicitly but the problem is in name of dataGrdiView and DataTable/DataSet reference variable – dg

    private void button2_Click(object sender, EventArgs e)
        {
            da.SelectCommand = new SqlCommand("SELECT * FROM tblContacts", cs);
            DataTable dt=new DataTable();
            da.Fill(dt);
    
            dg.DataSource = dt;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Took a copy of a database of SQL Server Express on a laptop using
I took a data structures class in C++ last year, and consequently implemented all
I took over a Visual C++ project in Visual Studio 2005 from a colleague.
I'm trying to add a cuepoint to a video using OSMF. I built an
I took over a Rails app and am trying to get the Salesforce.com API
Im picking a video from UIImagePicker, the video is then being uploaded to my
Recently I took on a rather daunting task of creating an interactive music video
A took a look at http://erlang.org/doc/apps/inets/http_client.html and found the following: An ordinary asynchronous request.
I took the following example from Jrista's answer to a post. Finding Twentyone count
It took me a while, and some help from Apple (one instruction I missed),

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.