Link to home
Start Free TrialLog in
Avatar of minglelinch
minglelinch

asked on

data type for attached file

My application need to have ability to attach files, display the names of the selected files, and to be saved to DB.

What data type should be defined in DB to store the column for the attached file?

I appreciate if there's an example for the above processes/functions.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of s_chilkury
s_chilkury
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of minglelinch
minglelinch

ASKER

Thank you both, I'm flollowing your direction. Here's my file upload code -

                string filename = Path.GetFileName(FileUpload1.FileName);
                intDocLen =FileUpload1.PostedFile.ContentLength;
                Docbuffer = new byte[intDocLen];
                objStream = FileUpload1.PostedFile.InputStream;
                objStream.Read(Docbuffer, 0, intDocLen);                         //Docbuffer gets {byte[37849]}
               
                insStmt = "Insert into AttachFiles(claimId, doc, docName) Values (";
                insStmt += claimIdent;
                insStmt += ",";
                insStmt += Docbuffer;                                                     //Passed the value {byte[37849]}
                insStmt += ",'";
                insStmt += filename;
                insStmt += "')";                    

                                                                            // but insStmt is:
                                                                            // Insert into AttachFiles(claimId, doc, docName)
                                                                            // Values (64,System.Byte[],'E-Matrix.doc')
                                                                            // Error: ... The name "System.Byte" is not
                                                                            //          permitted in this context ...
                string sqlConnstring = "";
                SqlConnection sqlConn = null;
                SqlCommand cmd = null;

                sqlConnstring = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
                sqlConn = new SqlConnection(sqlConnstring);
                cmd = new SqlCommand();
                cmd.Connection = sqlConn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = insStmt;
                sqlConn.Open();
                cmd.ExecuteNonQuery();

error msg: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name. The name "System.Byte" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

I still have the problem for saving the uploaded attachment. Please help.
najam_ftp:
If I change to use the following code, I have change numBytes from int to long due to compile error. However, ReadBytes only take int parameter. I still got compile error. ???

                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                long numBytes = (long)new FileInfo(fileName).Length;
                byte[] buff = br.ReadBytes(numBytes);
you can resize buffer, take a look at this http://www.yoda.arachsys.com/csharp/readbinary.html
article for reading file .