Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (gvAttachments.Rows.Count > 0)
          {
              string[] Records = Session["FileDetail"].ToString().Split('#');
              for (int i = 0; i <Records.Length; i++)
              {
                  string[] arrRecords = Records[i].Split(',');
                  string[] fileExtension = arrRecords[0].Split('.');
                  string documentType = string.Empty;
                  switch (fileExtension[1])
                  {
                      case : documentType = "application/pdf";
                          break;
                      case : documentType = "application/vnd.ms-excel";
                          break;
                      case : documentType = "application/vnd.ms-excel";
                          break;
                      case : documentType = "application/vnd.ms-word";
                          break;
                      case "docx": documentType = "application/vnd.ms-word";
                          break;
                      case "gif": documentType = "image/gif";
                          break;
                      case "png": documentType = "image/png";
                          break;
                      case "jpg": documentType = "image/jpg";
                          break;
                  }
                  string size = arrRecords[2];
                  int fileSize = Convert.ToInt32(size);
                  byte[] documentbinary = new byte[fileSize];
                  fuUpload.PostedFile.InputStream.Read(documentbinary, 0, fileSize);
                  string file = arrRecords[0].ToString();
                  string format = documentType.ToString();
                  cmd.CommandText = "Insert into Attachment(File_Name,File_Format,File_Content,User_Notice_Mapping_ID) values('" + file + "','" + format + "',@DocData," + userNoticeID + ")";
                  cmd.CommandType = CommandType.Text;
                  cmd.Connection = con;
                  cmd.Parameters.Add("@DocData", SqlDbType.VarBinary, fileSize);
                  cmd.Parameters["@DocData"].Value = documentbinary;
                  cmd.ExecuteNonQuery();
                  String msg = "Your message has been Sent Successfully";
                  ShowAlert(msg, true);
              }
          }
Posted
Updated 24-Apr-14 20:48pm
v2

1 solution

1.Your problem is that your are using the same SqlCommand object for all items of your for, so the command object already have the parameter after its first iteration.

2.The solution is to create the SqlCommand object local inside the for:


C#
for (int i = 0; i <Records.Length; i++)
              {
                  string[] arrRecords = Records[i].Split(',');
                  string[] fileExtension = arrRecords[0].Split('.');
                  string documentType = string.Empty;
                  switch (fileExtension[1])
                  {
                      case : documentType = "application/pdf";
                          break;
                      case : documentType = "application/vnd.ms-excel";
                          break;
                      case : documentType = "application/vnd.ms-excel";
                          break;
                      case : documentType = "application/vnd.ms-word";
                          break;
                      case "docx": documentType = "application/vnd.ms-word";
                          break;
                      case "gif": documentType = "image/gif";
                          break;
                      case "png": documentType = "image/png";
                          break;
                      case "jpg": documentType = "image/jpg";
                          break;
                  }
                  string size = arrRecords[2];
                  int fileSize = Convert.ToInt32(size);
                  byte[] documentbinary = new byte[fileSize];
                  fuUpload.PostedFile.InputStream.Read(documentbinary, 0, fileSize);
                  string file = arrRecords[0].ToString();
                  string format = documentType.ToString();
                  //
                  cmd = new SqlCommand(); //Must be a new SqlCommand for each item! 
                  // 
                  cmd.CommandText = "Insert into Attachment(File_Name,File_Format,File_Content,User_Notice_Mapping_ID) values('" + file + "','" + format + "',@DocData," + userNoticeID + ")";
                  cmd.CommandType = CommandType.Text;
                  cmd.Connection = con;
                  cmd.Parameters.Add("@DocData", SqlDbType.VarBinary, fileSize);
                  cmd.Parameters["@DocData"].Value = documentbinary;
                  cmd.ExecuteNonQuery();
                  String msg = "Your message has been Sent Successfully";
                  ShowAlert(msg, true);
              }
 
Share this answer
 
Comments
Neha Mukesh 25-Apr-14 5:32am    
Thanks a Lot!!!
Raul Iloc 25-Apr-14 6:20am    
Welcome!
Neha Mukesh 25-Apr-14 5:33am    
I have one more question .how to delete files from temporary folder which are uploaded more than one hour ago??
Raul Iloc 25-Apr-14 6:24am    
You have to use "FileInfo"class and look at "LastWriteTime" or "CreationTime" property.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900