0

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.

5
  • Why are you adding all TextBoxes to tb_names? Commented Sep 22, 2016 at 10:12
  • I want every textbox to have and unique name. I do this with increment int (int i and i++) so i can Always make specific changes to a textbox. Commented Sep 22, 2016 at 10:15
  • and Why do you have TextBox tb_name = new TextBox(); commented? Commented Sep 22, 2016 at 10:18
  • This was some old code that i tried before. But this one works better. Commented Sep 22, 2016 at 10:20
  • Can you please EDIT your question to explain what works and what doesn't? Is the problem only with IDs not being Unique or there is some other problem too. Commented Sep 22, 2016 at 10:22

1 Answer 1

1

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++;
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Every textbox has an unique ID within my code, so i can make specific chanegs if i want to. tb_name.ID is giving back: CreateT0_1, CreatT0_2 and in the next sequence CreateT1_1. Am i missing something else here?
And i want every div to have the same ID so i can use my CSS on them all at the same time.
For CSS purpose, use classes. No 2 elements on a page should have the same ID.
You have a point and i will change it! But that does not have anything to do with my textboxes and filling them with data, or am i wrong ?
The TextBoxes are being generated by the inner Input type tag and not by tb_name. tb_name is never being added to the DIV actually.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.