简体   繁体   English

当尝试从数据库中检索数据时,我收到错误“InvalidOperationException未处理”

[英]When trying to retrieve data from the database I get an error “InvalidOperationException was unhandled”

I have a form named Form1 : 我有一个名为Form1的表单:

There is one ComboBox and one TextBox , when I select US$ from the ComboBox then it must retrieve data from the database and display 150 in the TextBox . 有一个ComboBox和一个TextBox ,当我从ComboBox选择US $时,它必须从数据库中检索数据并在TextBox显示150。

This is myform code: 这是myform代码:

For ComboBox ; 对于ComboBox ;

namespace PCJ_System
{
  public partial class Form1 : Form
  {
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataReader dr;
    public Form1()
    {
        InitializeComponent();
    }

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = "server = DESKTOP-LKEG8FM\\SQLEXPRESS;initial catalog= PCJ_DB ; Integrated Security=True;";
        SqlConnection conn = new SqlConnection(str);
        conn.Open();
        conn = new SqlConnection(str);
        string GetData = "Select [FC_Rate] from Forcur where FC_TYPE ='" + comboBox1.Text + "' ";
        cmd = new SqlCommand(GetData, conn);
        var returnValue = cmd.ExecuteScalar();

        textBox1.Text = returnValue.ToString();
        conn.Close();
    }

  }
}  

My database table Forcur: 我的数据库表Forcur:

ID |FC_TYPE |FC_RATE|
1   US$      150
2   UK#      210

What's wrong with my code? 我的代码出了什么问题?

This might not be the exact answer you are looking for, but you need to take care of following: 这可能不是您正在寻找的确切答案,但您需要注意以下事项:

1) Assign DB connection string to SqlConnection object and open connection. 1)将DB连接字符串分配给SqlConnection对象并打开连接。

2) Since you are assigning one value to textbox, you need to use ExecuteScalar instead of ExecuteReader 2)由于您要为文本框分配一个值,因此需要使用ExecuteScalar而不是ExecuteReader

Once you fix this, you should get the desired result. 解决这个问题后,您应该得到所需的结果。

Example: 例:

conn=new SqlConnection(connectionStringHere);
conn.Open();
string GetData = "Select [FC_Rate] from Forcur where FC_TYPE ='" + comboBox1.Text + "' ";
cmd = new SqlCommand(GetData, conn);
var returnValue = cmd.ExecuteScalar();

textBox1.Text = returnValue.ToString();
conn.close();

Note: You still have SQL injection attack open in your SQL query. 注意:您仍然在SQL查询中打开SQL注入攻击。 Try using varables instead to stop that. 尝试使用变量来阻止它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试从数据库中检索密码时,发生InvalidOperationException,“如果不存在任何数据,则尝试进行无效读取。” - InvalidOperationException when i try to retrieve a password from database, “Invalid attempt to read when no data is present.” 尝试从数据库中检索时出错 - Error when trying to retrieve from database 尝试删除数据库中的记录时,我遇到了这个“InvalidOperationException 错误” - I'm stuck on this "InvalidOperationException Error" when trying to delete a record in my Database 尝试检索配置属性时,NullReferenceException是未处理的错误 - NullReferenceException was unhandled error when trying to retrieve config attributes 尝试运行 web 应用程序时出现 System.InvalidOperationException - I get System.InvalidOperationException when trying to run the web app 尝试保存在数据库中时 System.InvalidOperationException - System.InvalidOperationException when trying to save in database 尝试连接到SQL Server数据库时出现“ SqlException未处理”错误 - 'SqlException was unhandled' error when trying to connect to SQL Server database 尝试从活动目录检索数据时发生操作错误 - An Operations Error Occured When trying to retrieve data from active directory 当我尝试将数据插入文本框时,System.Data.dll中发生了类型为'System.InvalidOperationException'的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll when I tried to insert data into textbox 当我尝试从数据库创建模型时,为什么会出现此错误? - Why do I get this error when I'm trying to create models from database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM