1,105,594 Community Members

I attempted to get the records from the MySQL database and display it on a

Member Avatar
venkateshyeluri
Newbie Poster
14 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I attempted to get the records from the MySQL database and display it on a Grid View but nothing appeared. The code is below:

MySqlConnection myconn = new MySqlConnection("server=localhost;user id=root;password=db;database=workers;");

   // myconn.Open();
    string strSQL = "select * from details";



    MySqlDataAdapter mydata = new MySqlDataAdapter(strSQL, myconn);

    MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(mydata);

    DataSet ds = new DataSet();

    mydata.Fill(ds, "details");

    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
   // myconn.Close();
Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
1
 

**Are you sure you have the right connection string? **
And if there are data inside details table?
If so you can try this code:

MySqlConnection myconn = new MySqlConnection("server=localhost;user id=root;password=db;database=workers;");
string strSQL = @"SELECT * FROM details";
MySqlDataAdapter mydata = new MySqlDataAdapter(strSQL, myconn);
DataTable table = new DataTable("myTable");
mydata.Fill(table);
GridView1.DataSource = table.DefaultView;
//close and  dispose all iDisposable objects on the end!

This has to work 100% if all is ok (connection string and if there are data inside table). There is really nothing else that can be wrong for not showing data in dgv.

Member Avatar
venkateshyeluri
Newbie Poster
14 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes my connection string is correct!

still nothing appeared when I run the page.. Is there anything else I have to change in web.config??

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

Put a break point before this code and check if DataTable gets fill up (I means in this code). When Fill() method goes over, put a mouse cursor over "table" and you will see one magnifying glass. Double click on it, and another window will open with the data. There must be the data inside, but I am affraid it will empty (thats why the GridView is empty too).
This means your database table has to data inisde.

BTW: What database do you use this? mySql?

Member Avatar
venkateshyeluri
Newbie Poster
14 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes I am using MYSQL

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

Have you checked what I told you to, by putting a break point into this code?

Member Avatar
venkateshyeluri
Newbie Poster
14 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Just now I have tried using break point, dataTable has my database content. From here it is not posting into GridView

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

Do you use GridView or DataGridView?

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

If THERE really are data inside dataTable, the way you try to bind it should work:
GridView GridView1=new GridView();
GridView1.DataSource = table;
GridView1.DataBind();

In case if not, you can try binding like:
GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
GridView1.DataBind();

Member Avatar
venkateshyeluri
Newbie Poster
14 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I use GridView,
I have tried either case.. till here
GridView1.DataSource = table;
when I use Maganifier it shows me table content.. when comes to DataBind(); it shows null

And using this second method
GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
this raises to an error "Object reference not set to an instance of an object."

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: