Solved

Binding data to datagrid in C# web form

Posted on 2010-09-03
5
569 Views
Last Modified: 2013-12-17
I am trying to bind the data from a stored proceedure to a datagrid here is the code to retrieve the data
// Retrieving data from Stored Proceedure and bind it to datagrid
           try
           {
               con.Open();
               SqlCommand com = new SqlCommand("spx_leadershipAnalysis", con);
               com.CommandType = CommandType.StoredProcedure;
               rdr = com.ExecuteReader();
               while (rdr.Read())
               {
                   //Response.Write(rdr["Name"]);
                   
               } 

               //Response.Write(rdr);
               con.Close();
           }
           catch (Exception ex)
           {
               Response.Write(ex.Message);
               con.Close();
           }

        }

Open in new window


Here is my Datagrid
 <div id="div-datagrid">

    <asp:DataGrid id="DataGrid1" runat="server" CssClass="Grid" UseAccessibleHeader="True">
    <AlternatingItemStyle CssClass="GridAltRow"></AlternatingItemStyle>
    <ItemStyle CssClass="GridRow"></ItemStyle>
    <Columns>
    <asp:BoundColumn DataField="Uname" HeaderText="Name" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="entered" HeaderText="Entered" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlIntegrity" HeaderText="Integrity" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlValuePeople" HeaderText="Value People" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlTeamwork" HeaderText="Teamwork" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlAccountability" HeaderText="Accountability" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlCommunication" HeaderText="Communication" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlVision" HeaderText="Vision" 
        ItemStyle-Wrap="False"></asp:BoundColumn>   
    <asp:BoundColumn DataField="ddlAdaptability" HeaderText="Adaptability" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlHumility" HeaderText="Humility" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlCreativity" HeaderText="Creativity" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlTeachability" HeaderText="Teachability" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlPositiveInfluence" HeaderText="Positive Influence" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="ddlCourage" HeaderText="Courage" 
        ItemStyle-Wrap="False"></asp:BoundColumn>
    </Columns>
</asp:DataGrid>
    </div>

Open in new window


any ideas would be very appreciated
0
Comment
Question by:r3nder
  • 3
5 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 33597205
0
 
LVL 6

Author Comment

by:r3nder
ID: 33597254
This is for asp.Net with C# code behind
No links please
0
 
LVL 3

Accepted Solution

by:
JessyEzzy earned 500 total points
ID: 33597301
Instead of using SqlDataReader you can use DataTable by filling it through SqlDataAdapter and set DataTable as the DataSource of the DataGrid.
try
           {
               con.Open();
               SqlCommand com = new SqlCommand("spx_leadershipAnalysis", con);
               com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(com);
DataTable tbl = new DataTable();
adapter.Fill(tbl);
DataGrid1.DataSource=tbl;
DataGrid1.DataBind();
               //rdr = com.ExecuteReader();

               //while (rdr.Read())
               //{
                   //Response.Write(rdr["Name"]);
                   
               //} 

               //Response.Write(rdr);
               //con.Close();
//You don't need to explicitly close the connection when using //SqlDataAdapter coz it will close the connection after it fills //the DataTable
           }
           catch (Exception ex)
           {
               Response.Write(ex.Message);
               con.Close();
           }

Open in new window

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 33598207
Excellent job - It was a please learning from you :)
0
 
LVL 6

Author Comment

by:r3nder
ID: 33598236
Pleasure**
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question