Friday, May 27, 2011

Image upload

how to upload image -button click code written applicable only in .cs file





first on .aspx page use fileupload control then on button click how to call it example given



protected void btnsub_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
if (Page.IsPostBack)
{
string hotel_image = "";
if (imageupload.HasFile)
{

hotel_image = imageupload.PostedFile.FileName;
imageupload.SaveAs(Server.MapPath("upload") + "//" + hotel_image);
}
string Content1 = txtcontent.Text.ToString().Trim();
string Title_of_hotel = txttitle.Text.ToString().Trim();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ihotel"].ToString().Trim());
SqlCommand cmd = new SqlCommand("topfourhotel", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Content1", SqlDbType.VarChar).Value = Content1;
cmd.Parameters.Add("@Title_of_hotel", SqlDbType.VarChar).Value = Title_of_hotel;
cmd.Parameters.Add("@hotel_image", SqlDbType.VarChar).Value = hotel_image;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();

if (i > 0)
{
Response.Write("Inserted Successfully . ");
}
else
{
Response.Write("Error Occured");
}
txtcontent.Text = "";
txttitle.Text = "";

}
}
}
}

No comments:

Post a Comment