我有一个页面在下面。
我有2个GridView
Labes&文本框
和一个按钮
RECEIVED btn 点击时, 它保存在正确的数据库和我设置的其他命令中。
但我的命令if(Request.QueryString [“Status”] ==“收到”),
不工作并且“状态”表示/ null
但它甚至在我的数据库中也有价值
并由我设置的命令更新。
这是ASP.NET代码:
<div class="col-lg-12">
<center>
<br />
<i class="fa fa-user" style="font-size: xx-large;"> Receiving</i> <br /><br />
<i> See below the PO Details & Status</i><br /><br />
<div id="error2" runat="server" class="alert alert-danger" visible="false">
This Purchase Order had already been received.
</div>
<asp:Label runat="server" ID="Label1" Font-Bold="True"> PR By:</asp:Label>
<asp:Label runat="server" ID="txtPRBy"></asp:Label>
<asp:Label runat="server" ID="Label2" Font-Bold="True"> Approved By:</asp:Label>
<asp:Label runat="server" ID="txtOrderBy"></asp:Label>
<asp:Label runat="server" ID="Label3" Font-Bold="True"> PO Date:</asp:Label>
<asp:Label runat="server" ID="txtPODate"></asp:Label>
<asp:Label runat="server" ID="Label4" Font-Bold="True"> Final Delivery Date:</asp:Label>
<asp:Label runat="server" ID="txtFinalDelDate"></asp:Label>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ReceivingPO">
<Columns>
<asp:BoundField DataField="POID" HeaderText="POID" SortExpression="POID" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity" />
<asp:BoundField DataField="Amount" HeaderText="Amount"
SortExpression="Amount" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="ReceivingPO" runat="server"
ConnectionString="<%$ ConnectionStrings:MyOwnMeatshopConnectionString %>"
SelectCommand="SELECT PODetails.POID, PODetails.ProductID, PODetails.Name, PODetails.Price, PODetails.Quantity, PODetails.Amount, PO.Status FROM PODetails INNER JOIN PO ON PODetails.POID = PO.POID WHERE (PODetails.POID = @POID)">
<SelectParameters>
<asp:QueryStringParameter Name="POID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</center>
<asp:Button ID="btnApprove" runat="server" class="btn btn-success" text="Receive"
onclick="btnApprove_Click" />
<center><i> See below the PO Receiving Details</i><br /><br />
<asp:Label runat="server" ID="Label5" Font-Bold="True">Received By:</asp:Label>
<asp:Label runat="server" ID="txtFN"></asp:Label>
<asp:Label runat="server" ID="txtLN"></asp:Label>
<asp:Label runat="server" ID="Label6" Font-Bold="True"> Remarks:</asp:Label>
<asp:TextBox runat="server" ID="txtRemarks" Width="300px"></asp:TextBox>
<br />
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataSourceID="received" DataKeyNames="ReceivingID" >
<Columns>
<asp:BoundField DataField="ReceivingID" HeaderText="ReceivingID"
SortExpression="ReceivingID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="POID" HeaderText="POID" SortExpression="POID" />
<asp:BoundField DataField="ReceivedBy" HeaderText="ReceivedBy"
SortExpression="ReceivedBy" />
<asp:BoundField DataField="DateReceived" HeaderText="DateReceived"
SortExpression="DateReceived" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="received" runat="server"
ConnectionString="<%$ ConnectionStrings:MyOwnMeatshopConnectionString %>"
SelectCommand="SELECT Receiving.ReceivingID, Receiving.POID, Receiving.ReceivedBy, Receiving.DateReceived, Receiving.Status FROM Receiving INNER JOIN PO ON Receiving.POID = PO.POID WHERE (Receiving.POID = @POID)">
<SelectParameters>
<asp:QueryStringParameter Name="POID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</center>
<br />
<br />
</div>
这里是代码(btn收到)
protected void btnApprove_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (Request.QueryString["Status"] == "Received")
{
error2.Visible = true;
}
else
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string ProductID = GridView1.Rows[row.RowIndex].Cells[1].Text;
int Quantity = int.Parse(GridView1.Rows[row.RowIndex].Cells[4].Text);
cmd.CommandText = "INSERT INTO Receiving VALUES (@POID, @RequestedBy, " +
"@OrderBy, @PODate, @FinalDeliveryDate, @DateReceived, @ReceivedBy, @Status, @Remarks)";
cmd.Parameters.AddWithValue("@POID", Request.QueryString["ID"].ToString());
cmd.Parameters.AddWithValue("@RequestedBy", txtPRBy.Text);
cmd.Parameters.AddWithValue("@OrderBy", txtOrderBy.Text);
cmd.Parameters.AddWithValue("@PODate", txtPODate.Text);
cmd.Parameters.AddWithValue("@FinalDeliveryDate", txtFinalDelDate.Text);
cmd.Parameters.AddWithValue("@DateReceived", DateTime.Now);
cmd.Parameters.AddWithValue("@ReceivedBy", txtLN.Text + txtFN.Text);
cmd.Parameters.AddWithValue("@Status", "Received");
cmd.Parameters.AddWithValue("@Remarks", txtRemarks.Text);
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE PO SET Status=@Status WHERE POID=@POID";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE Products SET Available+=@Quantity WHERE ProductID=@ProductID";
cmd.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = ProductID.ToString();
cmd.Parameters.AddWithValue("@Quantity", Quantity);
cmd.ExecuteNonQuery();
GridView1.DataBind();
con.Close();
}
}
}
}
在我的其他页面中有效。但这里没有。
帮助伙伴们提前感谢你们!
我只是不知道为什么会发生
我也在页面加载中设置代码
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["ID"] != null)
{
int POID = 0;
bool validpurch = int.TryParse(Request.QueryString["ID"].ToString(), out POID);
if (validpurch)
{
if (!IsPostBack)
{
getPO();
GetUser();
}
}
else
Response.Redirect("Default.aspx");
}
else
Response.Redirect("Default.aspx");
}
答案 0 :(得分:2)
我会捅这个。如果我理解正确,您希望显示错误消息,如果GridView1状态中的产品==&#34;收到&#34;
,则不执行UPDATE。如果是这种情况,您需要从网格中获取该值。它不存在于查询字符串中。像这样:
protected void btnApprove_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string status = GridView1.Rows[row.RowIndex].Cells[6].Text;
if (status == "Received")
{
error2.Visible = true;
}
else
{
string ProductID = GridView1.Rows[row.RowIndex].Cells[1].Text;
int Quantity = int.Parse(GridView1.Rows[row.RowIndex].Cells[4].Text);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO Receiving VALUES (@POID, @RequestedBy, " +
"@OrderBy, @PODate, @FinalDeliveryDate, @DateReceived, @ReceivedBy, @Status, @Remarks)";
cmd.Parameters.AddWithValue("@POID", Request.QueryString["ID"].ToString());
cmd.Parameters.AddWithValue("@RequestedBy", txtPRBy.Text);
cmd.Parameters.AddWithValue("@OrderBy", txtOrderBy.Text);
cmd.Parameters.AddWithValue("@PODate", txtPODate.Text);
cmd.Parameters.AddWithValue("@FinalDeliveryDate", txtFinalDelDate.Text);
cmd.Parameters.AddWithValue("@DateReceived", DateTime.Now);
cmd.Parameters.AddWithValue("@ReceivedBy", txtLN.Text + txtFN.Text);
cmd.Parameters.AddWithValue("@Status", "Received");
cmd.Parameters.AddWithValue("@Remarks", txtRemarks.Text);
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE PO SET Status=@Status WHERE POID=@POID";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE Products SET Available+=@Quantity WHERE ProductID=@ProductID";
cmd.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = ProductID.ToString();
cmd.Parameters.AddWithValue("@Quantity", Quantity);
cmd.ExecuteNonQuery();
GridView1.DataBind();
con.Close();
}
}
}
}