我是 .NET 和 C# 领域的新手。我正在为我正在处理的网站创建一个注册页面。注册到我正在创建的网站时,我不断收到错误消息。输入我的详细信息时,它不会将我注册到数据库中。我在数据库中创建了一个表(我创建了一个连接字符串),但我无法注册 - 我收到一个异常,上面写着“错误,请尝试再次注册”(就像我一样)。有人知道我在做什么错吗?!谢谢!
这是我的代码和图片:
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;
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisConnectionString"].ConnectionString);
con.Open();
string cmdStr = "Select count(*) from Table where Username='" + TextBox1Username.Text + "'";
SqlCommand userExist = new SqlCommand(cmdStr, con);
// int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
con.Close();
/* if (temp == 1)
{
Response.Write("username already exists");
} */
}
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisConnectionString"].ConnectionStr ing);
con.Open();
string insCmd = "Insert into Table (Username, Password, EmailAddress,Fullname, City) values (@Username, @Password, @EmailAddress, @Fullname, @City)";
SqlCommand insertUser = new SqlCommand(insCmd, con);
insertUser.Parameters.AddWithValue("@Username", TextBox1Username.Text);
insertUser.Parameters.AddWithValue("@Password", TextBox2Password.Text);
insertUser.Parameters.AddWithValue("@EmailAddress", TextBox4Email.Text);
insertUser.Parameters.AddWithValue("@Password", TextBox2Password.Text);
insertUser.Parameters.AddWithValue("@City", TextBox6City.Text);
try
{
insertUser.ExecuteNonQuery();
con.Close();
Response.Redirect("Login.aspx");
}
catch (Exception er)
{
Response.Write("error,please try registering again");
}
}
}
图片:
[URL=http://imageshack.us/photo/my-images/4/os6b.jpg/][IMG=http://img4.imageshack.us/img4/2526/os6b.jpg][/IMG][ /网址]
使用 [URL=http://imageshack.us]ImageShack.us[/URL] 上传