我试图从DetailsView
控件中检索数据,以便将它们输入到另一个数据库表中。我已经查看了其他人提出的其他问题,但他们的问题都没有适合我的情况,即尝试访问SQL数据库。最终,我无法找到我需要的答案。
基本上,我的代码目标是提取DetailsView的第二行,因为第一行主要是不改变的静态头。我的DetailsView会在选择上面的GridView时发生变化。因此,我试图检索第二行的数据,解释我对行的使用[2],而我认为" Cells []"指的是列。
我使用以下代码来执行此操作。
string insertQuery = "insert into SalesOrders ([pQuotation#], pServices, pQty, pDescription, [p[Price/Unit]], pNetvalue, pProgress, pClientNo values (@quotationno,@services,@quantity,@description,@priceperunit,@netvalue,@progress,@clientno)";
cmd = new SqlCommand(insertQuery, con)
cmd.Parameters.AddWithValue("@quotationno", DetailsView1.Rows[0].Cells[1].Text);
cmd.Parameters.AddWithValue("@services", DetailsView1.Rows[1].Cells[1].Text);
cmd.Parameters.AddWithValue("@quantity", DetailsView1.Rows[2].Cells[1].Text);
cmd.Parameters.AddWithValue("@description", DetailsView1.Rows[3].Cells[1].Text);
cmd.Parameters.AddWithValue("@priceperunit", DetailsView1.Rows[4].Cells[1].Text);
cmd.Parameters.AddWithValue("@netvalue", DetailsView1.Rows[5].Cells[1].Text);
cmd.Parameters.AddWithValue("@progress", "0%");
cmd.Parameters.AddWithValue("@clientno", clientnolbl.Text.ToString());
我被抛出以下异常:
Error:System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index at System.Web.UI.ControlCollection.get_Item(Int32 index) at RealClientPurchase.Button1_Click(Object sender, EventArgs e) in d:\Desktop\RealClientPurchase.aspx.cs:line 27
编辑: 这是代码隐藏文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class RealClientPurchase : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
clientnolbl.Text = Session["sClientNo"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Desktop\TemporarySter\App_Data\legitdatabase.mdf;Integrated Security=True;Connect Timeout=30");
try
{
con.Open();
SqlCommand cmd;
string insertQuery = "insert into SalesOrders ([pQuotation#], pServices, pQty, pDescription, [p[Price/Unit]], pNetvalue, pProgress, pClientNo values (@quotationno,@services,@quantity,@description,@priceperunit,@netvalue,@progress,@clientno)";
cmd = new SqlCommand(insertQuery, con);
cmd.Parameters.AddWithValue("@quotationno", DetailsView1.Rows[0].Cells[1].Text);
cmd.Parameters.AddWithValue("@services", DetailsView1.Rows[1].Cells[1].Text);
cmd.Parameters.AddWithValue("@quantity", DetailsView1.Rows[2].Cells[1].Text);
cmd.Parameters.AddWithValue("@description", DetailsView1.Rows[3].Cells[1].Text);
cmd.Parameters.AddWithValue("@priceperunit", DetailsView1.Rows[4].Cells[1].Text);
cmd.Parameters.AddWithValue("@netvalue", DetailsView1.Rows[5].Cells[1].Text);
cmd.Parameters.AddWithValue("@progress", "0%");
cmd.Parameters.AddWithValue("@clientno", clientnolbl.Text.ToString());
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect",
"alert('Purchase order has been submitted! Kindly proceed to checkout.'); window.location='" +
Request.ApplicationPath + "CurrentCheckout.aspx';", true);
con.Close();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
}
这些是aspx代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/ClientPortal.master" AutoEventWireup="true" CodeFile="RealClientPurchase.aspx.cs" Inherits="RealClientPurchase" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Register1ConnectionString %>" SelectCommand="SELECT [qQuotationNo], [qServices], [qQuantity], [qDate] FROM [Quotations] WHERE ([qClientNo] = @qClientNo)">
<SelectParameters>
<asp:SessionParameter Name="qClientNo" SessionField="sClientNo" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Register1ConnectionString %>" SelectCommand="SELECT [qQuotationNo], [qServices], [qQuantity], [qDescription], [qPricePerQty], [qTotalPayable] FROM [Quotations] WHERE ([qQuotationNo] = @qQuotationNo)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="qQuotationNo" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="clientnolbl" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="qQuotationNo" DataSourceID="SqlDataSource1" Height="125px" Width="452px" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="qQuotationNo" HeaderText="Quotation No" InsertVisible="False" ReadOnly="True" SortExpression="qQuotationNo" />
<asp:BoundField DataField="qServices" HeaderText="Services" SortExpression="qServices" />
<asp:BoundField DataField="qQuantity" HeaderText="Quantity" SortExpression="qQuantity" />
<asp:BoundField DataField="qDate" HeaderText="Date" SortExpression="qDate" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource2" Height="50px" Width="452px" CellPadding="4" ForeColor="#333333" GridLines="None" DataKeyNames="qQuotationNo">
<AlternatingRowStyle BackColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="qQuotationNo" HeaderText="qQuotationNo" SortExpression="qQuotationNo" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="qServices" HeaderText="qServices" ReadOnly="True" SortExpression="qServices" />
<asp:BoundField DataField="qQuantity" HeaderText="qQuantity" SortExpression="qQuantity" ReadOnly="True" />
<asp:BoundField DataField="qDescription" HeaderText="qDescription" SortExpression="qDescription" ReadOnly="True" />
<asp:BoundField DataField="qPricePerQty" HeaderText="qPricePerQty" SortExpression="qPricePerQty" ReadOnly="True" />
<asp:BoundField DataField="qTotalPayable" HeaderText="qTotalPayable" SortExpression="qTotalPayable" ReadOnly="True" />
</Fields>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
</asp:DetailsView>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<br />
<br />
<br />
</asp:Content>
答案 0 :(得分:4)
我找到了解决方案。
我更改了数据库表中的字段以排除&#34;#&#34;和&#34; [&#34;或&#34;]&#34; 看来这些标志在SQL语句中不能很好地运行。 改进后的代码如下,它们确实有效。
SqlCommand cmd;
string insertQuery = "INSERT INTO SalesOrders (pQuotationNo, pServices, pQty, pDescription, pPricePerQty, pNetvalue, pProgress, pClientNo) values (@quotationno,@services,@quantity,@description,@priceperunit,@netvalue,@progress,@clientno)";
cmd = new SqlCommand(insertQuery, con);
cmd.Parameters.AddWithValue("@quotationno", DetailsView1.Rows[0].Cells[1].Text.ToString());
cmd.Parameters.AddWithValue("@services", DetailsView1.Rows[1].Cells[1].Text.ToString());
cmd.Parameters.AddWithValue("@quantity", DetailsView1.Rows[2].Cells[1].Text.ToString());
cmd.Parameters.AddWithValue("@description", DetailsView1.Rows[3].Cells[1].Text.ToString());
cmd.Parameters.AddWithValue("@priceperunit", DetailsView1.Rows[4].Cells[1].Text.ToString());
cmd.Parameters.AddWithValue("@netvalue", DetailsView1.Rows[5].Cells[1].Text.ToString());
cmd.Parameters.AddWithValue("@progress", "0%");
cmd.Parameters.AddWithValue("@clientno", clientnolbl.Text.ToString());
cmd.ExecuteNonQuery();
答案 1 :(得分:0)
你能试试DetailsView1.Rows[0].Cells[0].Text
吗?
由于您的DetailsView
只有BoundFields
,这应该可以。
此致