Shanaz K
Shanaz K

Reputation: 698

how to retrieve all data from sql server 2008 in asp.net

how to retrieve all data from sql server 2008 to my page in asp.net this is aspx code :

    <asp:Image ID="ImageProfile" runat="server" Width="20" Height="25" />
    <asp:Label ID="LabelName" runat="server" Text=""></asp:Label><br />
    <asp:Label ID="LabelAddress" runat="server" Text="Label"></asp:Label>

and this is my aspx.cs code :

    string cs=ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection con=new SqlConnection(cs))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM member",con);

            SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    ImageProfile.ImageUrl=dr["image"].ToString();
                    LabelName.Text=dr["Name"].ToString();
                    LabelAddress.Text=dr["Address"].ToString();

                }

        }

but my output just one result whereas I have more than three columns thanks for any guide

Upvotes: 0

Views: 7110

Answers (3)

Ajay
Ajay

Reputation: 6590

Try this

    <asp:Repeater ID="RepDetails" runat="server">
      <ItemTemplate>
           <asp:Image ID="ImageProfile" runat="server" Width="20" Height="25" ImageUrl = "<% # Eval("image") %>" />
           <asp:Label ID="LabelName" runat="server" Text = "<% # Eval("Name") %>"  ></asp:Label>
           <asp:Label ID="LabelAddress" runat="server" Text="<% # Eval("Address") %>"></asp:Label>
      </ItemTemplate>
    </asp:Repeater>

    string cs=ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con=new SqlConnection(cs))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM member",con);


       DataSet ds = new DataSet();
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       da.Fill(ds);
       RepDetails.DataSource = ds;
       RepDetails.DataBind();
    }

Upvotes: 0

Sasidharan
Sasidharan

Reputation: 3750

Better place your labels and image controls inside a gridview template..so that everything will occupy in it..

SqlConnection con=ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM member",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
ImageProfile.ImageUrl=ds.Tables[0].Rows[j][["image"].ToString();
LabelName.Text=ds.Tables[0].Rows[j]["Name"].ToString();
LabelAddress.Text=ds.Tables[0].Rows[j]["Address"].ToString();
}

Upvotes: 0

Manoj
Manoj

Reputation: 883

Try this way

<asp:Repeater ID="rpt" runat="server">
    <ItemTemplate>
        <asp:Image ID="ImageProfile" runat="server" Width="20" Height="25" ImageUrl = "<% # Eval("image") %>" />
        <asp:Label ID="LabelName" runat="server" Text = "<% # Eval("Name") %>"  ></asp:Label>
        <asp:Label ID="LabelAddress" runat="server" Text="<% # Eval("Address") %>"></asp:Label>
    </ItemTemplate>
</asp:Repeater>

In your C# code write this just set the data source to the repeater control

Upvotes: 1

Related Questions