Home > Software design >  when i try to display 3 records from database in single textbox only the last record is displayed an
when i try to display 3 records from database in single textbox only the last record is displayed an

Time:10-27

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.

CodePudding user response:

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 enter image description here

So a control like a text box can have one value.

but if you want "many", then use a grid control. You can also use a repeater if you not looking for a grid (table) like output.

  • Related