数据库不接收信息

时间:2013-10-20 02:11:11

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

我正在编写一个asp.net/c#网页表单,用于更新SQL Server数据库中的信息。我遇到的问题是注册页面。由于某种原因,数据不会被发送到服务器。我也有非常相似的主页代码,运行正常。

这是不向SQL Server发送信息的注册页面:

    using System;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Configuration;

    namespace WebApplication3
    {
public partial class Signup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString1"].ConnectionString);

      SqlCommand cmd = new SqlCommand("INSERT INTO Users(UserID, Password, FirstName, MiddleName, LastName, Email) VALUES (@UserID, @Password, @FirstName, @MiddleName, @LastName, @Email)", conn);
      cmd.CommandType = System.Data.CommandType.Text;

      cmd.Parameters.AddWithValue("@UserID", TextBox1.Text);
      cmd.Parameters.AddWithValue("@Password", TextBox2.Text);
      cmd.Parameters.AddWithValue("@FirstName", TextBox3.Text);
      cmd.Parameters.AddWithValue("@MiddleName", TextBox4.Text);
      cmd.Parameters.AddWithValue("@LastName", TextBox5.Text);
      cmd.Parameters.AddWithValue("@Email", TextBox6.Text);

      conn.Open();
      cmd.ExecuteNonQuery();
      Response.Redirect("login.aspx");
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString1"].ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("select * from Users where UserID= '" + TextBox1.Text + "'", conn);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
    {
        Label1.Text="UserName is Taken";
        this.Label1.ForeColor=Color.Red;
    }
    else
    {
        Label1.Text="UserName is not Taken";
        this.Label1.ForeColor = Color.Black;
    }

    }
}

这是主页面的代码,它应该按原样运行:

    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.Configuration;

    namespace WebApplication3
    {
public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString1"].ConnectionString);
       SqlCommand cmd = new SqlCommand("INSERT INTO Observations(UserID, DateTime, Loc_Lat, Loc_Lon, Observation, Comments) VALUES (@UserID, @DateTime, @Loc_Lat, @Loc_Lon, @Observation, @Comments)", conn);
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue("@UserID", TextBox1.Text);
        cmd.Parameters.AddWithValue("@Observation", TextBox2.Text);
        cmd.Parameters.AddWithValue("@DateTime", DateTime.Now);
        cmd.Parameters.AddWithValue("@Comments", TextBox3.Text);
        cmd.Parameters.AddWithValue("@Loc_Lat", TextBox4.Text);
        cmd.Parameters.AddWithValue("@Loc_Lon", TextBox5.Text);
        conn.Open();
        cmd.ExecuteNonQuery();
        Response.Redirect("Webform1.aspx");
    }
}

问题的原因是什么? 提前谢谢!

编辑: 继承我的连接字符串:

    (<)add name="testConnectionString1" connectionString="Data Source=MALAYSIA\SQLEXPRESS;Initial Catalog=test;Integrated Security=True"

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决问题的方法。事实证明,在按钮属性中, PostBackurl 设置为login.aspx页面。删除后,程序现在可以正常工作。