Rico Brouwer Rico Brouwer - 2 months ago 6
MySQL Question

Fill textboxes with data from database multiple columns with datareader

I'm new to the database stuff, but want to fill textboxes with data with an datareader and sql query.
My program creates 4 textboxes from every ship in my database. Each textbox needs to be filled with data from a different column. When the 4 textboxes are filled, a new ship with 4 textboxes will be added and the second row of every column has to be added to the textboxes.

This is my code so far:

public void Schip()
{
using (SqlConnection thisConnection = new SqlConnection("Data Source=VMB-LP12;Initial Catalog=SmmsData;Integrated Security=True"))
{
string oString = "Select SchipNaam, RederijNr, Lengte, Laadvermogen FROM AlgSchipInfo";
SqlCommand oCmd = new SqlCommand(oString, thisConnection);

thisConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
int i = 0;
while (oReader.Read())
// for (int i = 0; i < count; i++)
{

System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.ID = "createDiv";
this.Controls.Add(createDiv);
List<TextBox> tb_names = new List<TextBox>();
TextBox tb_name = new TextBox();

//TextBox tb_name = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_1";
tb_name.Text = (oReader["SchipNaam"].ToString());
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Scheepspnaam: <input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);

//TextBox tb_name1 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_2";
tb_name.Text = (oReader["RederijNr"].ToString());
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> RederijNr:<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);

//TextBox tb_name2 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_3";
tb_name.Text = (oReader["Lengte"].ToString());
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Lengte :<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);

//TextBox tb_name3 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_4";
tb_name.Text = (oReader["Laadvermogen"].ToString());
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Laadvermogen:<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
i++;
}
}
}
}


I use my query to find the four columns and my datareader to read the data.
With
tb_name.text = (oReader["SchipMaam"].ToString())
the textboxes will be filled. Am i missing piece in this puzzle?

The problem is that my textboxes keep being empty when i use this.
There are no problems with the DIV's or ID's so far i'm aware of.
How can i fill my textboxes with the data from the columns of my database table?

It would be great if you could point me in the right direction.

Answer

Use this code, you were never assigning value to <input type='text' tag being generated:

public void Schip()
{    
    using (SqlConnection thisConnection = new SqlConnection("Data Source=VMB-LP12;Initial Catalog=SmmsData;Integrated Security=True"))
    {
        string oString = "Select SchipNaam, RederijNr, Lengte, Laadvermogen FROM AlgSchipInfo";
        SqlCommand oCmd = new SqlCommand(oString, thisConnection);

        thisConnection.Open();
        using (SqlDataReader oReader = oCmd.ExecuteReader())
        {
            int i = 0;
            while (oReader.Read())
            // for (int i = 0; i < count; i++)
            {

                System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
                new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                createDiv.ID = "createDiv";
                this.Controls.Add(createDiv);
                List<TextBox> tb_names = new List<TextBox>();
                TextBox tb_name = new TextBox();

                //TextBox tb_name = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_1";
                tb_name.Text = (oReader["SchipNaam"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Scheepspnaam: <input type='text' value='" + tb_name.Text + "' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name1 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_2";
                tb_name.Text = (oReader["RederijNr"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> RederijNr:<input type='text' value='" + tb_name.Text + "' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name2 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_3";
                tb_name.Text = (oReader["Lengte"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Lengte  :<input type='text' value='" + tb_name.Text + "' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name3 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_4";
                tb_name.Text = (oReader["Laadvermogen"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Laadvermogen:<input type='text' value='" + tb_name.Text + "' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);
                i++;
            }
        }
    }
}