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.
2
Answers
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
This should repeat the list for each record.
Now you need to bind your data to the repeater
Well, how this works is quite simular to Access. But if you just place some text boxes on a page, then you can set the values of those text boxes.
However, like Access or any desktop software, those controls ONLY show one value.
since you have repeating data? Then best to adopt some kind of grid or table like control that supports "many" rows of data.
I would suggest you try a grid view say like this:
Now, here is the code to load the above up:
Now, I don’t have your data, but say this:
And we now get this for the output:
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.