Rowshan Ali

Rowshan Ali

  • NA
  • 64
  • 142.7k

how to insert from gridview textbox in asp.net.

Mar 8 2013 10:26 PM
 hello dear
i am in problems to insert data from gridview textbox. i am trying to general rule. but not success. i show my trial.
<table>
  <tr>
                    <td class="style5">
                        Work order no</td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                            HeaderStyle-BackColor="Red" HeaderStyle-ForeColor="White" Height="82px"
                            Width="470px">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chb1" runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="PRSL" HeaderText="PRSL" />
                                <asp:TemplateField HeaderText="Quantity">
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtQty"  Text='<%# Bind("RequiredQty") %>' runat="server">
                                        </asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                               <asp:BoundField DataField="Price" HeaderText="Price" />
                               <asp:BoundField DataField="Unit" HeaderText="Unit" />
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td class="style5">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style5">
                        &nbsp;</td>
                    <td>
                        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Save"
                            Width="58px" />
                    </td>
                </tr>
</table>

Work order no


 PRSLQuantityPriceUnit
4 50.0000KG
5 50.0000KG




database
WorkOrderNo varchar(50)primary key,
PRSL int ,
Qty int,
price money,
unit varchar(50)

code---

 protected void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('PR no can not be blank');", true);
        }
        else
        {
            string wono = ""; int prsl = 0, qty = 0; string unit = ""; decimal price = 0;
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                CheckBox Chbox = (CheckBox)row.FindControl("chb1");
                if (Chbox.Checked == true)
                {
                    select++;
                 
                }
            }

            if (select == 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('select checkbox for insert)');", true);
                return;
            }

            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                CheckBox Chbox = (CheckBox)row.FindControl("chb1");
                if (Chbox.Checked == true)
                {
                    for (int j = 1; j < GridView1.Rows[i].Cells.Count; j++)
                    {
                       
                        wono = TextBox1.Text;
                        //accsl = int.Parse(Label2.Text);
                        if (j == 1) prsl = Convert.ToInt32(GridView1.Rows[i].Cells[j].Text);
                        //if (j == 2) qty = Convert.ToInt32(GridView1.Rows[i].Cells[j].Text);

// my problem is that i can not insert data from gridview textbox.


                        TextBox txt = (TextBox)row.FindControl("txtQty");
                        //int ll = int.Parse(txt.Text);
                        int k = int.Parse(txt.Text);
                        if (j == 2) qty =Convert.ToInt32(GridView1.Rows[i].Cells[k].Text);                         if (j ==3 ) price = Convert.ToDecimal(GridView1.Rows[i].Cells[j].Text);
                        if (j ==4) unit = GridView1.Rows[i].Cells[j].Text;
                      
                    }
                    InsertData(wono,prsl, qty, price, unit);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Success fully added (PR...)');", true);

                }
            }
            TextBox1.Enabled = true;
            Button2.Visible = false;
        }
    }

please any body helpm me best regurds. thanks


Answers (1)