简体   繁体   English

如何在 Windows 窗体应用程序中修复“无法从“字符串”转换为 System.Data.SqlDbType”

[英]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.

相关问题 在 System.Data.SqlDbType 和 Microsoft.SqlServer.Management.Smo.SqlDataType 之间转换 - Convert between System.Data.SqlDbType and Microsoft.SqlServer.Management.Smo.SqlDataType 我如何遍历所有System.Data.SqlDbType? - How can i iterate through all System.Data.SqlDbType s? 无法从字符串转换为System.Windows.Forms.Control - Cannot convert from string to System.Windows.Forms.Control 无法从“字符串”转换为“ System.Windows.Forms.MessageBoxIcon” - cannot convert from 'string' to 'System.Windows.Forms.MessageBoxIcon 无法从“ system.windows.forms.messagebox”转换为“ string” - Cannot convert from 'system.windows.forms.messagebox' to 'string' 如何从“字符串”转换为“ System.Windows.Forms名称” - How to convert from 'string' to 'System.Windows.Forms name 无法从字符串转换为system.windows.forms.string iwin32window - cannot convert from string to system.windows.forms.string iwin32window 无法将类型&#39;string&#39;隐式转换为&#39;System.Windows.Forms.TextBox&#39; - Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox' 无法将类型&#39;string&#39;隐式转换为&#39;System.Windows.Forms.Label&#39; - Cannot implicitly convert type 'string' to 'System.Windows.Forms.Label' 错误 4 参数 5:无法从“System.Windows.Forms.DataGridViewTextBoxColumn”转换为“string” - Error 4 Argument 5: cannot convert from 'System.Windows.Forms.DataGridViewTextBoxColumn' to 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM