I am trying to check if the value exists in a database column. I know this question asked several times but none of them is solving my problem. I have tried it in many ways. Anyway my following code through an exception;
"An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: No mapping exists from object type System.Windows.Forms.TextBox to a known managed provider native type."
This problem is resolved of exception that was a mistake. but it still not working although the record is in database but it is not restricting me.
Can anyone tell me whats wrong with my code, the code is;
private void btn_Register_Click(object sender, EventArgs e)
{
lbl_Available.Visible = false;
lbl_Empty.Visible = false;
bool hasValue1 = string.IsNullOrWhiteSpace(txt_PinCode.Text);
bool hasValue2 = string.IsNullOrWhiteSpace(txt_Name.Text);
if (!hasValue1 && !hasValue2)
{
string pincode = txt_PinCode.Text;
if (UserExists(pincode))
{
lbl_Available.Visible = true;
return;
}
else
{
this.Hide();
RegCluePoint frm = new RegCluePoint();
frm.lbl_PinCode.Text = txt_PinCode.Text;
frm.lbl_Name.Text = txt_Name.Text;
frm.Show();
}
}
else
{
lbl_Empty.Visible = true;
}
}
private bool UserExists(string pincode)
{
//pincode = txt_PinCode.Text
//SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT PINCODE from UserInput where PINCODE = '@pincode'";
cmd.Parameters.AddWithValue("PINCODE", pincode);
if (conn.State != ConnectionState.Open)
conn.Open();
string pinString = (string)cmd.ExecuteScalar();
conn.Close();
return (pinString != null);
}
您发送txt_PinCode
,一个文本框对象,而不是一个字符串( txt_PinCode.Text
在此行中) cmd.Parameters.AddWithValue("PINCODE", txt_PinCode);
You are not passing the pincode
that is passed to the method, instead you are passing the textbox itself (not even the value of the textbox).
private bool UserExists(string pincode)
{
//pincode = txt_PinCode.Text
//SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT PINCODE from UserInput where PINCODE = '@pincode'";
cmd.Parameters.AddWithValue("@pincode", pincode);
if (conn.State != ConnectionState.Open)
conn.Open();
//Execute scalar returns the first column of the first row in the result set
string pinString = (string)cmd.ExecuteScalar();
conn.Close();
return (pinString != null);
}
You are trying to pass the textbox itself, but not text. Change the code
cmd.Connection = conn;
cmd.CommandText = "SELECT PINCODE from UserInput where PINCODE = '@pincode'";
cmd.Parameters.AddWithValue("PINCODE", txt_PinCode.Text);
In this line of code you are sending the textbox object onstead of text.
cmd.Parameters.AddWithValue("PINCODE", txt_PinCode);
Change it to:
cmd.Parameters.AddWithValue("PINCODE", txt_PinCode.Text);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.