Search code examples
c#htmlasp.netoledb

when i try to display 3 records from database in single textbox only the last record is displayed and not the first two in asp.net empty framework


I have a table called tbl_cmpjobdet in ms access where the details of the job are being stored. When the user wants to search for a job then all the available jobs should be shown to him. I have retrieved the database records using oledbdatareader and then used them for a loop trying to display this data in text-boxes. I am trying to display 4 things i.e company name, city, state, and job in 4 text-boxes. When the program runs the values are retrieved properly but only the last record is displayed on the screen and not the previous ones. I am using the same four text-boxes for all the records. I want to display all the records. Here is the code that I am trying. Here I am declaring the textbox

<div>
    <ul style="list-style-type:none">
        <li>
            <asp:TextBox ID="txt_cname" runat="server" BorderStyle="None"></asp:TextBox>
        </li>
        <li>
            <asp:TextBox ID="txt_city" runat="server" BorderStyle="None"></asp:TextBox>
        </li>
        <li>
            <asp:TextBox ID="txt_cstate" runat="server" BorderStyle="None"></asp:TextBox>
        </li>
        <li>
            <asp:TextBox ID="txt_cjob" runat="server" BorderStyle="None"></asp:TextBox>
        </li>
        <hr />
        
    </ul>
        </div>

here is where I am retrieving the records from the database and displaying them in textbox

con.Open();
        OleDbCommand cmd = new OleDbCommand("Select count(*) from tbl_cmpjobdet");
        OleDbCommand md = new OleDbCommand("select * from tbl_cmpjobdet");
        cmd.Connection = con;
        md.Connection = con;
        OleDbDataReader dr = md.ExecuteReader();
        int i = Convert.ToInt32(cmd.ExecuteScalar())
 for(int j=0;j<=i;j++)
        {
            while(dr.Read())
            {
                cname = dr["cmp_name"].ToString();
                city = dr["cmp_city"].ToString();
                state = dr["cmp_state"].ToString();
                jname = dr["job_name"].ToString();

              txt_cname.Text = cname;
                txt_city.Text = city;
                txt_cstate.Text = state;
                txt_cjob.Text = jname;
            }

        }

Now the loop does run properly and the values are also passed to the text-box but the values that are displayed on the screen are just of the last record only.


Solution

  • It sounds like you want to be able to show all records in their own set of text boxes. To do this with WebForms you will need asp:Repeater

    WARNING - I haven't used WebForms in anger for quite a while so this may not work out of the box

    First off, update your form to use a Repeater

    <asp:Repeater runast="server" id="Repeater">
        <ItemTemplate>
            <div>
                <ul style="list-style-type:none">
                    <li>
                       <%# Eval("cmp_name") %>
                    </li>
                    <li>
                       <%# Eval("cmp_city") %>
                    </li>
                    <li>
                       <%# Eval("cmp_state") %>
                    </li>
                    <li>
                       <%# Eval("job_name") %>
                    </li>
                    <hr />                
                </ul>
            </div>
        </ItemTemplate>
    </asp:Repeater>
    

    This should repeat the list for each record.

    Now you need to bind your data to the repeater

    OleDbDataReader dr = md.ExecuteReader();
    Repeater.DataSource = dr;
    Repeater.DataBind();