You must Sign In to post a response.
  • Category: ASP.NET

    How to bind data in gridview dynamic text box from database table

    Hi , Developers

    I am creating project , in current module am using gridview dynamic textbox. I have finished inser,validation, bind but i am struggle with bind record to gridview dynamic textbox from database table.

    My problem is bind some particular records into dynamic textbox on pageload. So how i am create object to the particular textbox and how to bind it.

    Text example box code

    <asp:TemplateField HeaderText="EXAMPLE">

    <ItemTemplate>
    <asp:TextBox ID="t3" runat="server" Enabled="false" Text='<%#Eval("Example") %>' Width="100px" Font-Bold="true" ForeColor="Black" />
    </ItemTemplate>
    </asp:TemplateField>


    My current using code

    public void Bind_FRsheetDetails()
    {

    Control control = null;
    if (GridView1.FooterRow != null)
    {
    control = GridView1.FooterRow;
    }
    else
    {
    control = GridView1.Controls[0].Controls[0];
    }
    DataSet ds = ObjFRList.Bind_FRsheetList(fr_id);
    if (ds.Tables[0].Rows.Count > 0)
    {
    lblEnquiryId.Text = ds.Tables[0].Rows[0]["enquiryid"].ToString();
    lblfr_no.Text = ds.Tables[0].Rows[0]["fr_id"].ToString();
    string t1 = (control.FindControl("t1") as TextBox).Text;
    t1 = ds.Tables[0].Rows[0]["boardname"].ToString();
    string t3 = (control.FindControl("t3") as TextBox).Text;
    t3 = ds.Tables[0].Rows[0]["layercount"].ToString();
    string t4 = (control.FindControl("t4") as TextBox).Text;
    t4 = ds.Tables[0].Rows[0]["material"].ToString();
    string t5 = (control.FindControl("t5") as TextBox).Text;
    t5 = ds.Tables[0].Rows[0]["tg"].ToString();
    string t6 = (control.FindControl("t6") as TextBox).Text;
    t6 = ds.Tables[0].Rows[0]["board_thickness_in_mm"].ToString();
    string t7 = (control.FindControl("t7") as TextBox).Text;
    t7 = ds.Tables[0].Rows[0]["inner_copper_in_ounze"].ToString();
    string t8 = (control.FindControl("t8") as TextBox).Text;
    t8 = ds.Tables[0].Rows[0]["outer_copper_in_ounze"].ToString();
    string t9 = (control.FindControl("t9") as TextBox).Text;
    t9 = ds.Tables[0].Rows[0]["finishing"].ToString();
    string t10 = (control.FindControl("t10") as TextBox).Text;
    t10 = ds.Tables[0].Rows[0]["masking"].ToString();
    string t11 = (control.FindControl("t11") as TextBox).Text;
    t11 = ds.Tables[0].Rows[0]["legend"].ToString();
    string t12 = (control.FindControl("t12") as TextBox).Text;
    t12 = ds.Tables[0].Rows[0]["extras"].ToString();
    string t13 = (control.FindControl("t13") as TextBox).Text;
    t13 = ds.Tables[0].Rows[0]["size_in_mm"].ToString();
    string t14 = (control.FindControl("t14") as TextBox).Text;
    t14 = ds.Tables[0].Rows[0]["quantity"].ToString();
    }
    }

    in the about code it throws following error .

    Object reference not set to an instance of an object.


    Line 65: lblEnquiryId.Text = ds.Tables[0].Rows[0]["enquiryid"].ToString();
    Line 66: lblfr_no.Text = ds.Tables[0].Rows[0]["fr_id"].ToString();
    Line 67: string t1 = (control.FindControl("t1") as TextBox).Text;
    Line 68: t1 = ds.Tables[0].Rows[0]["boardname"].ToString();
    Line 69: string t3 = (control.FindControl("t3") as TextBox).Text;

    Source File: f:\Paul Working Projects\PAUL PROTO MODIFIED 22-1-2016\Proto circuits Paul 22-1-2016\Tracking\AdminSettings\CSWOC.aspx.cs Line: 67

    So please help me to how i am creating object and how to bind it the nescessary text boxes.

    Thanking you
    With Paul.S
  • #764257

    Your textbox id in ItemTemplate is 't3' why you are accessing it with 't1'. correct your code as follows

    var rows = myGridView.Rows;
    foreach(GridViewRow row in rows)
    {
    TextBox t = (TextBox) row.FindControl("t3");
    t.Text = "Some Value";
    }
    do not increment value of textbox control id, use 't3' each time


    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764259
    Thanks mr.Prasad. I got exactly output.

    Thanks with
    Paul.S

  • #764269
    Hai Paul,
    Good to see that you got exact output but the approach you followed is not correct.
    If you want to bind any data from database to gridview textbox, you need to do it in RowDataBound which is best method to bind any controls during runtime.
    There are 2 event handlers which are used if you have controls inside the gridview:
    1. RowCreated event handler - When the row is getting created for the GridView, you can add the controls dynamically using this event handler.
    2. RowDataBound event handler: When the control is already there and you want to fill the data, this event handler can be used.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764293
    Hi Paul,

    As per your query you want to bind the database result into your gridview textbox control, for that already you are binded in designer itself like "Text='<%#Eval("Example") %>'",

    so why are you looking to bind in code behind again?

    Choose either any one of this, may be in some cases if you want to update the result then you can use in both cases, but until unless don't use 2 ways to bind, if you want to bind it from code behind in page load use OnRowDataBound event of gridview and find the gridview textbox control and assign the database value to that.

    Refer below sample

    protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    if(e.Row.RowType== DataControlRowType.DataRow)
    {
    TextBox txt=(TextBox)e.Row.FindControl("t3");
    txt.Text="Your Text";
    }
    }


    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764314
    Yes Mr.Naveen you are right.

    but i want to bind records from databse on pageload instead of that i also inserted the same data which is bind the textbox on the pageload.

    i am tried many types i got output but i can't able to get exact output.

    1.if i have 10 textbox na first 5 textbox values shold be bind from the database .
    2.other 5 textbox values sould be inserted by the user .
    3.If i am click the Insert button na the values of 5 binded and 5 insertedbyuser 10 textbox values will be inserted into database table.
    4.Bind all records.

    Above is my requirement. Mr.Naveen

    Thanks with
    Paul.S

  • #764318
    Hi Paul,

    For binding database records you simply use it in your design code

    "Text='<%#Eval("Example") %>'", this is enough for database data.

    For entering value into textbox and save it into database for that you need to do it separate code logic,

    You can use OnRowCommand Event of gridview to perform this action, while click on button you simply call the command event and wrote your code inside that as per your need and submit time you just send those entry records and save it into database for future reference purpose.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments