Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert data from grid view to sql server
    primarykey
    data
    text
    <p>I am doing project on asp.net. Right now I have got problem with insert data from gridview to sql server. In my gridview I have add a template as a textbox in order to input value. My problem is about insert value from textbox of gridview to sql server. My code as below:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/coca.Master" AutoEventWireup="true" CodeBehind="Orders.aspx.cs" Inherits="AssignmentWeb.Orders" %&gt; &lt;%@ Import Namespace="System.Data" %&gt; &lt;%@ Import Namespace="System.Data.SqlClient" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt; &lt;div class="center"&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function () { $("[id*=txtQty]").val("0"); }); $("[id*=txtQty]").live("change", function () { if (isNaN(parseInt($(this).val()))) { $(this).val('0'); } else { $(this).val(parseInt($(this).val()).toString()); } }); $("[id*=txtQty]").live("keyup", function () { if (!jQuery.trim($(this).val()) == '') { if (!isNaN(parseFloat($(this).val()))) { var row = $(this).closest("tr"); $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val())); } } else { $(this).val(''); } var grandTotal = 0; $("[id*=lblTotal]").each(function () { grandTotal = grandTotal + parseFloat($(this).html()); }); $("[id*=lblGrandTotal]").html(grandTotal.toString()); }); &lt;/script&gt; &lt;br /&gt; &lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" EnableModelValidation="True"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" /&gt; &lt;asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /&gt; &lt;asp:BoundField DataField="AverageProduct" HeaderText="AverageProduct" SortExpression="AverageProduct" ItemStyle-CssClass="price"/&gt; &lt;asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /&gt; &lt;asp:TemplateField HeaderText="QtyOrder"&gt; &lt;ItemTemplate&gt; &lt;asp:Textbox ID="txtQty" runat="server"&gt;&lt;/asp:Textbox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Total"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblTotal" runat="server"&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; Grand Total: &lt;asp:Label ID="lblGrandTotal" runat="server" Text="0"&gt;&lt;/asp:Label&gt; &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:InventoryRouting %&gt;" SelectCommand="SELECT [ProductName], [AverageProduct], [Description], [ProductID] FROM [Product]"&gt;&lt;/asp:SqlDataSource&gt; &amp;nbsp;&lt;br /&gt; &lt;asp:Button ID="btnOrder" runat="server" Text="Order!" Width="100px" OnClick="btnOrder_Click"/&gt; &lt;br /&gt; &lt;br /&gt; &lt;/div&gt; &lt;/asp:Content&gt; &lt;script runat="server"&gt; public void btnOrder_Click(object sender, EventArgs e) { int c = 0; for (int i = 0; i &lt; GridView1.Rows.Count; i++) { GridViewRow row = GridView1.Rows[i]; SqlCommand cmd = new SqlCommand(); cmd.Connection = new SqlConnection("Data Source=LIDA-PC; Initial Catalog=InventoryRouting; Integrated Security=True"); //cmd.CommandText = "insert into orders values('" + Session["Username"].ToString() + "',@ProductID, @ProductName, @QtyOrder, @Total)"; cmd.CommandText = "insert into orders values('" + Session["Username"].ToString() + "',@ProductID , @ProductName, @QtyOrder, @Total)"; cmd.Connection .Open(); //cmd.Parameters.AddWithValue("'" + Session["Username"].ToString() + "'", GridView1.Rows[i].Cells[1].Text); cmd.Parameters.AddWithValue("@ProductID", GridView1.Rows[i].Cells[0].Text); cmd.Parameters.AddWithValue("@ProductName", GridView1.Rows[i].Cells[1].Text); cmd.Parameters.AddWithValue("@QtyOrder", GridView1.Rows[i].Cells[4].Text); cmd.Parameters.AddWithValue("@Total", GridView1.Rows[i].Cells[5].Text); //InsertCommand = new SqlCommand("INSERT INTO [orders] ([client], [product], [amount], [price]) VALUES ('" + Session["Username"].ToString() + "', @ProductName, @AverageProduct, @QtyOrder, @Total)"); cmd.ExecuteNonQuery(); cmd.Connection.Close(); c = c + 1; } } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload