0

I have made a form in which I have two fields, Name and Products. Beside Products I have taken a Textbox and a button. I am allowing my user to add more than one textbox and the limit is upto 5 textbox's. Now I am inserting data from this form into my SQL Database. And Now I want to fetch the data from table and display into the respective Textbox's. I want that on clicking Show All Button all the dynamic Textbox should appear on the form. And if the data is present in the DB for that particular field then it should display as text in these dynamic textbox.

I have tried doing this-

<div>
    <table border="1" width="1000px">
    <tr><td colspan="2" align="center"><b>Inserting Data Into Table</b></td></tr>
    <tr>
    <td class="style1">Name: </td>
    <td class="style2">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td class="style1">Add Text Box: </td>
    <td class="style2">
        <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Add More" 
            onclick="Button1_Click" /><br />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>      
        </td>
    </tr>
    <tr><td colspan="2" align="center">
        <asp:Button ID="Button2" runat="server" Text="Submit" onclick="Button2_Click" />
        <br />
        </td></tr>
    </table>
    </div><br /><br />
    <div>
    <table border="1" width="1000px">
    <tr><td colspan="2" align="center"><b>Fetching Data And Showing into Textbox</b></td></tr>
    <tr>
    <td class="style1">Name: </td>
    <td class="style2">
        <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td class="style1">Add Text Box: </td>
    <td class="style2">
        <asp:TextBox ID="txtt1" runat="server"></asp:TextBox>
        <asp:Button ID="Button3" runat="server" Text="Show All" 
            onclick="Button3_Click" /><br />
        <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>      
        </td>
    </tr>
    <tr><td colspan="2" align="center">
        <asp:Button ID="Updt" runat="server" Text="Update" onclick="Updt_Click" />
        <br />
        </td></tr>
    </table>
    </div>

CS Page:-

General_Logic g1 = new General_Logic();
DataTable dt = new DataTable();
int rows = 0;
List<string> ControlIdList = new List<string>();
int Counter = 1;
TextBox tb = new TextBox();
protected override void LoadViewState(object SavedState)
{
    base.LoadViewState(SavedState);
    ControlIdList = (List<string>)ViewState["ControlIdList"];
    foreach (string Id in ControlIdList)
    {
        Counter++;
        TextBox tb = new TextBox();
        tb.ID = Id;
        LiteralControl linebreak = new LiteralControl();
        PlaceHolder1.Controls.Add(tb);
        PlaceHolder1.Controls.Add(linebreak);
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    show();
}
public void show()
{
    dt = g1.return_dt("select product1,product2,product3,product4,product5,name from tbl_products where name='Yo Yo'");
    if (dt.Rows.Count > 0)
    {
        txtname.Text = dt.Rows[0]["name"].ToString();
        txtt1.Text = dt.Rows[0]["product1"].ToString();
        //TextBox txtb;
        int x = 2;
        foreach (Control ctrl in PlaceHolder2.Controls)
        {
            if (ctrl is TextBox)
            {
                if (x <= 5)
                {
                    if (Counter <= 4)
                    {
                        Counter++;
                        tb.ID = "TextBox" + Counter;
                        //tb.Text = tb.ID;
                        tb = (TextBox)ctrl;
                        LiteralControl linebreak = new LiteralControl("<br />");
                        tb.Text = dt.Rows[0]["product'" + x + "'"].ToString();
                        PlaceHolder2.Controls.Add(tb);
                        PlaceHolder2.Controls.Add(linebreak);
                        ControlIdList.Add(tb.ID);
                        ViewState["ControlIdList"] = ControlIdList;
                        x++;
                    }
                    //txtb = (TextBox)ctrl;
                    //LiteralControl linebreak = new LiteralControl("<br />");
                    //txtb.Text = dt.Rows[0]["product'" + x + "'"].ToString();
                    //PlaceHolder2.Controls.Add(txtb);
                    //PlaceHolder2.Controls.Add(linebreak);
                    //x++;
                }
            }

        }
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (Counter <= 4)
    {
        Counter++;
        tb.ID = "TextBox" + Counter;
        tb.Text = tb.ID;
        LiteralControl linebreak = new LiteralControl("<br />");
        PlaceHolder1.Controls.Add(tb);
        PlaceHolder1.Controls.Add(linebreak);
        ControlIdList.Add(tb.ID);
        ViewState["ControlIdList"] = ControlIdList;
    }
    else
    {
        Button1.OnClientClick = null;
        Response.Write("<script>alert('Maximum Entry is 5');</script>");
    }
}
protected void Button2_Click(object sender, EventArgs e)
{
        int limit = 4;
        string[] DBVALUES = new string[5];
        for (int parcount = 0; parcount<=limit; parcount++)
        {
            if (parcount == 0)
            {
                DBVALUES[parcount] = txt1.Text;
            }
            else
            {
                DBVALUES[parcount] = Request.Form["TextBox" + (parcount + 1).ToString()];
            }
        }
        for (int i = 0; i <= 4; i++)
        {
            if (DBVALUES[i] == null)
            {
                DBVALUES[i] = "NULL";
            }
        }
        rows = g1.ExecDB("insert into tbl_products(product1,product2,product3,product4,product5,name) values('" + DBVALUES[0].ToString() + "','" + DBVALUES[1].ToString() + "','" + DBVALUES[2].ToString() + "','" + DBVALUES[3].ToString() + "','" + DBVALUES[4].ToString() + "','"+TextBox1.Text.ToString()+"')");
        TextBox1.Text = string.Empty;
        txt1.Text = string.Empty;
        TextBox txtb;
        foreach (Control ctrl in PlaceHolder1.Controls)
        {
            if (ctrl is TextBox)
            {
                txtb = (TextBox)ctrl;
                txtb.Text = string.Empty;
            }
        }
    Response.Write("<script>alert('Data Inserted!!!');</script>");
}
protected void Updt_Click(object sender, System.EventArgs e)
{

}
protected void Button3_Click(object sender, System.EventArgs e)
{
    show();
}

Please guide me where I am doing wrong. I am waiting for your all suggestions.

Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
Omi
  • 427
  • 7
  • 21
  • 42
  • As per your code where you are facing the issue are u able to find the dynamically created textboxes? – Developer Feb 28 '14 at 07:33
  • @Dotnet Actually in second div I am not able to get the dynamically created textboxes on clicking Show All Button. Here I want that on clicking show all the dynamically created Textboxes should appear with the values from database. And in First Div I am able to create Textbox on clicking Add More button. And also I am able to insert values into DB into respective attributes of my table. – Omi Feb 28 '14 at 07:38
  • `Omi` as per your code I have seen that you are not adding the controls to the second place holder, just debug and check whether your `Show` method is entering in to this condition on button click `foreach (Control ctrl in PlaceHolder2.Controls){` – Developer Feb 28 '14 at 07:44
  • Also I think instead of `counter` while you are retrieving and creating the controls from database I will suggest you can give the `primary key` value so that it will be unique id for each control – Developer Feb 28 '14 at 07:58
  • @Dotnet Sir, I am not able to do what you suggested. Please can you provide me with a solution. Or some example of this kind. – Omi Feb 28 '14 at 09:07
  • @Dotnet Sir, Actually I have a form in which I am asking my users to insert values. And if they more than one products then I am providing them with option to add more textbox to insert products. Now I have a Edit Link on my web page. And onclicking this link the user will be redirected to the same page where he/she is inserting data. Here I want that if the data is present in the database for a particular field then it should display within the textbox, And if user want's to make any changes then he/she can do it and click on submit button to update. – Omi Feb 28 '14 at 09:12
  • @Dotnet The problem I am facing is with the dynamic textbox which I am creating on Add More button click. I want that if the data inserted from these dynamic textbox is present in the database, then these dynamic textbox should appear automatically on my web form. I don't know how to do this. Please guide me to solve this problem. – Omi Feb 28 '14 at 09:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48667/discussion-between-dotnet-and-omi) – Developer Feb 28 '14 at 10:11
  • @Dotnet Sir, I have started discussing in this chat room – Omi Feb 28 '14 at 10:30

1 Answers1

1

I think you need to modify your show method like this to have the controls inside placeholder, if your datatable is returning multiple rows you need to loop through all the rows to get the data of individual

public void show()
{
 dt = g1.return_dt("select product1,product2,product3,product4,product5,name from tbl_products where name='Yo Yo'");
if (dt.Rows.Count > 0)
{
    txtname.Text = dt.Rows[0]["name"].ToString();
    txtt1.Text = dt.Rows[0]["product1"].ToString();
    //TextBox txtb;
    int x = 2;
            if (x <= 5)
            {
                if (Counter <= 4)
                {
                    TextBox tb = new TextBox();
                    Counter++;
                    tb.ID = "TextBox" + Counter;
                    //tb.Text = tb.ID;
                    LiteralControl linebreak = new LiteralControl("<br />");
                    tb.Text = dt.Rows[0]["product'" + x + "'"].ToString();
                    PlaceHolder2.Controls.Add(tb);
                    PlaceHolder2.Controls.Add(linebreak);
                    ControlIdList.Add(tb.ID);
                    ViewState["ControlIdList"] = ControlIdList;
                    x++;
                }
                //txtb = (TextBox)ctrl;
                //LiteralControl linebreak = new LiteralControl("<br />");
                //txtb.Text = dt.Rows[0]["product'" + x + "'"].ToString();
                //PlaceHolder2.Controls.Add(txtb);
                //PlaceHolder2.Controls.Add(linebreak);
                //x++;
            }
        }

    }

If it was fixed that the controls should not be more that 4 try this code, while your clicking on showall replace with DataTable row as per your requirement

protected void Button3_Click(object sender, EventArgs e)
    {
        createControls();
    }

private void createControls()
    {
        PlaceHolder2.Controls.Clear();
        for (int i = 0; i < 4; i++)
        {
            TextBox tb = new TextBox();
            tb.ID = "TextBox" + i;
            tb.Text = tb.ID;
            LiteralControl linebreak = new LiteralControl("<br />");
            PlaceHolder2.Controls.Add(tb);
            PlaceHolder2.Controls.Add(linebreak);
        }
    }

I have created a datatable as per your requirement check this

private void createControls()
    {
        PlaceHolder2.Controls.Clear();
        DataTable dt = Session["Table"] as DataTable;
        for (int i = 1; i < 5; i++)
        {
            int cnt = 0;
            cnt = i;
            TextBox tb = new TextBox();
            cnt = cnt + 1;
            tb.ID = "TextBox" + i;
            tb.Text = dt.Rows[0]["Product" + cnt + ""].ToString();
            LiteralControl linebreak = new LiteralControl("<br />");
            PlaceHolder2.Controls.Add(tb);
            PlaceHolder2.Controls.Add(linebreak);
        }
    }

    private void assignValues()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Product2", typeof(string));
        dt.Columns.Add("Product3", typeof(string));
        dt.Columns.Add("Product4", typeof(string));
        dt.Columns.Add("Product5", typeof(string));

        DataRow lrow = dt.NewRow();
        lrow["Product2"] = "ABC";
        lrow["Product3"] = "DEF";
        lrow["Product4"] = "GHI";
        lrow["Product5"] = "JKL";
        dt.Rows.Add(lrow);
        Session["Table"] = dt;
    }

 protected void Button3_Click(object sender, EventArgs e)
    {
        assignValues();
        createControls();
    }

Here is the o/p as per my datatable on clicking showall

PageLoad

ShowAll

Developer
  • 8,390
  • 41
  • 129
  • 238
  • I have replaced my code and now I am getting this exception `System.ArgumentException: Column 'product '2'' does not belong to table` I am getting error in this line- `tb.Text = dt.Rows[0]["product '" + x + "'"].ToString();` What is wrong in it sir? – Omi Feb 28 '14 at 12:28
  • `tb.Text = dt.Rows[0]["product '" + x + "'"].ToString();` what is `x` Here – Developer Feb 28 '14 at 12:31
  • Actually I have attributes in my table like this- product1,product2,. . etc That is why I am concatenating the value of x with the attribute name within if statement. – Omi Feb 28 '14 at 12:34
  • How many products will be there like that `4` or more, and will that `Product1,Product2,Product3,Product4` will be in `datatable` rows – Developer Feb 28 '14 at 12:35
  • Also can you post the sample data what ur `datatable` consists – Developer Feb 28 '14 at 12:36
  • I have 5 Products attribute in my table- product1,product2,. . .,product5 Data from dynamic textbox is inserted into product2,. . ,product5 – Omi Feb 28 '14 at 12:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48686/discussion-between-dotnet-and-omi) – Developer Feb 28 '14 at 12:40
  • I have- `dt = g1.return_dt("select product2,product3,product4,product5 from tbl_products where name='Yo Yo'");` – Omi Feb 28 '14 at 12:40
  • Thanks a lot Sir, For your continuous support and very helpful suggestions. Thanks a ton sir :-) I got what I was expecting :-) Finally I am done with this problem. – Omi Feb 28 '14 at 18:04