protected void DetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
FileUpload FileUploadControl = DetailsView.FindControl("FileUpload1") as FileUpload;
if (FileUploadControl.HasFile && e.Values["Name"] != null)
{
string Name = e.Values["Name"].ToString();
try
{
string FileName = Path.GetFileName(FileUploadControl.FileName);
string MapPath = "~/Images/" + FileName;
FileUploadControl.SaveAs(Server.MapPath("~/Images/") + FileName);
byte[] imgdata = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(MapPath));
SqlConnection myConnection;
string myConnectionString = "Initial Catalog=42HNetDb;Data Source=localhost;Integrated Security=SSPI";
myConnection = new SqlConnection(myConnectionString);
string myInsertQuery = "INSERT INTO Client (Name,Logo) Values(@Name,@Logo )";
SqlCommand myCommand = new SqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
myCommand.Parameters.Add("@Name", SqlDbType.NVarChar);
myCommand.Parameters["@Name"].Value = Name;
myCommand.Parameters.Add("@Logo", SqlDbType.Image);
myCommand.Parameters["@Logo"].Value = imgdata;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
else
{
string script = "alert('Please Insert all the mandatory Fields');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true); //gets executed properly
}
Search This Blog
Wednesday, May 22, 2013
Details View Item Inserting
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment