[英]How to fix 'Cannot convert from 'string' to System.Data.SqlDbType' in a Windows Forms Application
This only happens when I created a basic login screen for a sample application I'm building.这只发生在我为正在构建的示例应用程序创建基本登录屏幕时。 I have various other functions in the application that read/write/update rows in a database without errors.
我在应用程序中有各种其他功能,可以无错误地读取/写入/更新数据库中的行。
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
if(txtUname.Text == "" && txtPW.Text == "")
{
MessageBox.Show("Please fill in required fields!");
}
else
{
SqlCommand cmd = new SqlCommand("select * from LoginUsers where U_Name=@Name and U_Pass=@Pass", con);
cmd.Parameters.Add("@Name", txtUname.Text); <- Error on textbox string
cmd.Parameters.Add("@Pass", txtPW.Text); <- Error on textbox string
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
int count = ds.Tables[0].Rows.Count;
if(count == 1 )
{
MessageBox.Show("Successfully Logged in!");
}
else
{
MessageBox.Show("Incorrect username or password!");
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Try anyone from below:试试下面的任何人:
Approach 1:方法一:
command.Parameters.Add("@ID", SqlDbType.Int).Value = customerID;
Approach 2:方法二:
command.Parameters.AddWithValue("@demographics", demoXml);
Just a random code based on the classification defined, Do let me know if you still face an issue after using any of the above mentioned approach.只是基于定义的分类的随机代码,如果您在使用上述任何方法后仍然遇到问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.