We get a problem when inserting image into sql table.
columns name is
thumbnail_url and column type is
image and we have to insert in it from an xml file.
but the main problem is that in xml file thumbnail_url is varchar like
<thumbnail_url>http://test.com/images/test.jpg </thumbnail_url>
we have used code for that
protected void Page_Load(object sender, EventArgs e)
{
}
static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}
static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
protected void btn_Upload_Click(object sender, EventArgs e)
{
try
{
DataSet dsxml = new DataSet();
dsxml.ReadXml("C:\\Documents and Settings\\madhyam1\\Desktop\\Property XML\\propertyfeedHomecouk.xml");
string d = ConfigurationManager.AppSettings["ConnectionString"] + "";
SqlConnection sqlcon = new SqlConnection(d);
sqlcon.Open();
for (int i = 0; i <= dsxml.Tables[0].Rows.Count; i++)
{
string url = dsxml.Tables[0].Rows[i][6].ToString();
if (!string.IsNullOrEmpty(url))
{
string DestinationPath = "C:/Temp";
string filename = url.Substring(url.LastIndexOf('/') + 1);
byte[] bytes = GetBytesFromUrl(url);
WriteBytesToFile(DestinationPath + "/" + i + ".jpg", bytes);
SqlCommand cmd = new SqlCommand(@"INSERT INTO Property_details
(price
,bedrooms
,type
,location
,postcode
,url
,thumbnail_url
,agent_id
,agent_url
,lat
,long
,developmentid
,description
,CustomerId
) VALUES('" + dsxml.Tables[0].Rows[i][0] + "','" + dsxml.Tables[0].Rows[i][1] + "','" + dsxml.Tables[0].Rows[i][2] + "','" + dsxml.Tables[0].Rows[i][3] + "','" + dsxml.Tables[0].Rows[i][4] + "','" + dsxml.Tables[0].Rows[i][5] + "','" + dsxml.Tables[0].Rows[i][6] + "','" + dsxml.Tables[0].Rows[i][7] + "','" + dsxml.Tables[0].Rows[i][8] + "','" + dsxml.Tables[0].Rows[i][9] + "','" + dsxml.Tables[0].Rows[i][10] + "','" + dsxml.Tables[0].Rows[i][11] + "','" + dsxml.Tables[0].Rows[i][12].ToString().Replace("'", ".") + "',0)", sqlcon);
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
Through the above code images is stored into Temp folder of C drive
from the link of xml file but
we have to insert into DATABAE.
Please Anyone help me.
Thanks.
[edit]Code block added - OriginalGriff[/edit]