更新查询未正确触发。更新未反映

时间:2014-05-02 16:39:49

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

我的代码隐藏文件是UpdatePublication.aspx.cs

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;
using System.Data;

namespace SBAIMS
{
    public partial class UpdatePublisher : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["P_Id"] != null)
            {
                string s = Request.QueryString["P_Id"].ToString();
                SqlConnection con = new SqlConnection(@"Data Source=AP\sqlexpress;Initial Catalog=SBAIMS;Integrated Security=True");
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from Publication_Master where P_Id='" + s + "' ", con);
                SqlDataAdapter adptr = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adptr.Fill(ds, "Publication_Master");
                if (ds.Tables["Publication_Master"].Rows.Count > 0)
                {
                    upub.Text = ds.Tables["Publication_Master"].Rows[0]["Publication"].ToString();
                    upname.Text = ds.Tables["Publication_Master"].Rows[0]["Publisher"].ToString();
                    upc1.Text = ds.Tables["Publication_Master"].Rows[0]["P_Contact"].ToString();
                    upc2.Text = ds.Tables["Publication_Master"].Rows[0]["P_Contact2"].ToString();
                    upemail.Text = ds.Tables["Publication_Master"].Rows[0]["P_Email"].ToString();
                    upban.Text = ds.Tables["Publication_Master"].Rows[0]["P_BankAccount"].ToString();
                }
                con.Close();
            }


        }
        protected void submitupdatepub_Click(object sender, EventArgs e)
        {

                string s = Request.QueryString["P_Id"].ToString();
                SqlConnection con2 = new SqlConnection(@"Data Source=AP\sqlexpress;Initial Catalog=SBAIMS;Integrated Security=True");
                con2.Open();
                SqlCommand cmd2 = new SqlCommand("UPDATE Publication_Master SET Publication='" + upub.Text + "', Publisher='" + upname.Text + "', P_Contact='" + upc1.Text + "', P_Contact2='" + upc2.Text + "', P_Email='" + upemail.Text + "', P_BankAccount='" + upban.Text + "' WHERE P_Id='"+s+"'", con2);
                cmd2.ExecuteNonQuery();         
                con2.Close();
                Response.Redirect("~/UPublisher.aspx");

        }
    }
}

我的ASPX文件是UpdatePublication.aspx

<%@ Page Title="Update Publication" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
CodeBehind="UpdatePublication.aspx.cs" Inherits="SBAIMS.UpdatePublisher" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="updatepublisher">
        <table width="100%" class="table table-borderless table-responsive">
            <tr>
                <td width="50%" style="vertical-align: top;">
                    <div class="panel panel-success" id="updatepub">
                        <div class="panel-heading">
                            <h4>
                                <b>Update Publication.</b></h4>
                        </div>
                        <div class="panel-body">
                            <h5>
                                Update data about Publication.</h5>
                            <br />
                            <div align="center">
                                <asp:Label ID="updatepubsubmit" runat="server" Font-Size="Medium" Text="Update Was Successful !!"
                                    CssClass="alert alert-success alcntr" Visible="False"></asp:Label>
                            </div>
                            <br />
                            <br />
                            <table class="table table-borderless table-responsive fs15" align="center" style="width: 500px; background-color: transparent;">
                           <tr>
                                    <td width="40%">
                                        Publication Name
                                    </td>
                                    <td width="60%">
                                        <asp:TextBox ID="upub" CssClass="form-control fs15" autocomplete="off" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="* Field Required"
                                            ControlToValidate="upub" Display="Dynamic" SetFocusOnError="True" Font-Size="Medium"
                                            CssClass="rfvlabel" ValidationGroup="rfvup"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Publisher (/Distributor) Name
                                    </td>
                                    <td>
                                        <asp:TextBox ID="upname" CssClass="form-control fs15" autocomplete="off" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="* Field Required"
                                            ControlToValidate="upname" Display="Dynamic" SetFocusOnError="True" Font-Size="Medium"
                                            CssClass="rfvlabel" ValidationGroup="rfvup"></asp:RequiredFieldValidator>
                                              <asp:RegularExpressionValidator ID="RegularExpressionValidator9" runat="server" ErrorMessage="* Enter characters or valid symbols" ControlToValidate="upname" Display="Dynamic" SetFocusOnError="True" Font-Size="Medium"
                                            CssClass="rfvlabel" ValidationExpression="^[a-zA-Z''-'\s]{1,40}$" ValidationGroup="rfvup"></asp:RegularExpressionValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Contact No. 1
                                    </td>
                                    <td>
                                        <asp:TextBox ID="upc1" CssClass="form-control fs15" autocomplete="off" placeholder="optional" runat="server"></asp:TextBox>
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator10" runat="server" ErrorMessage="* Numbers Only" 
                                            ControlToValidate="upc1" Display="Dynamic" SetFocusOnError="True" Font-Size="Medium"
                                            CssClass="rfvlabel" ValidationExpression="^([0-9]*)$" ValidationGroup="rfvup"></asp:RegularExpressionValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Contact No. 2
                                    </td>
                                    <td>
                                       <asp:TextBox ID="upc2" CssClass="form-control fs15" autocomplete="off" placeholder="optional" runat="server"></asp:TextBox>
                                          <asp:RegularExpressionValidator ID="RegularExpressionValidator11" runat="server" ErrorMessage="* Numbers Only" 
                                            ControlToValidate="upc2" Display="Dynamic" SetFocusOnError="True" Font-Size="Medium"
                                            CssClass="rfvlabel" ValidationExpression="^([0-9]*)$" ValidationGroup="rfvup"></asp:RegularExpressionValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Publisher Bank A/C No.
                                    </td>
                                    <td>
                                      <asp:TextBox ID="upban" CssClass="form-control fs15" autocomplete="off" placeholder="optional" runat="server"></asp:TextBox>
                                       <asp:RegularExpressionValidator ID="RegularExpressionValidator12" runat="server" ErrorMessage="* Numbers Only" 
                                            ControlToValidate="upban" Display="Dynamic" SetFocusOnError="True" Font-Size="Medium"
                                            CssClass="rfvlabel" ValidationExpression="^([0-9]*)$" ValidationGroup="rfvup"></asp:RegularExpressionValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Pub. Email ID
                                    </td>
                                    <td>
                                        <asp:TextBox ID="upemail" CssClass="form-control fs15" autocomplete="off" placeholder="optional" runat="server"></asp:TextBox>
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator13" runat="server" ErrorMessage="* Please Enter Valid Email ID"
                                            Display="Dynamic" CssClass="rfvlabel" ControlToValidate="upemail" ValidationGroup="rfvup"
                                            ValidationExpression="^([0-9a-zZ-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"></asp:RegularExpressionValidator>
                                    </td>
                                </tr>
        </table>
        <br />
        <table class="table table-borderless table-responsive" align="center" style="width: 300px;
            background-color: transparent;">
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="submitupdatepublisher" runat="server" OnClick="submitupdatepub_Click" class="btn btn-success btn-block"
                        Font-Size="Large" Text="Update" ValidationGroup="rfvup" />
                </td>
            </tr>
        </table>
    </div>
    </div> 
    </td> 
    <td></td>
    </tr> 
    </table> 
    </div>
</asp:Content>

更新无法成功运行。我的意思是它没有反映在数据库中。它只是根据代码,重定向到指定的页面。请帮助我在哪里出错。

1 个答案:

答案 0 :(得分:0)

实际上问题现在已经解决了。我感谢你们所有人的关心。

如果不是回发条件,那么问题就解决了。

if (!Page.IsPostBack)
{

    if (Request.QueryString["P_Id"] != null)
    {
                            ....
    }
}