4
Answers

List data in gridview using objects and class or in procedural method?

Jirr Meynor

Jirr Meynor

14y
2.3k
1
Hi,

Can anyone help me? I just want to know if it is advisable to use objects or procedural in listing all items in gridview. Example is if i want to "SELECT * FROM dogs" and put it in a gridview. Is it proper to make a method in a class or directly code it in the load event of a form?

Pardon my bad english.

Thanks guys. Hope anyone can help me.

Jirr
Answers (4)
0
Krishna Garad

Krishna Garad

NA 16.5k 6.2m 14y
Try this in Class
Public DataSet GetData()
{
   SqlConnection _sqlcncn=new SqlConnection("ConnectionString);
 SqlCommand  sqlcmd = new SqlCommand("Select Query", sqlcn);
          
          
           // sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcn.Open();
            DataSet ds = new DataSet();
            sqlda = new SqlDataAdapter(sqlcmd);
            sqlda.Fill(ds);
            return ds;
}

In code behind call this method like bello
DataSet ds=Class.GetData();
Gridview.DataSource=ds.Tables[0];
GridView.DataBind();
Accepted
0
Jirr Meynor

Jirr Meynor

NA 22 0 14y
Thanks sir! Help is greatly appreciated.
0
Jirr Meynor

Jirr Meynor

NA 22 0 14y
Hi sir,

Thank you for answering that question. But I have another problem regarding that. I don't know how will I use that method and bind it in the gridview. Can you give me a simple example? Thanks again sir.

Jirr
0
Krishna Garad

Krishna Garad

NA 16.5k 6.2m 14y
Create a method in a class and call this method in page load event if you want to show the details on load or in any another event also.