2

I have Grid View and Following Code In javascript to add new row. Problem is that when i click button to add new row it adds row but It contain the previous row data also and don't saves to DB. It generates error "Invalid Number".

<script type='text/javascript' language='javascript'>
        function AddNewRecord()
        {
            var grd = document.getElementById('GridView1');
            var tbod=grd.rows[0].parentNode;
            var newRow=grd.rows[grd.rows.length - 1].cloneNode(true);
            tbod.appendChild(newRow);
            return false;

        }
    </script>
<asp:Button ID="Button2" runat="server" Text="Add Row" OnClientClick="return AddNewRecord();"/>

What should i do so that row should generate empty and also should saves to DB.

I have this code for Grid

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" OnRowCreated="myGridView_ItemCreated"  Headerstyle-CssClass="myStyle"
                                BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="859px">
                                <RowStyle BackColor="White" ForeColor="#003399" HorizontalAlign="Right" />
                                <Columns>
                                    <asp:TemplateField HeaderText="Card No">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtCardNo" runat="server" Width="60px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Zone">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlZone" runat="server" DataSourceID="SqlDataSource2"
                                                DataTextField="ZONE_DESC" DataValueField="ZONE_NO" Width="80px">
                                            </asp:DropDownList>
                                            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT DISTINCT * FROM "MLK_00_05" order by Zone_DESC'>
                                            </asp:SqlDataSource>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Vehicle No">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlVeh" runat="server" DataSourceID="SqlDataSource1" DataTextField="VEHICLE_NUMBER"
                                                DataValueField="VEH_COD" Width="80px">
                                            </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT DISTINCT VEHICLE_NUMBER, DEVICEID, VEH_COD FROM MLK_VEHICLE ORDER BY 1'>
                                            </asp:SqlDataSource>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlStartDate" runat="server" Width="80px" AppendDataBoundItems="True">
                                            </asp:DropDownList>


                                        </ItemTemplate>
                                        <ItemStyle Wrap="False" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="HH  MM">
                                        <ItemTemplate>
                                            &nbsp;<asp:DropDownList ID="ddlStartHH" runat="server" Width="50px">
                                            </asp:DropDownList>&nbsp;<asp:DropDownList ID="ddlStartMM" runat="server" Width="50px">
                                            </asp:DropDownList>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" Width="800px" Wrap="False" />
                                        <HeaderTemplate>
                                            HH &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; MM
                                        </HeaderTemplate>
                                        <HeaderStyle Wrap="False" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date">
                                        <ItemTemplate>
                                            &nbsp;<asp:DropDownList ID="ddlEntryDate" runat="server" Width="80px">
                                            </asp:DropDownList>

                                        </ItemTemplate>
                                        <ItemStyle Wrap="False" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="hh:mm">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlEntryHH" runat="server" Width="50px">
                                            </asp:DropDownList>
                                            <asp:DropDownList ID="ddlEntryMM" runat="server" Width="50px">
                                            </asp:DropDownList>
                                        </ItemTemplate>
                                        <ItemStyle Wrap="False" />
                                        <HeaderTemplate>
                                            HH &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; MM
                                        </HeaderTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date">
                                        <ItemTemplate>
                                            &nbsp;<asp:DropDownList ID="ddlGrossDate" runat="server" Width="80px">
                                            </asp:DropDownList>

                                        </ItemTemplate>
                                        <ItemStyle Wrap="False" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="hh:mm">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlGrossHH" runat="server" Width="50px">
                                            </asp:DropDownList>
                                            <asp:DropDownList ID="ddlGrossMM" runat="server" Width="50px">
                                            </asp:DropDownList>
                                        </ItemTemplate>
                                        <ItemStyle Wrap="False" />
                                        <HeaderTemplate>
                                            HH &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; MM
                                        </HeaderTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Gross Weight(kg)">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtGrossWt" runat="server" Width="60px"></asp:TextBox>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Right" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Tare Weight(kg)">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtTareWt" runat="server" Width="60px" ></asp:TextBox>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Right" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Net Weight(kg)">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtNetWt" runat="server" Width="60px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Rate">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtRate" runat="server" Width="60px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date">
                                        <ItemTemplate>
                                            &nbsp;
                                            <asp:DropDownList ID="DdlTareDate" runat="server" Width="80px" >
                                            </asp:DropDownList>

                                        </ItemTemplate>
                                        <ItemStyle Wrap="False" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="hh:mm">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlTareHH" runat="server" Width="50px">
                                            </asp:DropDownList>
                                            <asp:DropDownList ID="ddlTareMM" runat="server" Width="50px">
                                            </asp:DropDownList>
                                        </ItemTemplate>
                                        <ItemStyle Wrap="False" />
                                        <HeaderTemplate>
                                            HH &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; MM
                                        </HeaderTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="FAT(%)">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtFat" runat="server" Width="50px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="LR">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtLR" runat="server" Width="50px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="SNF(%)">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtSNF" runat="server" Width="50px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Protein(%)">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtProtein" runat="server" Width="50px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="MBRT">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtMBRT" runat="server" Width="50px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Cheese">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtCheese" runat="server" Width="50px"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Distance">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtDistance" runat="server" Width="60px"></asp:TextBox>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Right" />
                                    </asp:TemplateField>
                                </Columns>
                                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                                <HeaderStyle BackColor="CornflowerBlue" Font-Bold="False" ForeColor="White" Font-Names="Arial" Font-Size="9pt" CssClass="myStyle" />
                            </asp:GridView>
1
  • Any One can Give answer to my question Commented Dec 14, 2011 at 5:10

2 Answers 2

2

since you are doing cloneNode(true) , it will just clone the previous row and hence have the same data as previous row. to make it empty use the .text("") with each element in row, since you have not included the html i cant write the code.

Sign up to request clarification or add additional context in comments.

4 Comments

Do U Mean HTML of Gridview or Button on which function is being called
can you please paste your one complete row element?
Yes I have now paste the Whole code I f u say i can paste my vb.net code for Inserting and loading Gridview
So what i have to do For adding a new row with empty texboxes and able to inserted in DB
0

For you scenario, You create a new row in gridview by javasscript, but the new row is not a server side control, So when do postback, The state of control can't save in viewstate,This is the reason why you can't save to DB.

So i think you can add a asp:hidden field in page , combine the data of new row(all columns) and store in it,when you click a button,It should trigger a postback event, you can get the data of new row by the value of this hidden field. and then save them to DB.

Comments

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.