Solved

Binding data to datagrid in C# web form

Posted on 2010-09-03
5
567 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
Comment Utility
0
 
LVL 6

Author Comment

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

Accepted Solution

by:
JessyEzzy earned 500 total points
Comment Utility
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
Comment Utility
Excellent job - It was a please learning from you :)
0
 
LVL 6

Author Comment

by:r3nder
Comment Utility
Pleasure**
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now