Uploading Multiple image through file upload and saving to database ASP.NET

  1. < asp: FileUpload AllowMultiple = "true"  
  2. ID = "fileuploadimages"  
  3. runat = "server" / > < asp: Button runat = "server"  
  4. ID = "btnUpload"  
  5. CssClass = "btnStyle"  
  6. Text = "Upload Image"  
  7. OnClick = "btnUpload_Click" / >  
  8.   
  9.   
  10.   
  11. protected void btnUpload_Click(object sender, EventArgs e)  
  12. {  
  13.     if (fileuploadimages.HasFile == false)   
  14.     {  
  15.         ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", "  
  16. <script>alert('No File Uploaded.')</script>", false);  
  17.     }  
  18.     else  
  19.     {  
  20.         foreach(var file in fileuploadimages.PostedFiles)  
  21.         {  
  22.             string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);  
  23.             fileuploadimages.SaveAs(Server.MapPath("../Pictures/" + filename));  
  24.             SqlCommand cmd = new SqlCommand("Insert into  
  25. EventPageView(Event_name,Event_Text,Image_Path)  
  26. values(@EventName,@EventText,@ImagePath)", conn);  
  27.             cmd.Parameters.AddWithValue("@ImagePath", filename);  
  28.             cmd.Parameters.AddWithValue("@EventName", txtEventName.Text);  
  29.             cmd.Parameters.AddWithValue("@EventText", txtEnterDesc.Text);  
  30.             conn.Open();  
  31.             cmd.ExecuteNonQuery();  
  32.             conn.Close();  
  33.             BindDataList();  
  34.         }  
  35.     }  
  36. }