?
Solved

Binding data to datagrid in C# web form

Posted on 2010-09-03
5
Medium Priority
?
573 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 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