Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi guys,

i have a problem in fetching a data from sql server in asp.net using c#.

details:-

i want to create a dynamic list from sql. like notice board or New & Events.

i have 2 column in sql table filling with a 4 entry like
Column1                 Column2
 A                         12
 B                         32
 C                         65
 D                         45

thats my table.

i want on home page onsite column1 list has been display. when some one is click on this new page has been open in which column 2 data has been show only selected value of column one.

thanks
Posted
Updated 1-Jan-14 21:22pm
v2
Comments
JoCodes 2-Jan-14 4:24am    
What you tried and where are you stuck?

use GRIDVIEW
for reference try this :

how to get data from sql database to gridview by coding ?[^
 
Share this answer
 
use gridview or repeater and set href to another page by taking "id" through query string.
 
Share this answer
 
Hello

You can use Grid view and for opening the new page use Query string to pass the value and get the data from there corresponding to the query string.

Here is a Code snippet to help you with...

On Home Page Create a Gridview like this


XML
<asp:GridView ID="gdvBookedProduct" runat="server" AutoGenerateColumns="False" CellPadding="4"
                                        ForeColor="#333333" GridLines="None" Width="100%"
                                        HeaderStyle-HorizontalAlign="Left"
                                        onrowdatabound="gdvBookedProduct_RowDataBound">
                                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                        <Columns>
                                            <asp:TemplateField HeaderText="First Column">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lnkFirstColumn" runat="server" Text="First Column Value" OnClick="lnkFirstColumn_Click" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                        <EditRowStyle BackColor="#999999" />
                                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                                    </asp:GridView>



On Code behind of Home Page

C#
protected void lnkDetails_Click(object sender, EventArgs e)
       {
           GridViewRow row = ((LinkButton)sender).Parent.Parent as GridViewRow;
           int r = row.RowIndex;
           string FirstColumnValue = gdvBookedProduct.Rows[r].Cells[0].Text.Trim();


           Response.Redirect("SecondPage.aspx?FistColumnValue=" + FirstColumnValue);


       }



On Second Page

Bind another grid with Select Statement and pass the query string to where condition.

  string FirstColumnValue= Request.QueryString.Get("FirstColumnValue");
SqlConnection con = new SqlConnection("");//connection name

               con.Open();
 
               SqlCommand cmd = new SqlCommand("select * from tablename where FirstColumn = " + FirstColumnValue , con);

 
               cmd.CommandType = CommandType.Text;
 
               SqlDataAdapter da = new SqlDataAdapter(cmd);
 
               DataSet ds = new DataSet();
 
               da.Fill(ds, "ss");
 
               dataGridView1.DataSource = ds.Tables["ss"]; ;



I think this solution will help you. For any queries feel free to write back.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900