如果没有任何带撇号的值,则此代码片段可以正常运行。但是,如果ddList.SelectedValue是“Women's day”之类的值,则会显示“Incorrect syntax near 's'. Unclosed quotation mark after the character string ''." 我尝试了albumName.Replace,但目前还没有成功。
protected void btnUpload_Click(object sender, EventArgs e)
{
string albumName = ddList.SelectedValue.ToString();
albumName.Replace("'", "''");
conn.Open();
SqlCommand command = new SqlCommand("select ID from Album where AlbumName = '" + albumName + "'", conn);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
AlbmID = Int32.Parse((reader["ID"].ToString()));
}
reader.Close();
if (fileuploadimages.HasFile == false)
{ ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", "<script>alert('No File Uploaded.')</script>", false); }
else
{
//Get Filename from fileupload control
string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
//Save images into SlideImages folder
fileuploadimages.SaveAs(Server.MapPath("Pictures/" + filename));
//Open the database connection
//Query to insert images name and Description into database
SqlCommand cmd = new SqlCommand("Insert into Images(ImagePath,Album_ID,ImageDesc) values(@ImagePath,@Album_ID,@ImageDesc)", conn);
//Passing parameters to query
cmd.Parameters.AddWithValue("@ImagePath", filename);
cmd.Parameters.AddWithValue("@Album_ID", AlbmID);
cmd.Parameters.AddWithValue("@ImageDesc", txtDescription.Text);
cmd.ExecuteNonQuery();
//Close dbconnection
conn.Close();
txtDescription.Text = "";
BindDataList();
}
}