skip to Main Content

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


  1. 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();
    
    Login or Signup to reply.
  2. 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:

            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
    

    Now, here is the code to load the above up:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadGrid();
        }
    
        void LoadGrid()
        {
            using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))
            {
                using (OleDbCommand cmd = new OleDbCommand("SELECT * fromo tbl_cpmjobdet", conn))
                {
                    conn.Open();
                    GridView1.DataSource = cmd.ExecuteReader();
                    GridView1.DataBind();
                }
            }
        }
    

    Now, I don’t have your data, but say this:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadGrid();
        }
    
        void LoadGrid()
        {
            using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))
            {
                string strSQL = "SELECT ID, FirstName, LastName, HotelName, Description FROM tblHotels" +
                             "ORDER BY HotelName";
    
                using (OleDbCommand cmd = new OleDbCommand(strSQL, conn))
                {
                    conn.Open();
                    GridView1.DataSource = cmd.ExecuteReader();
                    GridView1.DataBind();
                }
            }
        }
    

    And we now get this for the output:

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search