我已经尝试了一些我在StackOverflow上阅读的一些其他主题并没有成功的事情。
public partial class Content_Management : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//Code fires EVERY TIME a Row is selected in the Gridview control
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//Extract upload Title and assign to relevant Textbox control
TextBox1.Text = GridView1.SelectedRow.Cells[2].Text.ToString();
//Extract upload album name and assign to relevant Textbox control
TextBox3.Text = GridView1.SelectedRow.Cells[3].Text.ToString();
//Extract upload Image and assign to relevant Image control
Image4.ImageUrl = "~/uploadedimages/" + GridView1.SelectedRow.Cells[5].Text.ToString();
//Extract upload Status and assign to DropDownList control
DropDownList1.Text = GridView1.SelectedRow.Cells[8].Text.ToString();
//Enable the "Update" Button control
Button2.Enabled = true;
//Extract upload Multimedia Clip and load into MediaPlayer control ready for playing
Media_Player_Control1.MovieURL = Server.MapPath("~/uploaded_multimedia/" + GridView1.SelectedRow.Cells[5].Text.ToString());
}
//This code will execute EVERY TIME "Save" Button is clicked
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Birdspotting"].ConnectionString);
//Then I would reference the string e.g
//Create variable to point to image upload folder
string image_folder = Server.MapPath("~\\uploadedimages\\");
//Create 2 variables and set the file names to blank in case User does not upload any files - DB entries will still be made
string dbfilename1 = "";
string dbfilename2 = "";
//Check if the Image Fileupload control has a file or not
if (FileUpload1.HasFile)
{
//If the Fileupload control has a file, change the value of the variable to the selected image file name
string filename1 = FileUpload1.FileName.ToString();
//Copy the selected Image file from the Users device accross the Internet and save it into the Image folder within the Web App structure
FileUpload1.SaveAs(image_folder + filename1);
//Assign the new file name to the variable used to populate the DB - change from "" to actual file name
dbfilename1 = filename1;
}
//Repeat as above for Multimedia Clip - audio or video
string multimedia_folder = Server.MapPath("~\\uploaded_multimedia\\");
if (FileUpload2.HasFile)
{
string filename2 = FileUpload2.FileName.ToString();
FileUpload2.SaveAs(multimedia_folder + filename2);
dbfilename2 = filename2;
}
//Create DB Connection - point to SQL
// create sql connection object. make sure to put a valid connection string
SqlCommand cmd = new SqlCommand("Insert into LeagueTable" + "(BirdName, Quantity, ArtworkImage, MediaClip, Username, Datetime, ActiveStatus, UserId)"
+ "Values(@1, @2, @3, @4, @5, @6, @7, @8) Select SCOPE_IDENTITY()");
//SqlXml newXml = new SqlXml(new XmlTextReader("Birds.xml"));
using (SqlDataAdapter sda = new SqlDataAdapter())
{
//Define the structure of the SQL Statement - used to executed instructions against the DB Table
//The DB Table name and each field is indentified in the command.
//INSERT commands includes Primary Key insertion -that is generated by the DB itself
cmd.Parameters.AddWithValue("@1", TextBox1.Text.ToString());
cmd.Parameters.AddWithValue("@2", TextBox3.Text.ToString());
cmd.Parameters.AddWithValue("@3", dbfilename1.ToString());
cmd.Parameters.AddWithValue("@4", dbfilename2.ToString());
cmd.Parameters.AddWithValue("@5", User.Identity.Name);
cmd.Parameters.AddWithValue("@6", DateTime.Now.ToString());
cmd.Parameters.AddWithValue("@7", DropDownList1.Text.ToString());
cmd.Parameters.AddWithValue("@8", System.Data.SqlDbType.Int);
cmd.Connection = conn;
try
{
conn.Open();
var added = cmd.ExecuteNonQuery();
lblConfirm.Text = added.ToString() + "Record Inserted.";
}
catch (Exception ex)
{
lblError.Text = "Error Inserting Record";
lblError.Text += ex.Message;
}
finally
{
conn.Close();
}
GridView1.DataBind();
}
}