[英]ASP.NET C# Validating username and password
Im trying to validate username and password from an MySql server. 我正在尝试从MySql服务器验证用户名和密码。 Login validation is working, but I can't for the life of me figure out why the "Create new user" validation isn't working.
登录验证有效,但我一生都无法弄清楚为什么“创建新用户”验证无效。
Here are the code for registering new user. 这是用于注册新用户的代码。 What happens is;
发生了什么事?
catch (Exception)
{
Label1.Text = "Brukernavnet er allerede i bruk";
}
Seems like this part ^ is ruining it for me somehow, whenever i test run this code I get this message. 看起来这部分^某种程度上对我来说是毁了它,每当我测试运行此代码时,我都会收到此消息。
protected void newBtn_Click(object sender, EventArgs e)
{
String cs = "Database=trafikkskole; User=user; Password=password";
MySqlConnection dbconnect = new MySqlConnection(cs);
try
{
dbconnect.Open();
cmd.CommandText = "INSERT INTO user (username, password) VALUES (@un, @pw)";
cmd.Parameters.AddWithValue("@un", inputUser.Text);
cmd.Parameters.AddWithValue("@pw", inputPw.Text);
cmd.Connection = dbconnect;
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
Label1.Text = "Gratulerer! Du har nå laget en bruker!";
}
else
{
Label1.Text = "ERROR";
}
}
catch (Exception)
{
Label1.Text = "Brukernavnet er allerede i bruk";
}
finally
{
dbconnect.Close();
}
}
}
EDIT: 编辑:
If I try it like this: 如果我这样尝试:
protected void newBtn_Click(object sender, EventArgs e)
{
String cs = "Database=trafikkskole; User=root; Password=root";
MySqlConnection dbconnect = new MySqlConnection(cs);
String sql = "SELECT * FROM user";
MySqlCommand cmd = new MySqlCommand(sql, dbconnect);
da = new MySqlDataAdapter(cmd);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
ds = new DataSet("TEST");
da.Fill(ds, "user");
Response.Write(ds.Tables["user"].Rows.Count);
try
{
dbconnect.Open();
cmd.CommandText = "INSERT INTO user (username, password) VALUES (@un, @pw)";
cmd.Parameters.AddWithValue("@un", inputUser.Text);
cmd.Parameters.AddWithValue("@pw", inputPw.Text);
cmd.Connection = dbconnect;
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
Label1.Text = "Gratulerer! Du har nå laget en bruker!";
}
else
{
Label1.Text = "ERROR";
}
}
catch (Exception Exception)
{
Label1.Text = "Brukernavnet er allerede i bruk";
}
finally
{
dbconnect.Close();
}
}
}
This ends up with the possibility of making a user without username or password. 最终可能使用户没有用户名或密码。
There are a number of things that could be going wrong. 有很多事情可能会出错。 You should examine the exception.message to get insights as to what it could be.
您应该检查exception.message以获得对它可能是什么的见解。
For example, put a break point in the catch statement and see if the exception thrown for things like... does the username already exist and SQL is throwing an error. 例如,在catch语句中放置一个断点,看看是否为诸如...之类的事件引发了异常,该用户名是否已经存在,并且SQL引发了错误。 ... or are the username/password null, too long, etc...
...或者用户名/密码为空,太长等...
Regardless, change the catch statement to catch(Exception exception)
and see what the exception is. 无论如何,将catch语句更改为
catch(Exception exception)
并查看异常是什么。
I want to thank everyone for trying, found a working solution, will post it here for future reference. 我要感谢大家的努力,找到了一个可行的解决方案,并将其发布在此处以供将来参考。
protected void newBtn_Click(object sender, EventArgs e)
{
String cs = "Database=trafikkskole; User=root; Password=root";
MySqlConnection dbconnect = new MySqlConnection(cs);
try
{
if (!string.IsNullOrWhiteSpace(inputUser.Text) && !string.IsNullOrWhiteSpace(inputPw.Text))
{
dbconnect.Open();
Label1.Text = "Gratulerer! Du har nå laget en bruker!";
string qry = "INSERT INTO user(username, password) VALUES (@un, @pw)";
cmd = new MySqlCommand(qry, dbconnect);
cmd.Parameters.AddWithValue("@un", inputUser.Text);
cmd.Parameters.AddWithValue("@pw", inputPw.Text);
cmd.Connection = dbconnect;
cmd.ExecuteNonQuery();
}
else
{
Label1.Text = "ERROR";
}
}
catch (Exception)
{
Label1.Text = "Brukernavnet er allerede i bruk";
}
finally
{
dbconnect.Close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.