0

我目前在一个需要你帮助的特定问题上感到困惑。我的 asp.net 网站遇到了这个特殊问题。

无论出于何种原因,当用户试图将信息从文本框中保存到我的 .mdb 文件时,它都不会接受它。一切都编译得很好,我已经四次检查了所有的 ID 和字符串名称,一切似乎都与 .mdb、.c 文件和 aspx.cs 页面中的表上的内容相匹配。

这是输入信息的 .aspx 页面

 <asp:Label ID="lblFirstName1" runat="server" align="left" Text="First Name: " Width="125px"></asp:Label>
        <asp:TextBox ID="txtfirstName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="txtfirstName" ErrorMessage="First Name cannot be empty"></asp:RequiredFieldValidator>
        <br />

        <asp:Label ID="lblLastName1" runat="server" Text="Last Name: " Width="125px"></asp:Label>
        <asp:TextBox ID="txtlastName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
            ControlToValidate="txtlastName" ErrorMessage="Last Name cannot be empty"></asp:RequiredFieldValidator>
        <br />


         <asp:Label ID="lblUserAddress1" runat="server" Text="Street Addres: " Width="125px"></asp:Label>
         <asp:TextBox ID="txtstreetAddress" runat="server"></asp:TextBox>
         <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtstreetAddress"
             ErrorMessage="Address cannot be empty"></asp:RequiredFieldValidator>
         <br />
         <asp:Label ID="lblcity1" runat="server" Text="City: " Width="125px"></asp:Label>
         <asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
         <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtcity"
             ErrorMessage="City cannot be empty"></asp:RequiredFieldValidator>
         <br />
         <asp:Label ID="lblstate1" runat="server" Text="State: " Width="125px"></asp:Label>
         <asp:TextBox ID="txtstate" runat="server"></asp:TextBox>
         <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txtstate"
             ErrorMessage="State cannot be empty"></asp:RequiredFieldValidator>
         <br />
         <asp:Label ID="lblzipCode1" runat="server" Text="Zip Code: " Width="125px"></asp:Label>
         <asp:TextBox ID="txtzipCode" runat="server"></asp:TextBox>
         <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtzipCode"
             ErrorMessage="Zip Code cannot be empty"></asp:RequiredFieldValidator>

这是 .cs 页面中用于保存到 .mdb 的信息

      public static bool Saveneworder(string Database, string firstName, string lastName, string streetAddress, string city, string state, string zipCode )
{

      bool recordSaved;

    // Create a new Oledb Transaction object
    OleDbTransaction myTransaction = null;

     try
    {
        // Create a New Connection Object to the Access Database
        OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
        "Data Source=" + Database);
        conn.Open();
        OleDbCommand command = conn.CreateCommand();
        string strSQL;

        // set the transaction object and start the transaction
        myTransaction = conn.BeginTransaction();
        command.Transaction = myTransaction;

          strSQL = "Insert into tblOrder " +
        "(firstName, lastName, streetAddress, city, state, zipCode) values ('" +
        firstName + "', '" + lastName + "', '" + streetAddress + "', '" + city + "', '" + state +
        "', '" + zipCode + "')";

          // set the command text of the command object
          command.CommandType = CommandType.Text;
          command.CommandText = strSQL;
          // Execute the insert statement
          command.ExecuteNonQuery();

          myTransaction.Commit();

          // Close the Database connection
          conn.Close();
          recordSaved = true;
    }

     catch (Exception ex)
     {
         //Rollback the transaction if some failure occurs
         myTransaction.Rollback();

         recordSaved = false;


     }

     return recordSaved;


}

这是第一个将数据从文本框传输到 orderverified 页面的 aspx.cs 文件,该页面列出了信息未发布的 else 语句

  protected void btnSubmit_Click(object sender, EventArgs e)
{

    if (ValidateFields()) //if Validate fields method has returned true
    {
         Session.Add("txtfirstName", txtfirstName.Text);
        Session.Add("txtlastName", txtlastName.Text);
        Session.Add("txtstreetAddress", txtstreetAddress.Text);
        Session.Add("txtcity", txtcity.Text);
        Session.Add("txtstate", txtstate.Text);
        Session.Add("txtzipCode", txtzipCode.Text);


        Server.Transfer("orderverified.aspx");

}

并将信息转发到这个 aspx.cs 文件

  protected void Page_Load(object sender, EventArgs e)
{
    //So here we are initializing text property of the textbox "txtVerifiedInfo" after fetching the
    //values from the session object

    txtVerifiedInfo.Text =   Session["txtfirstName"].ToString() +
   "\n" + Session["txtlastName"].ToString() +
   "\n" + Session["txtstreetAddress"].ToString() +
   "\n" + Session["txtcity"].ToString() +
   "\n" + Session["txtstate"].ToString() +
   "\n" + Session["txtzipCode"].ToString() 
                                                  ;

    // Check if the record is successfully saved in the tblOrder Table and prints the appropriate message in the text box txtVerifiedInfo
    if (clsDataLayer.Saveneworder(Server.MapPath("App_Data\\WSC_DB.mdb" ),
    Session["txtfirstName"].ToString(),
    Session["txtlastName"].ToString(),
    Session["txtstreetAddress"].ToString(),
    Session["txtcity"].ToString(),
    Session["txtstate"].ToString(),
    Session["txtzipCode"].ToString()  ))



    {
        txtVerifiedInfo.Text = txtVerifiedInfo.Text +
        "\nThe Order successfully submitted!";

    }
    else
    {
        txtVerifiedInfo.Text = txtVerifiedInfo.Text +
        "\n The order did not save, please return to the previous screen and verify all of your data is correct, thank you.";
    }    

在最后一页 - Orderverified.aspx 我有一个多行文本框,清楚地显示所有数据,但它返回我的 else 语句,它无法保存到 tblOrder

很抱歉发布了这么多代码,但我真的很困惑为什么这不能保存。

感谢您的阅读和您的时间

在进一步排除故障后,我尝试排除除了名字之外的所有内容,但它仍然不会发布。我觉得问题很可能出在 .cs 文件代码上,即插入到 tblOrder 中的 sql 语句

4

2 回答 2

0

好的,我想通了——我找不到任何代码的问题,因为没有!我发现我将主键 int 他.mdb 设置为我没有输入的字段,这导致它无法保存,因为它被设置为文本而不是自动编号并且不会让文件保存!谢谢你看看它 jeremywho

于 2013-06-21T01:21:45.963 回答
0

我从未使用过 OleDbConnection,但似乎您可能需要使用 .Parameters.AddWithValue()。请参阅此 StackOverflow 答案

于 2013-06-21T00:56:22.847 回答