好的,我知道我是业余爱好者,最近我问了很多问题。我已修复它,因此无法将重复记录输入到我的sql server数据库。这是代码:
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 Default2 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Guid newGUID = Guid.NewGuid();
SqlConnection User = new SqlConnection("Data Source=CONNECTION STRING BLAH BLAH");
{
User.Open();
string checkuser = "select count(*) from userdatabase where Username = @username";
SqlCommand com = new SqlCommand(checkuser, User);
com.Parameters.AddWithValue("@username", InputUsername.Text);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
User.Close();
if (temp == 1)
{
Response.Write("Username is already in use");
}
if (temp == 0)
{
SqlCommand xp = new SqlCommand("Insert into userdatabase(ID,Username, Email, Password, Fname, LName, Addl1, AddL2,
City, County, Postcode, Country, Company)Values(@ID, @Username,
@Email, @Password, @FName, @LName, @AddL1, @AddL2, @City, @County,
@Postcode, @Country, @Company )", User);
xp.Parameters.AddWithValue("@ID", newGUID.ToString());
xp.Parameters.AddWithValue("@Username", InputUsername.Text);
xp.Parameters.AddWithValue("@Email", InputEmail.Text);
xp.Parameters.AddWithValue("@Password", InputPassword.Text);
xp.Parameters.AddWithValue("@FName", InputFname.Text);
xp.Parameters.AddWithValue("@LName", InputSname.Text);
xp.Parameters.AddWithValue("@AddL1", InputAddress1.Text);
xp.Parameters.AddWithValue("@AddL2", InputAddress2.Text);
xp.Parameters.AddWithValue("@City", InputCity.Text);
xp.Parameters.AddWithValue("@County", InputState.Text);
xp.Parameters.AddWithValue("@Postcode", InputPostcode.Text);
xp.Parameters.AddWithValue("@Country", InputCountry.Text);
xp.Parameters.AddWithValue("@Company",InputCompany.Text);
User.Open();
xp.ExecuteNonQuery();
User.Close();
}
if (IsPostBack)
{
InputUsername.Text = "";
InputPassword.Text = "";
InputEmail.Text = "";
VerifyPassword.Text = "";
InputFname.Text = "";
InputSname.Text = "";
InputAddress1.Text = "";
InputAddress2.Text = "";
InputCity.Text = "";
InputState.Text = "";
InputPostcode.Text = "";
InputCompany.Text = "";
Response.Redirect("Login.aspx");
}
}
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
} }
但是,正如您所看到的,我已将其设置为“用户名已在使用中”。但它不会显示它,也不会将数据输入到表中。它还将其重定向到login.aspx页面。有关停止页面重定向到新页面并显示消息的任何帮助吗?
修改
有人提到有关这个工作的尝试/捕获方法,有人能够向我解释这是如何工作的,我已经在YouTube上看了但我还没有找到任何可以帮助这个< / p>