I am trying to attach the results of an SQL query to a gridview without creating a collection and instead using the SQLdataAdapter.
DataTable dt = new DataTable();
using (SqlCommand cm = cn.CreateCommand())
{
StringBuilder ct = new StringBuilder();
ct.AppendLine("SELECT DISTINCT........");
ct.AppendLine("FROM ...... er");
ct.AppendLine("INNER JOIN ....... ev ON er.eKey = ev.eKey");
ct.AppendLine("WHERE UserId ='" + _CurrentUser.UserId + "' AND [Type] = 'R' AND EndDate is NULL");
ct.AppendLine("Order By er.eKey DESC");
Debug.WriteLine(ct.ToString());
cm.CommandType = CommandType.Text;
cm.CommandText = ct.ToString();
System.Diagnostics.Debug.WriteLine(ct.ToString());
SqlDataAdapter dr = new SqlDataAdapter(cm);
dr.Fill(dt);
}
Previously I was using a simple table format to represent my data and it worked fine.
// int i = 0;
// foreach (DataRow row in dt.Rows)
// {
// if (i++ == 0)
// Response.Write("<span class='style5'><strong><span class='style6'><center>Existing Enrollments:</center><br/><br/></span></strong></span><table><tr><th>Enroll Key<br/></th><th>Name<br/><br/></th><th>DBA<br/><br/></th><th>Address<br/><br/></th><th>City<br/><br/></th><th>State<br/><br/></th><th>Zip<br/><br/></th><th>TIN<br/><br/></th><th>UserID<br/><br/></th></tr>");
// Response.Write("<tr>");
// foreach (DataColumn column in dt.Columns)
// {
// Response.Write("<td>" + row[column].ToString() + "</td>");
// }
Now I am trying this method:-
gvwERAenrollments.DataSource = dt;;
and it gives me an empty gridview.
my reference:- http://csharp.net-informations.com/dataadapter/datagridview-sqlserver.htm
Can anyone help me with this? There are going to be multiple rows returned by the query.