0

I have created a form and added a gridview, I am trying to retrieve data from a SQL Server database. When I run my code I don't get any error or data displayed in the gridview. Please help..

private void Form1_Load(object sender, EventArgs e)
{
    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

    using (SqlConnection con = new SqlConnection(CS))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from tblProductInventory", con);
        dataGridView1.DataSource = cmd.ExecuteReader();            
    }  
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nimo
  • 45
  • 5
  • ?!! `dataGridView1.DataSource = cmd.ExecuteReader(); ` – Reza Aghaei Apr 07 '18 at 11:32
  • Take a look at [CRUD Operations using DataGridView, DataTable and TableAdapter](https://stackoverflow.com/a/36274706/3110834) or to get most out of the designer, take a look at a [Quick start to create a data application](https://stackoverflow.com/a/37824444/3110834) – Reza Aghaei Apr 07 '18 at 11:38

3 Answers3

0
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

SqlConnection conn = new SqlConnection(CS);
    conn.Open();
    string query = "Select * from tblProductInventory";

    SqlCommand cmd = new SqlCommand(query, conn);

    DataTable t1 = new DataTable();
    using (SqlDataAdapter a = new SqlDataAdapter(cmd))
    {
        a.Fill(t1);
    }

    dataGridView1.DataSource = t1;
Murat Can OĞUZHAN
  • 735
  • 11
  • 19
0

Use DataAdapter to fill the DataTable.

 private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dataTable= new DataTable();
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
              con.Open();
              SqlCommand cmd = new SqlCommand("Select * from tblProductInventory", con);               
              SqlDataAdapter adapter = new SqlDataAdapter(cmd);
              adapter.Fill(dataTable);                    
              dataGridView1.DataSource = dataTable;             
            }  
       }
user9405863
  • 1,506
  • 1
  • 11
  • 16
  • i tried with this dataGridView1.DataBind(); ... it shows erros saying it does exist – Nimo Apr 07 '18 at 11:52
  • i tried this code and getting error on this line: adapter.Fill(dataTable); InvalidOperationException – Nimo Apr 07 '18 at 11:59
  • i am getting as InvalidOperationException was unhandled.. and this line is being selected adapter.Fill(dataTable); – Nimo Apr 07 '18 at 12:03
  • Thanks alot. this works fine.. can you share me where did you learn this ado.net c# windowform. this will be big help if you can share – Nimo Apr 07 '18 at 12:18
  • so many blogs over internet. https://msdn.microsoft.com/en-us/library/jj943772.aspx – user9405863 Apr 07 '18 at 12:20
0

The ExecuteReader method reads data from the given table(from database) and it builds a DataReader for you.

You cannot directly populate a datagridview with command.ExecuteReader.You need to either use a DataReader or a DataTable.

DataTable Example :

  DataTable dt = new DataTable()
  using (SqlDataAdapter ada = new SqlDataAdapter(cmd))
{
    ada.Fill(t1);
}
 MyDatagrid.Datasource = dt;

Now i would never suggest using a DataTable as it is both time consuming and hindrance to performance.Rather go with a DataReader.

DataReader Example:

''It is better to create a class to handle your data :)

  public class MyData
{
    private int age;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
       } 
    }

  ''Ever heard of ArrayList...It's amazing :)

  ArrayList myarray= new ArrayList();

  '''Now let's fix the main issue :)

  SqlDataReader reader = command.ExecuteReader();
  while (reader.Read())
  {
  MyData data = new MyData();
  data.Id = (int)reader[0];
  data.Name = reader[1].ToString();
  data.Age = (int)reader[2];
  myArray.Add(data);
  }
  myDataGrid.DataSource = sequence;
Software Dev
  • 5,368
  • 5
  • 22
  • 45