Click here to Skip to main content
16,004,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a C# program that stores images in "H:\oucu\Student Photos", and supposedly to an SQL database, in one of eight tables. the file saves well, but I try to use a web application to read the images, every information in the row appears except the images.
C#
public bool SaveDIBAs( string picname, IntPtr bminfo, IntPtr pixdat )
	{
		string  Partion=  @"h:\oucu\Student_Images\";
		string StName=St_num;
		string FullPath=Partion+StName;
		
 
		Guid clsid;
		if( ! GetCodecClsid( FullPath.ToString(), out clsid ) )
		{
			MessageBox.Show( "Unknown picture format for extension " + Path.GetExtension( FullPath.ToString() ),
				"Image Codec", MessageBoxButtons.OK, MessageBoxIcon.Information );
			return false;
		}
		
		IntPtr img = IntPtr.Zero;
		int st = GdipCreateBitmapFromGdiDib( bminfo, pixdat, ref img );
		if( (st != 0) || (img == IntPtr.Zero) )
			return false;
		
		textBox2.Text = FullPath.ToString() ;
		
		st = GdipSaveImageToFile( img,FullPath.ToString(), ref clsid, IntPtr.Zero );
		GdipDisposeImage( img );
		return st == 0;
		
	}
 
	public void ShowFrm()
	{
		this.Show();
	}

private void button1_Click(object sender, System.EventArgs e)
	{
        //St_num=stNTxt.Text ;
        St_num = StudentPhoto.photo.fileName;
		SaveDIBAs( this.Text, bmpptr, pixptr );
		byte[] byte_Img;
        
		//SqlConnection mySqlConn=new SqlConnection("Network Library=DBMSSOCN;"+"Data Source=82.201.143.224,2022;"+"Initial Catalog=oucu_card;"+"User ID=ousa;"+"Password=cO45_iu89");
        SqlConnection mySqlConn = new SqlConnection("server=192.168.1.25;workstation id=localhost;packet size=4096; persist security info=False;user id=araslan;Password=hR@oucu1;Initial catalog=mandoobeen;min pool size=1;max pool size=10000000;connection lifetime=6000");
		
		
		SqlCommand sqlcmd = new SqlCommand();
		sqlcmd.Connection=mySqlConn;
		sqlcmd.CommandText="insert into " + StudentPhoto.photo.tableName + " (nationalNumber,pictureName,Picture) values(@nN,@pN,@pic)";
		
		sqlcmd.Parameters.Add("@nN",System.Data.SqlDbType.NVarChar);				
		sqlcmd.Parameters.Add("@pic",System.Data.SqlDbType.Image);
		sqlcmd.Parameters.Add("@pN",System.Data.SqlDbType.NVarChar);
		
 
		FileStream fs=new FileStream(this.textBox2.Text.ToString(),FileMode.Open,FileAccess.Read,FileShare.Read);
		byte_Img=new byte[Convert.ToInt32(fs.Length)];
		int iBytesRead=fs.Read(byte_Img,0,Convert.ToInt32(fs.Length));
		fs.Close();
 
        sqlcmd.Parameters["@nN"].Value = StudentPhoto.photo.nationalNumber;
		sqlcmd.Parameters["@pic"].Value=byte_Img;
        TwainGui.Login l = new TwainGui.Login();
        sqlcmd.Parameters["@pN"].Value = StudentPhoto.photo.fileName;
 
		mySqlConn.Open();
		int iresult=sqlcmd.ExecuteNonQuery();
		mySqlConn.Close();
 
				
		System.Diagnostics.Process process1;
		process1= new System.Diagnostics.Process();
		process1.EnableRaisingEvents = false;
		Directory.SetCurrentDirectory(textBox3.Text);
        Application.Exit();
    }
Posted
Updated 14-May-12 22:42pm
v2
Comments
Pete O'Hanlon 15-May-12 4:05am    
There's far too much code here. Isolate the code parts that are giving you the problem. BTW - have you run your SQL query directly to see what results it brings back?
Rahul Rajat Singh 15-May-12 4:06am    
Please post the code related to your problem. It seems you have posted the whole codebase here. Try to avoid that as you might get into trouble if you so that(employer and NDA etc)
OriginalGriff 15-May-12 4:13am    
Agreed - post just the relevant code fragments - save to DB, load from DB, display. If we need more, we will ask for it.
Use the "Improve question" widget to edit your question and provide better information.
Member 8491132 15-May-12 4:14am    
OK:
public bool SaveDIBAs( string picname, IntPtr bminfo, IntPtr pixdat )
{
string Partion= @"h:\oucu\Student_Images\";
string StName=St_num;
string FullPath=Partion+StName;


Guid clsid;
if( ! GetCodecClsid( FullPath.ToString(), out clsid ) )
{
MessageBox.Show( "Unknown picture format for extension " + Path.GetExtension( FullPath.ToString() ),
"Image Codec", MessageBoxButtons.OK, MessageBoxIcon.Information );
return false;
}

IntPtr img = IntPtr.Zero;
int st = GdipCreateBitmapFromGdiDib( bminfo, pixdat, ref img );
if( (st != 0) || (img == IntPtr.Zero) )
return false;

textBox2.Text = FullPath.ToString() ;

st = GdipSaveImageToFile( img,FullPath.ToString(), ref clsid, IntPtr.Zero );
GdipDisposeImage( img );
return st == 0;

}

public void ShowFrm()
{
this.Show();
}

private void button1_Click(object sender, System.EventArgs e)
{
//St_num=stNTxt.Text ;
St_num = StudentPhoto.photo.fileName;
SaveDIBAs( this.Text, bmpptr, pixptr );
byte[] byte_Img;

//SqlConnection mySqlConn=new SqlConnection("Network Library=DBMSSOCN;"+"Data Source=82.201.143.224,2022;"+"Initial Catalog=oucu_card;"+"User ID=ousa;"+"Password=cO45_iu89");
SqlConnection mySqlConn = new SqlConnection("server=192.168.1.25;workstation id=localhost;packet size=4096; persist security info=False;user id=araslan;Password=hR@oucu1;Initial catalog=mandoobeen;min pool size=1;max pool size=10000000;connection lifetime=6000");


SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection=mySqlConn;
sqlcmd.CommandText="insert into " + StudentPhoto.photo.tableName + " (nationalNumber,pictureName,Picture) values(@nN,@pN,@pic)";

sqlcmd.Parameters.Add("@nN",System.Data.SqlDbType.NVarChar);
sqlcmd.Parameters.Add("@pic",System.Data.SqlDbType.Image);
sqlcmd.Parameters.Add("@pN",System.Data.SqlDbType.NVarChar);


FileStream fs=new FileStream(this.textBox2.Text.ToString(),FileMode.Open,FileAccess.Read,FileShare.Read);
byte_Img=new byte[Convert.ToInt32(fs.Length)];
int iBytesRead=fs.Read(byte_Img,0,Convert.ToInt32(fs.Length));
fs.Close();

sqlcmd.Parameters["@nN"].Value = StudentPhoto.photo.nationalNumber;
sqlcmd.Parameters["@pic"].Value=byte_Img;
TwainGui.Login l = new TwainGui.Login();
sqlcmd.Parameters["@pN"].Value = StudentPhoto.photo.fileName;

mySqlConn.Open();
int iresult=sqlcmd.ExecuteNonQuery();
mySqlConn.Close();


System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
Directory.SetCurrentDirectory(textBox3.Text);
Application.Exit();
}

1 solution

firstly its a request not to put this much code, it will be hard for us to solve your problem.
Secondaly storing images in database is not an good idea, even small image occupies large amount of bytes which effects the performance. However, alternatively to do this store the relative path of image into your database.
for eg c:\\images\\xyz.jpg

While to find the solution to your problem you can try following link:

http://www.aspsnippets.com/Articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx[^]

Hope you'll get solution of your answer.
thanks.
 
Share this answer
 

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