插入数据库

时间:2016-02-12 15:29:00

标签: c# asp.net sql-server gridview

我有一个基本上是这样的网络表单,

enter image description here

我的页面有LABELS,BUTTONS,GRIDVIEW,TEXTBOXES

显示的数据来自另一个数据库

每当我尝试INSERT到另一个数据库时(再次) 错误发生说:

  

表'PO2'中标识列的显式值只能是   使用列列表且IDENTITY_INSERT为ON时指定。

我正在使用Sql server

here

ASPNET代码:

 <table class="col-lg-12">

    <tr>
        <td class="style5">  
        Shipping Method</td>
        <td class="style6">Shipping Term</td>
        <td class="style6">Payment Term</td>
        <td class="style4">Delivery Date</td>
         <td class="style4">Final Delivery Date</td>
    </tr>

    <tr>
        <td> 
            <asp:Label ID="lbShippingMethod" runat="server" BorderColor="Black" Font-Size="Larger" /></td>
        <td> 
            <asp:Label ID="lbShippingTerm" runat="server" BorderColor="Black" Font-Size="Larger" /></td>

        <td> 
            <asp:Label ID="lbPaymentTerm" runat="server" BorderColor="Black" Font-Size="Larger" /></td>
        <td> 
            <asp:Label ID="lbDeliveryDate" runat="server" BorderColor="Black" Font-Size="Larger" /></td>
        <td> 
           <asp:Textbox ID="txtDate" runat="server" BorderColor="Black" Font-Size="Larger" type="date" /></td>
    </tr>

        <tr>
            <td>
                <br />
                <br />
            </td>
        </tr>

        </div>
    </table>


<div class="container">
<div class="col-lg-10 pull-right">
 <table class="col-lg-12">

<tr>  
     <td>
         PR#</td> 


    <td class="style2">  
        Product Name  
    </td> 
    <td>Product#</td>

    <td>Price</td>

    <td>  
        Quantity  
    </td> 


</tr>  
<tr>  

            <td>
                <asp:Label ID="PRID" runat="server" BorderColor="Black" Font-Size="Larger" />
           </td>

           <td>
                <asp:DropDownList ID="ddlName" runat="server" class="form-control" 
                    Width="150px" onselectedindexchanged="ddlName_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
           </td>

 <td class="style2">   
    <asp:Label ID="lbProductID" runat="server" Width="90px"></asp:Label>
 </td> 


  <td class="style2">   
    <asp:Label ID="lbPrice" runat="server"></asp:Label>
     </td>  

      <td>  
      <asp:TextBox ID="Quantity" runat="server" Width="90px" Type="Number"  required></asp:TextBox>  
      </td>

      <td>  
      <asp:Label ID="Amount" runat="server" Width="90px" Type="Number" required> </asp:Label>  
  </td>


    <div class="pull-right">  
        <td><asp:Button ID="btnAdd" runat="server" class="btn btn-default" 
            style="background-color:Silver" text="Add Product"   ForeColor="Black" 
            onclick="btnAdd_Click" />
    </td>
    </div>
    </tr>
    </table>
    </div>  
    </div>
    <br />



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"   Width="1000px" HorizontalAlign="Center"
            DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="PRID" HeaderText="PRID" SortExpression="PRID"  />
        <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
            SortExpression="ProductID" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
            SortExpression="ProductName" />
        <asp:BoundField DataField="Price" HeaderText="Price"   SortExpression="Price" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
        SortExpression="Quantity" />
        <asp:BoundField DataField="Amount" HeaderText="Amount" 
            SortExpression="Amount" />
    </Columns>
</asp:GridView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$  ConnectionStrings:MyOwnMeatshopConnectionString %>" 
        SelectCommand="SELECT [PRID], [ProductID], [ProductName], [Price],  [Quantity], [Amount] FROM [PRDetails] WHERE ([PRID] = @PRID)">
        <SelectParameters>
            <asp:QueryStringParameter Name="PRID" QueryStringField="ID"  Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>


   <div class="container">
    <div class="pull-right col-lg-5">
        <h4 />SUB TOTAL Php   <asp:Label ID="lbSubTotal" runat="server"></asp:Label>
        <h4 />VAT (12%) 
        Php   <asp:Literal ID="ltVAT" runat="server"></asp:Literal>
        <h4 />TOTAL Php    <asp:Literal ID="ltTotal" runat="server">    </asp:Literal>
     </div>
   </div>  


   <div class="pull-right">  
    <td>
     <asp:Button ID="btnSum" runat="server" class="btn btn-default"  style="background-color:Silver" text="TOTAL" ForeColor="Black" />
    </td>
   </div>

   <br />
   <br />
   <br />
   <br />

     <div class="form-group">
            <label class="control-label col-lg-4">Remarks</label>
            <div class="col-lg-8">
                <asp:TextBox ID="txtRemarks" runat="server" class="form- control" MaxLength="100" TextMode="MultiLine" />
            </div>
        </div>

    <div class="pull-right"> 
     <div class="form-group">
     <asp:Button ID="btnApprove" runat="server" class="btn btn-success"  text="Approve" 
             onclick="btnApprove_Click" />
            <asp:Button ID="btnCancel" runat="server" class="btn"    style="color:White" text="Disapprove" BackColor="Black"  PostBackUrl="~/Default.aspx" />


            </div>
        </div>
</div>

    </form>

这里是BTN代码(APPROVE)

void addtoPO()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "INSERT INTO PO2 VALUES (@UserID, @SupplierID, @CompanyName, @ShippingMethod, " +
            "@ShippingTerm, @Term, @DeliveryDate, @ModifiedBy, @DateModified, @Status, @Remarks, @PODate, @DeliveryDate)";
        cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
        cmd.Parameters.AddWithValue("@SupplierID", lbSupplierID.Text);
        cmd.Parameters.AddWithValue("@CompanyName", lbCompName.Text);

        cmd.Parameters.AddWithValue("@ShippingMethod", lbShippingMethod.Text);
        cmd.Parameters.AddWithValue("@ShippingTerm", lbShippingTerm.Text);
        cmd.Parameters.AddWithValue("@Term", lbPaymentTerm.Text);

        cmd.Parameters.AddWithValue("@ModifiedBy", lbFNA.Text + lbLNA.Text);
        cmd.Parameters.AddWithValue("@DateModified", DateTime.Now);
        cmd.Parameters.AddWithValue("@Status", "Approved");
        cmd.Parameters.AddWithValue("@Remarks", txtRemarks.Text);

        cmd.Parameters.AddWithValue("@PODate", DateTime.Now);
        cmd.Parameters.AddWithValue("@DeliveryDate", txtDate.Text);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        addtoPO();

        foreach (GridViewRow row in GridView1.Rows)
        {

            if (row.RowType == DataControlRowType.DataRow)
            {


                int ProductID = int.Parse(GridView1.Rows[row.RowIndex].Cells[1].Text);
                string ProductName = GridView1.Rows[row.RowIndex].Cells[2].Text;
                decimal Price = decimal.Parse(GridView1.Rows[row.RowIndex].Cells[3].Text);
                int Quantity = int.Parse(GridView1.Rows[row.RowIndex].Cells[4].Text);
                int Amount = int.Parse(GridView1.Rows[row.RowIndex].Cells[5].Text);

                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "INSERT INTO PODetails (ProductID, Name, Price, Quantity, Amount) " +
                     "VALUES (@ProductID, @Name, @Price, @Quantity, @Amount)";

                //cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
                cmd.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = ProductID;
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = ProductName.ToString();
                cmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = Price;
                cmd.Parameters.Add("@Quantity", SqlDbType.Int).Value = Quantity;
                cmd.Parameters.Add("@Amount", SqlDbType.Int).Value = Amount;

                cmd.ExecuteNonQuery();



                GridView1.EditIndex = -1;
                GridView1.DataBind();

                //con.Open();

                //cmd.CommandText = "DELETE FROM Orders";
                //cmd.Parameters.Add("@RefNo", RefNo);


                con.Close();

            }
        }

我正在插入两个不同的表,这就是为什么插入是分开的,其他值正在GET中进入GRIDVIEW。

我打开和关闭身份,但没有工作。

我拥有的其他插件都很好,这只是一个有错误。

2 个答案:

答案 0 :(得分:1)

insert into TABLE ([column list])
values ([values list])

您没有列出插入列列表,因此服务器尝试完成所有列&#34;从左到右&#34;。下一个错误是关于为表列提供的值不够。目前它试图将@UserID发布到PO2.ID,@ SupplierID发布到PO2.UserID等等。

答案 1 :(得分:0)

谢谢你的回答让人非常感激! 请原谅我的问题,因为我对硬编码c#和asp.net

非常陌生

我只是按照@Ivan Starostin先生的回答

 cmd.CommandText = "INSERT INTO PO2 (UserID, SupplierID, CompanyName, ShippingMethod, " +
            "ShippingTerm, Term, ModifiedBy, DateModified, Status, Remarks, PODate, DeliveryDate, FinalDeliveryDate, PRID) VALUES (@UserID, @SupplierID, @CompanyName, @ShippingMethod, " +
            "@ShippingTerm, @Term, @ModifiedBy, @DateModified, @Status, @Remarks, @PODate, @DeliveryDate, @FinalDeliveryDate, @PRID)";

这样做并修复了一些列插入语句并且工作了!