Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi bax
i have problem in loading an image from sqlserver2088 r2 please help me
my code has this error : the parameter value is not valid.

its my code:

C#
string pic = "select PIC from _TRBS_PIC WHERE SERIAL = '" + fieldTxt1.Text + "';
SqlDataAdapter sda = new SqlDataAdapter(pic, sqlCnn);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count>0)
{
    byte[] imageBytes = new byte[0];
    imageBytes = (byte[])dt.Rows[0][0];
    MemoryStream ms = new MemoryStream(imageBytes);
    picBoxUser.Image = Image.FromStream(ms); // the parameter value is not valid
}
Posted
Updated 30-Oct-13 1:01am
v2

1 solution

It's probably how you stored the data in the first place. See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]
 
Share this answer
 
Comments
fatima...68 30-Oct-13 7:41am    
thanks i did it and i fix my insert command but i have that error yet
OriginalGriff 30-Oct-13 10:54am    
And did you fix the values in your database as well?
Because fixing the INSERT will not affect existing values...
fatima...68 31-Oct-13 1:41am    
thanks my firiend
what do you mean
OriginalGriff 31-Oct-13 2:46am    
I mean that fixing the INSERT command fixes images you as to the database from now on. It doesn't fix images you added to the DB before you changed the way you did INSERTs - you have to delete all the old, faulty images from the DB before you start adding new images that will work.
fatima...68 31-Oct-13 4:39am    
yes i did this
i delete old image and insert the new image
this is my insert code
pic_count = Directory.GetFiles("c:\\OMEGA\\kaj" , "*.Bmp", SearchOption.AllDirectories).Length;
string[] fileInDir = new string [pic_count];
string[] NAM = new string[pic_count];

fileInDir = Directory.GetFiles("c:\\OMEGA\\kaj", "*.Bmp", SearchOption.TopDirectoryOnly);

int l = fileInDir.ToString().Length;
int index = 0;

for (int i = 0; i < pic_count; i++)
{
index = fileInDir[i].ToString().LastIndexOf("\\");
if (index > -1)
{
NAM[i] = fileInDir[i].ToString().Substring(index + 1);
}

IMG = Image.FromFile("c:\\OMEGA\\kaj\\" + NAM[i] + "");
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
IMG.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
data = ms.ToArray();
}
picBoxUser.Image = IMG;

string num = "select SERIAL from _TRBS_PIC where SERIAL = '"+int.Parse(NAM[i].Replace(".Bmp",""))+"' ";

SqlDataAdapter dp = new SqlDataAdapter (num , sqlCnn);
DataTable tp = new DataTable ();
dp.Fill(tp);

if (tp.Rows.Count>0)
{
p_num = int.Parse(tp.Rows[0][0].ToString());
}


if (tp.Rows.Count <= 0)
{
try
{
double max;
DateTime d;
SqlCommand cmdDateTime = new SqlCommand("Select getdate() ", sqlCnn);
sqlCnn.Open();
d = DateTime.Parse(cmdDateTime.ExecuteScalar().ToString());
sqlCnn.Close();
SqlCommand cmdCount = new SqlCommand("Select Count(_ID) from _TRBS_PIC ", sqlCnn);
sqlCnn.Open();
double No = double.Parse(cmdCount.ExecuteScalar().ToString());
sqlCnn.Close();
if (No == 0) max = 1000 + StID;
else
{
SqlCommand cmdMax = new SqlCommand("Select max(_ID) from _TRBS_PIC ", sqlCnn);
sqlCnn.Open();
max = double.Parse(cmdMax.ExecuteScalar().ToString());
max = max % 1000;
max = double.Parse(cmdMax.ExecuteScalar().ToString()) - max + 1000 + StID;
sqlCnn.Close();
}
ID = max;
string strsql = "INSERT INTO _TRBS_PIC ([_ID]"
+ ",[SERIAL]"
+ ",[DATE]"
+ ",[PIC]"
+ ",[NUM]"
+ ",[DETAIL]"
+ ",[ESERIAL]"
+ ",[UID1]"
+ ",[UID2]"
+ ", [_LastSecurityOption], [_LastSaveOption], [_LastDate], [_LastTime], [_LastDateTime], [_LastUID], [_LastStID], [_LastStatus]) values ('"
+ max.ToString()
+ "','" + NAM[i].Replace(".Bmp","")
+ "','" + fieldTxt2.Text
+ "','" + picBoxUser.Image
+ "','" + pic_count
+ "',' "
+ "','0"
+ "','" + UID1.ToString()
+ "','" + UID2.ToString()
+ "','0','0','" + d.ToShortDateString().ToString() + "','" + d.ToShortTimeString().ToString() + "','" + d.ToString() + "','" + UID.ToString() +

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900