Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter is not valid exception in c#

Tags:

c#

sql

ado.net

I wrote following code to pass picture from the data base to picure box in 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

Additional information: Parameter is not valid.

Here is the snap of my database. LOgin Table

like image 996
Pegasus008 Avatar asked Oct 29 '22 20:10

Pegasus008


1 Answers

You have 3 problem (Performance & Security issue):

  1. You need to handle SQL connection
  2. You need to store files (Binary & Images) on disk (No Database)
  3. Never try to store users password without encryption (Like 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:

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();
               }
           }
      }
}
like image 106
Imran Sh Avatar answered Nov 15 '22 05:11

Imran Sh