Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
im new to .net and how to display data in tabular format from dataset in windowform.

below code is what i have tried. but it shows nothing.

thanks in advance.

What I have tried:

C#
private void button2_Click(object sender, EventArgs e)
        {
                        SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            da.SelectCommand = new SqlCommand("SELECT name,addres,number FROM people ", con);
            da.Fill(ds, "data");
            dt = ds.Tables["data"];
}
Posted
Updated 15-Mar-17 10:01am
v3
Comments
[no name] 15-Mar-17 14:08pm    
It "shows nothing" because you aren't showing the data anywhere.
Member 12927094 15-Mar-17 14:13pm    
how can i show ?
can u guide me "where i do mistake?

thankyou
[no name] 15-Mar-17 14:19pm    
What do you mean "how can I show?"? Like everything else in Winforms, you show data in controls. What control are you using to show the data?
Member 12927094 15-Mar-17 14:22pm    
can we show in table format ?
[no name] 15-Mar-17 14:40pm    
I have no idea what you think that means. Probably safe to say "yes".

1 solution

Based on your input, I think you want to see some data on the User Interface on button click.

1. Add a DataGridView control on to the Window form, you can leave the name dataGridView1 for now, you can change it later once you got it to work.
2. use the updated code below.

Assumption
1. The connection string is working
2. there are data in the database.

C#
private void button2_Click(object sender, EventArgs e)
{
    SqlDataAdapter da = new SqlDataAdapter();
    // DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    
    da.SelectCommand = new SqlCommand("SELECT name,address,number FROM people ", con);
    da.Fill(dt);
    //  da.Fill(ds, "data");
    //  dt = ds.Tables["data"];
    
    con.Close();
    
    dataGridView1.DataSource = dt;
}


How to: Bind Data to the Windows Forms DataGridView Control[^]
 
Share this answer
 
Comments
Member 12927094 16-Mar-17 1:36am    
Thank you sir ..just i started learning .net 2 days back. it helped me to go forward..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900