?
Solved

Binding data to datagrid in C# web form

Posted on 2010-09-03
5
Medium Priority
?
577 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 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 2000 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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 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