简体   繁体   English

c#中的参数无效异常

[英]Parameter is not valid exception in c#

I wrote following code to pass picture from the data base to picure box in c#. 我编写了以下代码,将图片从数据库传递到c#中的图片框。 I got this code from the microsoft .here's the url of that page. 我从微软那里得到了这个代码。那是该页面的网址。 Microsoft 微软

When I run this code it's display parameter is not valid exception. 当我运行此代码时,它的显示参数无效异常。

Whats wrong with this code? 这段代码有什么问题?

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        String strCn =@"Data Source=DESKTOP-ROF2H0M\BHAGI;Initial Catalog=Golden;Integrated Security=True";

        SqlConnection cn = new SqlConnection(strCn);
        cn.Open();


        //Retrieve BLOB from database into DataSet.
        SqlCommand cmd = new SqlCommand("SELECT User_id ,img FROM login", cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "login");
        int c = ds.Tables["login"].Rows.Count;


        if (c > 0)
        {   //BLOB is read into Byte array, then used to construct MemoryStream,
            //then passed to PictureBox.
            Byte[] byteBLOBData = new Byte[0];
            byteBLOBData = (Byte[])(ds.Tables["login"].Rows[c-1]["img"]);
            MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
            pictureBox1.Image = Image.FromStream(stmBLOBData);
        }
        cn.Close();

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

I got the following error message. 我收到以下错误消息。

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll System.Drawing.dll中发生了未处理的“System.ArgumentException”类型异常

Additional information: Parameter is not valid. 附加信息:参数无效。

Here is the snap of my database. 这是我的数据库的快照。 LOgin Table LOgin表

You have 3 problem (Performance & Security issue): 您有3个问题(性能和安全问题):

  1. You need to handle SQL connection 您需要处理SQL连接
  2. You need to store files (Binary & Images) on disk (No Database) 您需要在磁盘上存储文件(二进制和图像)(无数据库)
  3. Never try to store users password without encryption (Like MD5) 切勿尝试在没有加密的情况下存储用户密码(如MD5)

private void button2_Click(object sender, EventArgs e)
{           
    string strCn = @"Data Source=DESKTOP-ROF2H0M\BHAGI;Initial Catalog=Golden;Integrated Security=True";
        using (var cn = new SqlConnection(strCn))
        {
            try
            {
                cn.Open();
                using (var cmd = new SqlCommand("SELECT User_id ,imgUrlOnDisk FROM login", cn))
                {
                    using (var dr = cmd.ExecuteReader())
                    {
                        if (dr.HasRows)
                        {
                            if (dr.Read())
                            {
                                pictureBox1.Image = Image.FromFile(Convert.ToString(dr["imgUrlOnDisk"]));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                {
                    cn.Close();
                }
            }
        }
}

Best way that I suggest you to use ADO.net query is this: 我建议你使用ADO.net查询的最佳方式是:

try
{
     using (SqlCommand cmd = new SqlCommand(Query, Connection))
     {
          try
          {
               cmd.CommandType = CommandType;
               foreach (var p in InParameters)
               {
                    cmd.Parameters.Add(p);
               }
               cmd.Connection.Open();
               affectedRows = cmd.ExecuteNonQuery();
               if (affectedRows == 0)
               {
                    //Zero Record Success
               }
               else
               {
                   if (affectedRows > 1)
                   {
                        //Many Record Success
                   }
                   else
                   {
                        //One Record Success
                   }
               }
           }
           catch (Exception InnerEx)
           {
                //Handle your error
           }
           finally
           {
               if (cmd.Connection.State != ConnectionState.Closed)
               {
                   cmd.Connection.Close();
               }
           }
      }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM