Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey all,

I'm using c# windows application and i want to save file in server folder name(files) instead of directly database.I also want to retrieve save files from server using datagridview. I'm facing error when i try to send my code to you. Kindly suggest me any authentic link or solution ASAP.

Thanks
Imran

What I have tried:

SqlDataAdapter objAdapter = new SqlDataAdapter(strQuery_AllAttachments_AllFields, objConn);
            objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            SqlCommandBuilder objCmdBuilder = new SqlCommandBuilder(objAdapter);
            DataTable objTable = new DataTable();
            FileStream objFileStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
            int intLength = Convert.ToInt32(objFileStream.Length);
            byte[] objData;
            
            objData = new byte[intLength];
            DataRow objRow;
            string[] strPath = strFile.Split(Convert.ToChar(@"\"));
            objAdapter.Fill(objTable);

            objFileStream.Read(objData, 0, intLength);
            objFileStream.Close();

            objRow = objTable.NewRow();
            objRow["FileName"] = strPath[strPath.Length - 1]; //clip the full path - we just want last part!
            objRow["FileSize"] = intLength / 5008;  // KB instead of bytes
            objRow["CustData"] = objData;  //our file
            objRow["CustName"] = comboBox1.Text.ToString();
            objRow["CaseName"] = textBox1.Text.ToString();
            objRow["Remarks"] = textBox3.Text.ToString();
            objRow["Date"] = dateTimePicker1.Value.ToShortDateString();
            objTable.Rows.Add(objRow); //add our new record
            objAdapter.Update(objTable);
Posted
Updated 1-Apr-19 2:13am
Comments
Mohammad Imran1 1-Apr-19 5:49am    
This is not full code, i'm getting space/long code error when tried to upload whole code. Kindly let me know the full solution i'm new in this field.
Richard Deeming 4-Apr-19 13:36pm    
You've told us you're getting an error, but you haven't told us what that error is, or which line of your code it relates to.

Without that information, nobody can help you.

Click the green "Improve question" link and add the full details of the error to your question.

1 solution

What is an issue with your code ?

You can save the file reference (file path) in database with reference to documentID (specific ID) and then you can access file with the same file path.

How you are storing the path

e.g.

Server.MapPath("/Folder/file.txt")

or

"Folder/File.txt" only ?
 
Share this answer
 
Comments
Mohammad Imran1 1-Apr-19 8:14am    
This is my second file saving code .

private void CreateAttachment(string strFile)
{
SqlDataAdapter objAdapter = new SqlDataAdapter(strQuery_AllAttachments_AllFields, objConn);
objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder objCmdBuilder = new SqlCommandBuilder(objAdapter);
DataTable objTable = new DataTable();
FileStream objFileStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
int intLength = Convert.ToInt32(objFileStream.Length);
byte[] objData;

objData = new byte[intLength];
DataRow objRow;
string[] strPath = strFile.Split(Convert.ToChar(@"\"));
objAdapter.Fill(objTable);

objFileStream.Read(objData, 0, intLength);
objFileStream.Close();

objRow = objTable.NewRow();
objRow["FileName"] = strPath[strPath.Length - 1]; //clip the full path - we just want last part!
objRow["FileSize"] = intLength / 5008; // KB instead of bytes
objRow["CustData"] = objData; //our file
objRow["CustName"] = comboBox1.Text.ToString();
objRow["CaseName"] = textBox1.Text.ToString();
objRow["Remarks"] = textBox3.Text.ToString();
objRow["Date"] = dateTimePicker1.Value.ToShortDateString();
objTable.Rows.Add(objRow); //add our new record
objAdapter.Update(objTable);

}
Mohammad Imran1 4-Apr-19 6:03am    
any update anyone can help please???

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