我正在尝试检查已输入的用户名是否已存在于数据库中。但高于错误率。
protected void txtUsername_TextChanged(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(con1);
try{
if (!string.IsNullOrEmpty(txtUsername.Text))
{
con.Open();
MySqlCommand cmd = new MySqlCommand("select username from client_login where username=@Name", con);
DataSet ds = new DataSet();
MySqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
checkusername.Visible = true;
imgstatus.ImageUrl = "NotAvailable.jpg";
lblStatus.Text = "UserName Already Taken";
}
else
{
checkusername.Visible = true;
imgstatus.ImageUrl = "Icon_Available.gif";
lblStatus.Text = "UserName Available";
}
}
else
{
checkusername.Visible = false;
}
con.Close();
}
catch(Exception ex){
Response.Write(ex);
}
}
和textBox代码是
` <asp:TextBox ID="txtUsername" runat="server" AutoPostBack="true" OnTextChanged="txtUsername_TextChanged"></asp:TextBox>`
错误在Execute Reader as base = {&#34;命令执行期间遇到致命错误。&#34;}
答案 0 :(得分:1)
您需要在sql查询中填充参数。像:
cmd.Parameters.AddWithValue("@Name", txtUsername.Text);