Hi
I am saving the file content of upload files in database 2005.
which files are have single and double quotes, its not uploading and it shows the error in single quotes and double quotes.
i want to trim the quotes while uploading the file and save the file content in db.
Advanx thx
Sudharsan S
Loading
Jignesh TrivediPosted Jun 14, 2012, 7:52 AM
To resolved this issue use parameterized queryin command text.
query = "INSERT INTO Fileupload([category],[subcategory],[uid],[oid],[up_name],[demo1],[keyword],[fdata],[aflag],sizebyte)
VALUES(@category,@subcategory,@uid,@oid,@up_name,@demo1,@keyword,@fdata,1,@sizebyte) ";
command.Parameters.AddWithValue("@category", dropcategory.SelectedValue);
command.Parameters.AddWithValue("@subcategory", DropDownList1.SelectedValue);
command.Parameters.AddWithValue("@uid", Session["user_id"].ToString());
command.Parameters.AddWithValue("@oid", txtOwnerId.SelectedItem);
command.Parameters.AddWithValue("@up_name", fname);
command.Parameters.AddWithValue("@demo1", demo1.Text);
command.Parameters.AddWithValue("@keyword", key);
command.Parameters.AddWithValue("@fdata", txt);
command.Parameters.AddWithValue("@sizebyte", size);
command.ExecuteNonQuery();
hope this will help you.
sudharsan sPosted Jun 20, 2012, 3:00 AM
iN NORMAL sql query why single qoutes(Special characters) are not accepted to save into database
sudharsan sPosted Jun 14, 2012, 7:06 AM
But, here i am read the upload file data and stored in database field.
So the error generated
protected void cmdAdd_Click(object sender, EventArgs e)
{
//if (getoid() != "")
//{
string filename = string.Empty;
int size1 = 0;
int size = 0;
string getval = string.Empty;
for (int i = 1; i <= Convert.ToInt32(fuhid.Value); i++)
{
SqlConnection con = new SqlConnection(constring);
con.Open();
if (i == 1)
{
filename = FileUpload1.FileName;
//string size=FileUpload1.FileBytes);//.ToString();
size1 = FileUpload1.PostedFile.ContentLength;
size = size1 / 1024;
FileUpload1.SaveAs(Server.MapPath("uploads/" + filename));
}
if (i == 2)
{
filename = FileUpload2.FileName;
//string size=FileUpload1.FileBytes);//.ToString();
size1 = FileUpload2.PostedFile.ContentLength;
size = size1 / 1024;
FileUpload2.SaveAs(Server.MapPath("uploads/" + filename));
}
if (i == 3)
{
filename = FileUpload3.FileName;
//string size=FileUpload1.FileBytes);//.ToString();
size1 = FileUpload3.PostedFile.ContentLength;
size = size1 / 1024;
FileUpload3.SaveAs(Server.MapPath("uploads/" + filename));
}
string fname = Path.GetFileName(filename);
//string myDateTime=demo1.Text;
//DateTime myDateTime = DateTime.Parse(myDate);
string query = string.Empty;
//string res = DropDownList1.SelectedItem as string;
string extn = Path.GetExtension(filename);
string key = keyword1.Text;
if (extn.Equals(".txt"))
{
TextReader tr = new StreamReader(Server.MapPath("uploads/" + filename));
query = "INSERT INTO Fileupload([category],[subcategory],[uid],[oid],[up_name],[demo1],[keyword],[fdata],[aflag],sizebyte) VALUES('" + dropcategory.SelectedValue + "','" + DropDownList1.SelectedValue + "','" + Session["user_id"].ToString() + "','" + txtOwnerId.SelectedItem + "','" + fname + "','" + demo1.Text + "','" + key + "','" + tr.ReadToEnd().TrimEnd() + "',1,'" + size + "') ";
// create reader & open file
}
else if (extn.Equals(".doc"))
{
Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileName = Server.MapPath("uploads/" + filename);
object readOnly = true;
object isVisible = false;
object missing = System.Reflection.Missing.Value;
//Word.ApplicationClass oWordApp = new Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
//oWordDoc.Activate();
oWordDoc.ActiveWindow.Selection.WholeStory();
oWordDoc.ActiveWindow.Selection.Copy();
string txt = oWordDoc.Content.Text;
query = "INSERT INTO Fileupload([category],[subcategory],[uid],[oid],[up_name],[demo1],[keyword],[fdata],[aflag],sizebyte) VALUES('" + dropcategory.SelectedValue + "','" + DropDownList1.SelectedValue + "','" + Session["user_id"].ToString() + "','" + txtOwnerId.SelectedItem + "','" + fname + "','" + demo1.Text + "','" + key + "','" + txt + "',1,'" + size + "') ";
}
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();// ExecuteScalar().ToString();
//}
}
Jignesh TrivediPosted Jun 14, 2012, 7:01 AM
Can you please share piece of code?
so we can help you.
As per my knowledge does matter files with single and double quotes.