Monday, March 22, 2010

Code for upload the image and add into database

Code for upload the image using Fileupload This function save the image in the server Folder path specify In server.map path Save the image name into tabel.


DataTable dt = new DataTable();
string strimage, path;
strimage = fileuploadstphoto.FileName;
path = Server.MapPath("~/newseventphoto/");
if (!txtmod.Text.Equals(""))
{
if (!fileuploadstphoto.FileName.Equals(""))
{
fileuploadstphoto.PostedFile.SaveAs(path + fileuploadstphoto.FileName);
txtimage.Text = fileuploadstphoto.FileName;
}

// if you don't want update image and only update the data use stored procedure for update

if (obj1.updatenews(Convert.ToInt16(txtmod.Text), ddrnews.SelectedValue.ToString(), txtnewstitle.Text.Replace("'", "''").ToString(), FCKeditor1.Value.Replace("'", "''"), txtimage.Text, Session["adminid"].ToString()))
{
txtnewstitle.Text = "";
FCKeditor1.Value = "";
txtimage.Text = "";
errormsg.InnerText = "Record update Successfully";

}
else
{
errormsg.InnerText = "Record update Inserted Try Again";
}
}
else
{
if (!fileuploadstphoto.FileName.Equals(""))
{
fileuploadstphoto.PostedFile.SaveAs(path + fileuploadstphoto.FileName);
txtimage.Text = fileuploadstphoto.FileName;
}
else
{
txtimage.Text = "noimage.gif";
}

// use here stored Procedure or query For insert the data

if (obj1.insertnews(ddrnews.SelectedValue.ToString(), txtnewstitle.Text.Replace("'", "''").ToString(), FCKeditor1.Value.Replace("'", "''"), txtimage.Text, Session["adminid"].ToString(), Session["adminid"].ToString()))
{

txtnewstitle.Text = "";
FCKeditor1.Value = "";
txtimage.Text = "";
errormsg.InnerText = "Record Add Successfully";

}
else
{
errormsg.InnerText = "Record Not Inserted Try Again";
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}