none
Using DataRead in Asp.net! RRS feed

  • Question

  • Hi Team Technet,

    I have one question to ask u. I use DataRead to bind data from sqlserver for 4 records to my table but when it execute it can show only three records. I don't why it can retrieve and show for only three records. Can you show me how to show all record that I quested? 

    Friday, June 24, 2011 4:50 AM

Answers

  • This is a Training and Certification forum.

    To ensure that your post can be moved to a more appropriate forum, please provide the following information.

    • Are you creating a Web Application or Windows Application?
    • What version of Visual Studio and SQL Server are you using?
    • Can you post a copy of your code (by using the Insert Code Block icon at the top of your reply post)?

     


    Jeff Wharton
    MSysDev (C.Sturt), MDbDsgnMgt (C.Sturt) MCT, MCPD, MCITP, MCDBA
    Blog: Mr. Wharty's Ramblings
    Please mark solved if I've answered your question, vote for it as helpful to help other user's find a solution quicker
    Friday, June 24, 2011 5:03 AM
    Moderator

All replies

  • This is a Training and Certification forum.

    To ensure that your post can be moved to a more appropriate forum, please provide the following information.

    • Are you creating a Web Application or Windows Application?
    • What version of Visual Studio and SQL Server are you using?
    • Can you post a copy of your code (by using the Insert Code Block icon at the top of your reply post)?

     


    Jeff Wharton
    MSysDev (C.Sturt), MDbDsgnMgt (C.Sturt) MCT, MCPD, MCITP, MCDBA
    Blog: Mr. Wharty's Ramblings
    Please mark solved if I've answered your question, vote for it as helpful to help other user's find a solution quicker
    Friday, June 24, 2011 5:03 AM
    Moderator
  • I am creating Web Application. I use Visual Studio 2010 and Sql Server 2008 R2.

    This is my code for create.

    <!--Laptop Show-->

                <%System.Data.SqlClient.SqlConnection con=new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbStock.MDF;Integrated Security=True;User Instance=True");%>

                <%con.Open(); 

                System.Data.SqlClient.SqlCommand cmdread=new System.Data.SqlClient.SqlCommand("SELECT TOP 4 * FROM tbLaptop ORDER BY LaptopID desc",con);

                System.Data.SqlClient.SqlDataReader readData=cmdread.ExecuteReader();

     

                %>

                <tr>

                <%while(readData.Read())

                  {%>

                  <td align="center"><asp:Image ID="imageLaptop" runat="server" Width="160px" Height="160px" /><% imageLaptop.Visible = true; imageLaptop.ImageUrl = "~/Images/" +  readData[1].ToString().Trim() + ".jpg"; %><br /><asp:Label ID="lbBrandname" runat="server"></asp:Label><%lbBrandname.Text=readData[1].ToString().Trim(); %><br /><asp:Label ID="lbPrice" runat="server"></asp:Label><%lbPrice.Text="$ " + readData["Price"].ToString(); %></td>

     

                 <% }

                     cmdread.Dispose();

                     con.Close();

                 %>

                </tr>

                <!--Desktop Show-->

     

                <%con.Open(); 

                System.Data.SqlClient.SqlCommand cmdreadDesk=new System.Data.SqlClient.SqlCommand("SELECT TOP 4 * FROM tbDesktop ORDER BY DeskID desc",con);

                System.Data.SqlClient.SqlDataReader readDataDesk=cmdreadDesk.ExecuteReader();

     

                %>

                <tr>

                <%while (readDataDesk.Read())

                  {%>

                  <td align="center"><asp:Image ID="imageDesk" runat="server" Width="160px" Height="160px" /><% imageDesk.Visible = true; imageDesk.ImageUrl = "~/Images/" + readDataDesk[1].ToString().Trim() + ".jpg"; %><br /><asp:Label ID="lbNameDesk" runat="server"></asp:Label><%lbNameDesk.Text = readDataDesk[1].ToString().Trim(); %><br /><asp:Label ID="lbPriceDesk" runat="server"></asp:Label><%lbPriceDesk.Text = "$ " + readDataDesk["Price"].ToString(); %></td>

     

                 <% }

                     cmdreadDesk.Dispose();

                     con.Close();

                 %>

                </tr>

                <!--Printer Show-->

     

                <%con.Open(); 

                System.Data.SqlClient.SqlCommand cmdreadPrint=new System.Data.SqlClient.SqlCommand("SELECT TOP 4 * FROM tbPrinter ORDER BY PrinterID desc",con);

                System.Data.SqlClient.SqlDataReader readDataPrint = cmdreadPrint.ExecuteReader();

     

                %>

                <tr>

                <%while (readDataPrint.Read())

                  {%>

                  <td align="center"><asp:Image ID="imagePrint" runat="server" Width="160px" Height="160px" /><% imagePrint.Visible = true; imagePrint.ImageUrl = "~/Images/" + readDataPrint[1].ToString().Trim() + ".jpg"; %><br /><asp:Label ID="lbNamePrint" runat="server"></asp:Label><%lbNamePrint.Text = readDataPrint[1].ToString().Trim(); %><br /><asp:Label ID="lbPricePrint" runat="server"></asp:Label><%lbPricePrint.Text = "$ " + readDataPrint["Price"].ToString(); %></td>

     

                 <% }

                     cmdreadPrint.Dispose();

                     con.Close();

                 %>

                </tr>

                <!--Printer Show-->

     

                <%con.Open(); 

                System.Data.SqlClient.SqlDataAdapter cmdreadCamera=new System.Data.SqlClient.SqlDataAdapter("SELECT TOP 4 * FROM tbCamera ORDER BY CameraID desc",con);

                System.Data.DataSet ds=new System.Data.DataSet();

                    cmdreadCamera.Fill(ds);

     

                %>

                <tr>

                <%foreach (System.Data.DataRow dr in ds.Tables[0].Rows)

                  {%>

                  <td align="center"><asp:Image ID="imageCamera" runat="server" Width="160px" Height="160px" /><% imageCamera.Visible = true; imageCamera.ImageUrl = "~/Images/" + dr[1].ToString().Trim() + ".jpg"; %><br /><asp:Label ID="lbNameCamera" runat="server"></asp:Label><%lbNameCamera.Text = dr[1].ToString().Trim(); %><br /><asp:Label ID="lbPriceCamera" runat="server"></asp:Label><%lbPriceCamera.Text = "$ " + dr["Price"].ToString(); %></td>

     

                 <% }

                     cmdreadCamera.Dispose();

                     con.Close();

                 %>

                </tr>

     

    Friday, June 24, 2011 5:52 AM
  • Hi Moderators,

    Can you please move this thread to the Web Forms Data Controls forum so that the OP can get the assistance they require.

    http://forums.asp.net/24.aspx/1?Web+Forms+Data+Controls


    Jeff Wharton
    MSysDev (C.Sturt), MDbDsgnMgt (C.Sturt) MCT, MCPD, MCITP, MCDBA
    Blog: Mr. Wharty's Ramblings
    Please mark solved if I've answered your question, vote for it as helpful to help other user's find a solution quicker
    Friday, June 24, 2011 5:57 AM
    Moderator
  • Okay, I can think two reasons why your code may be showing three records intstead of four.

    The first is that the database only contains three records that satisfy your query. Even if you added a "TOP 4" clause, it is not guaranteed that you will receive 4 records; you can receive less if there are less records in the tables.

    The second one is that the data are actually being retrieved, but they are not being properly displayed by the browser due to wrong or mismatched HTML tags. Unfortunately, such a circumstance is very difficcult to verify at first sight from taking a look at your code, because you are using the old coding style of ASP instead of the newer techniques available in ASP.NET. There is a reason why the ASP style is nowadays considered obsolete; it is not easily readable or maintainable. There are much better techniques in ASP.NET for achieveing your goal, such as using a DataRepeater control and databinding the control to either a DataReader or a DataSet. This would make the source code much clearer and the mistake would likely be easier to spot.

     

    Friday, June 24, 2011 7:34 AM