tag:blogger.com,1999:blog-9075786158122089104.post3186624532541983900..comments2023-10-23T19:47:51.926+05:30Comments on SQL-Server Blog of Ritesh Shah --Fight the fear of SQL with SQLHub.com: Image store in SQL-Server 2005 database and retrieve it in asp.net application with C#:Ritesh Shahhttp://www.blogger.com/profile/09562656548324468450noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-9075786158122089104.post-33439929915165750642013-03-05T19:04:39.795+05:302013-03-05T19:04:39.795+05:30Hello Sir, I"m trying to retrieve image from ...Hello Sir, I"m trying to retrieve image from database and want to show it on Image control in asp.net. But it's getting an error ""<br /><br /> SqlCommand cm = new SqlCommand("select * from asdf where img_id='" + DropDownList1.SelectedItem.ToString() + "'", cn);//image id will be selected.<br /> SqlDataAdapter da = new SqlDataAdapter(cm);<br /> DataSet ds = new DataSet();<br /> da.Fill(ds, "asdf");<br /> SqlDataReader dr = cm.ExecuteReader();<br /> if (dr.Read())<br /> {<br /><br /> string image1 = Convert.ToString(DateTime.Now.ToFileTime());<br /> image1 = "~/E:\\Visual Studio 2010\\WebSites\\WebSite7\\images/" + image1 + ".jpeg";<br />FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);<br />byte[] bimage1 = (byte[])ds.Tables[0].Rows[0]["passport_photo"];<br />fs1.Write(bimage1, 0, bimage1.Length);<br />Image1.ImageUrl = "~/E:\\Visual Studio 2010\\WebSites\\WebSite7\\images/";Chiklu.Soumyanoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-90600613285670826202012-08-16T23:58:22.598+05:302012-08-16T23:58:22.598+05:30I tried this method for a txt files, and it worked...I tried this method for a txt files, and it worked great! Thanks Ritesh Shah! You made my day.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-57078158016919240522012-07-27T10:43:14.952+05:302012-07-27T10:43:14.952+05:30hi i write one code for save doc file in sqlserver...hi i write one code for save doc file in sqlserver and retrieve.<br /><br />save:<br />string name = @"C:\Documents and Settings\icoresoft\Desktop\murali.doc";<br /><br /> FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Read);<br /> BinaryReader br = new BinaryReader(fs);<br /><br /> byte[] bname = new byte[fs.Length];<br /> fs.Read(bname, 0, Convert.ToInt32(fs.Length));<br /><br /> tbal.my_doc = bname;<br /> string i = tdal.createfile(tbal);<br /> if (i == "1")<br /> {<br /> MessageBox.Show("saved");<br /> }<br /><br /><br />retrive:<br /><br /> byte[] myByte = new byte[0]; <br /> tbal.id_ = 1;<br /> SqlDataReader dr = tdal.getvalue(tbal);<br /> dr.Read();<br /> if (dr.HasRows)<br /> {<br /> myByte =(byte[])dr["mydoc"];<br /> }<br /><br /> FileStream fs1 = new FileStream(@"C:\Documents and Settings\icoresoft\Desktop\murali1.doc", FileMode.OpenOrCreate, FileAccess.Write);<br /> BinaryWriter br1 = new BinaryWriter(fs1);<br /> br1.Write(myByte);<br /><br /><br />NOW I OPEN MY WORD FILE MEANS ERROR COMES.<br /><br />ERROR:YOUR FILE CORRUPTED. SO HELP ME HOW CAN I VIEW MY TEXT FILE...PLEASE SEND YOUR ANSWER TO murali.bala.job@gmail.comUnknownhttps://www.blogger.com/profile/05330100057848673039noreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-80989499603064396072011-12-14T07:28:39.057+05:302011-12-14T07:28:39.057+05:30Thanks a lot... it really helped me. It works usin...Thanks a lot... it really helped me. It works using sql server 2008.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-4856641622534679792011-06-08T06:13:08.691+05:302011-06-08T06:13:08.691+05:30Hello sir,
How can i upload and retrieve excel fil...Hello sir,<br />How can i upload and retrieve excel file in the same application..please help me....its urgent. In your code any image file working fine...but i want store and retrieve excel file....please helpthomashttps://www.blogger.com/profile/02364546610051701770noreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-82831534099328909612011-04-29T15:45:01.979+05:302011-04-29T15:45:01.979+05:30Hello, I just used the first part of your code for...Hello, I just used the first part of your code for storing images, but after execution of this code there's no change in my database tables, they remain null. Could you please check? Here's my code behind:<br /><br />using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Web;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Drawing;<br />using System.Data.SqlClient;<br />using System.IO;<br />using System.Data;<br /><br /><br />public partial class _Default : System.Web.UI.Page<br />{<br /> protected void Page_Load(object sender, EventArgs e)<br /> {<br /><br /> }<br /> protected void Button1_Click(object sender, EventArgs e)<br /> {<br /> string strImageName = Path.GetFileName(FileUploadControl.FileName);<br /> FileUploadControl.SaveAs(Server.MapPath("~/") + strImageName);<br /> <br /><br /> Bitmap bNewImage = new Bitmap(strImageName);<br /> FileStream fs = new FileStream(strImageName, FileMode.Open, FileAccess.Read);<br /> //creating byte array to read image<br /><br /> byte[] bImage = new byte[fs.Length];<br /><br /> //this will store images.jp in bImage byte array<br /><br /> fs.Read(bImage, 0, Convert.ToInt32(fs.Length));<br /><br /> fs.Close();<br /> fs = null;<br /><br /> //open the database using odp.net and insert the data<br /><br /> string connstr = "Server=WINSP3UE\\SqlExpress;Database=MovieTestDB;Trusted_Connection=True;";<br /> SqlConnection conn = new SqlConnection(connstr);<br /> conn.Open();<br /> string strQuery;<br /> <br /> strQuery = "INSERT INTO ImageStore (ID, ImageContent) values (" + "1," + " @pic)";<br /><br /> SqlParameter ImageParameter= new SqlParameter();<br /> ImageParameter.SqlDbType = SqlDbType.Image;<br /> ImageParameter.ParameterName = "pic";<br /> ImageParameter.Value = bImage;<br /><br /> <br /><br /> SqlCommand cmd = new SqlCommand(strQuery, conn);<br /> cmd.Parameters.Add(ImageParameter);<br /> cmd.ExecuteNonQuery();<br /> Response.Write("Image has been added to database successfully");<br /><br /> <br /><br /> cmd.Dispose();<br /> conn.Close();<br /> conn.Dispose();<br /><br /><br /> }<br />}<br /><br /><br />Thank you so much!kinonoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-16059340419162234092011-04-29T15:43:05.209+05:302011-04-29T15:43:05.209+05:30Hello, I just used the first part of your code for...Hello, I just used the first part of your code for storing images, but after execution of this code there's no change in my database tables, they remain null. Could you please check? Here's my code behind:<br /><br />using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Web;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Drawing;<br />using System.Data.SqlClient;<br />using System.IO;<br />using System.Data;<br /><br /><br />public partial class _Default : System.Web.UI.Page<br />{<br /> protected void Page_Load(object sender, EventArgs e)<br /> {<br /><br /> }<br /> protected void Button1_Click(object sender, EventArgs e)<br /> {<br /> string strImageName = Path.GetFileName(FileUploadControl.FileName);<br /> FileUploadControl.SaveAs(Server.MapPath("~/") + strImageName);<br /> <br /><br /> Bitmap bNewImage = new Bitmap(strImageName);<br /> FileStream fs = new FileStream(strImageName, FileMode.Open, FileAccess.Read);<br /> //creating byte array to read image<br /><br /> byte[] bImage = new byte[fs.Length];<br /><br /> //this will store images.jp in bImage byte array<br /><br /> fs.Read(bImage, 0, Convert.ToInt32(fs.Length));<br /><br /> fs.Close();<br /> fs = null;<br /><br /> //open the database using odp.net and insert the data<br /><br /> string connstr = "Server=WINSP3UE\\SqlExpress;Database=MovieTestDB;Trusted_Connection=True;";<br /> SqlConnection conn = new SqlConnection(connstr);<br /> conn.Open();<br /> string strQuery;<br /> <br /> strQuery = "INSERT INTO ImageStore (ID, ImageContent) values (" + "1," + " @pic)";<br /><br /> SqlParameter ImageParameter= new SqlParameter();<br /> ImageParameter.SqlDbType = SqlDbType.Image;<br /> ImageParameter.ParameterName = "pic";<br /> ImageParameter.Value = bImage;<br /><br /> <br /><br /> SqlCommand cmd = new SqlCommand(strQuery, conn);<br /> cmd.Parameters.Add(ImageParameter);<br /> cmd.ExecuteNonQuery();<br /> Response.Write("Image has been added to database successfully");<br /><br /> <br /><br /> cmd.Dispose();<br /> conn.Close();<br /> conn.Dispose();<br /><br /><br /> }<br />}<br /><br /><br />Thank you so much!kinonoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-58795689889366168232009-10-19T21:31:30.920+05:302009-10-19T21:31:30.920+05:30Excellent, just the example I was looking for. I ...Excellent, just the example I was looking for. I converted the code to vb.net since that is what we use, but it worked perfectly. Thank you!Davenoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-80238583393240794932009-10-14T16:28:13.847+05:302009-10-14T16:28:13.847+05:30I am 100% sure you have made mistake either while ...I am 100% sure you have made mistake either while converting image to byte or while converting byte to image. as this is the only possibility.Ritesh Shahhttps://www.blogger.com/profile/09562656548324468450noreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-20171801633154016592009-10-14T16:25:23.079+05:302009-10-14T16:25:23.079+05:30Hi, Ritesh I have Used ur code for retreiving the ...Hi, Ritesh I have Used ur code for retreiving the Image from the database. The file is creating well,<br />But there is no image into that. and further the code is not giving any error too..Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-20264046699772150302009-10-10T06:06:40.848+05:302009-10-10T06:06:40.848+05:30Hi Ani,
As long as PDF concern, you can store it ...Hi Ani,<br /><br />As long as PDF concern, you can store it in VarBinary field and can give same treatment I saw you in article for Image. <br /><br />About Web page, can you please be more specific? I don't understand your second question.<br /><br />RiteshRitesh Shahhttps://www.blogger.com/profile/09562656548324468450noreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-38674850386408830502009-10-09T23:37:56.894+05:302009-10-09T23:37:56.894+05:30Ritesh
How can I to open a pdf file from BLOB fie...Ritesh<br /><br />How can I to open a pdf file from BLOB field ?<br /><br />How can I open in a web page or open diretly with web page?Ashish Kanoongohttps://www.blogger.com/profile/06318917800859175845noreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-41447351492062546152009-09-10T12:02:18.716+05:302009-09-10T12:02:18.716+05:30Well you can generate it in any temporary folder o...Well you can generate it in any temporary folder of your website, display in your website from that temporary folder and delete from it whenever you will finish work on it.Ritesh Shahhttps://www.blogger.com/profile/09562656548324468450noreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-43580824184106122012009-09-10T11:45:37.594+05:302009-09-10T11:45:37.594+05:30hi,
while retriving image from database ,u r stori...hi,<br />while retriving image from database ,u r storing it in D drive.But i want to retrive image directly into webpage. can u help me in coding part.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-92037872694406664612009-09-10T11:40:17.555+05:302009-09-10T11:40:17.555+05:30hi,
while retriving image from database ,u r stori...hi,<br />while retriving image from database ,u r storing it in D drive.But i want to retrive image directly into webpage. can u help me in coding part.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-51305965175664117112009-09-07T14:48:06.477+05:302009-09-07T14:48:06.477+05:30well you can go to below link at my friend's b...well you can go to below link at my friend's blog which will help you for this matter.<br /><br />http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/<br /><br />go to 4) Enable Remote Connection in above link.Ritesh Shahhttps://www.blogger.com/profile/09562656548324468450noreply@blogger.comtag:blogger.com,1999:blog-9075786158122089104.post-5358610480894247962009-09-07T14:45:09.045+05:302009-09-07T14:45:09.045+05:30hi,
while am executing the program its showing th...hi, <br />while am executing the program its showing this error <br />"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"<br />can u help me?Anonymousnoreply@blogger.com