2016-12-20 39 views
0

我有一个问题,我一直试图解决两天,但无法弄清楚它的解决方案。 我有一个Web窗体显示数据库中的事件详细信息..它正确显示它们,问题是,当用户尝试更新字段并单击保存按钮时,Web窗体只保存相同的从数据库中检索的数据。无法更新本地数据库

例如,如在所谓的事件名称如下图所示:“测试1”,而如果用户改为test2的,它不会改变,它会与test1的

enter image description here

被卡住实际上,我看不出代码有什么问题,他们只是不保存更新而不显示任何错误。下面

的代码背后节省更新按钮:

protected void savebtn_Click(object sender, EventArgs e) 
    { 


     command.CommandText = ""; 
     //System.Diagnostics.Debug.WriteLine(name.Value); 


     string latitude = ""; 
     string longitude = ""; 
     string imagesArr = null; 
     bool fields = false; 

     string eventname = name.Text; 

     string disc = Request.Form["TextArea3"].ToString(); 
     string startDate = DropDownList1.Text + "/" + DropDownList7.Text + "/" + DropDownList2.Text; 
     string endDate = DropDownList4.Text + "/" + DropDownList6.Text + "/" + DropDownList5.Text; 
     string eventduration = duration.Value; 
     string eventadmission = admission.Value; 
     string categ = DropDownList3.Text; 
     string contact = Request.Form["TextArea4"].ToString(); 
     string eventwebsite = website.Value; 
     string eventvenue = venue.Value; 
     string eventcountry = country.Value; 
     //string venue = venuetxt.Text; 

     if (Session["Latitude"].ToString() == null && Session["Longitude"].ToString() == null) 
     { 
      latitude = ""; 
      longitude = ""; 
     } 
     else if (Session["Latitude"].ToString() != null && Session["Longitude"].ToString() != null) 
     { 
      latitude = Session["Latitude"].ToString(); 
      longitude = Session["Longitude"].ToString(); 
     } 

     if (Session["ImagesArray"].ToString() != null) 
     { 
      imagesArr = Session["ImagesArray"].ToString(); 
     } 



     if (DropDownList2.Text.Equals("Year") || DropDownList1.Text.Equals("Month") || DropDownList6.Text.Equals("Day") || DropDownList5.Text.Equals("Year") || DropDownList4.Text.Equals("Month") || DropDownList7.Text.Equals("Day") || eventname == null || disc == null || eventduration == null || eventadmission == null || categ == null || contact == null || eventwebsite == null || eventvenue == null) 
     { 
      fields = true; 
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(' Make sure to enter all the required data')", true); 
     } 
     else { fields = false; } 

     if (fields == false) 
     { 

       command.CommandText = "Update EventsEnglish SET EventName = '" + eventname + "', EventDescription = '" + TextArea3.Value + "', EventStartDate = '" + startDate + "', EventEndDate = '" + endDate + "', EventDuration = '" + duration.Value + "', EventAdmission = '" + admission.Value + "', EventCategory = '" + categ + "', EventContact = '" + TextArea4.Value + "', EventWebsite = '" + website.Value + "', EventVenue = '" + venue.Value + "', EventMapLatitude = '" + latitude + "', EventMapLongitude = '" + longitude + "', CountryName = '" + eventcountry + "', EventImages = '" + imagesArr + "' WHERE EventID = '" + id + "'"; 

      //command.ExecuteNonQuery(); 
      int result = command.ExecuteNonQuery(); 
       if (result == 1) 
       { 


       ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(' Event has been updated ')", true); 
        Server.Transfer("ViewEventsEng.aspx"); 

       } 

      con.Close(); 
     } 
    } 

有关详细信息,这是我的网页加载代码:

protected void Page_Load(object sender, EventArgs e) 
    { 




     this.Form.Target = "_blank"; 
     DropDownList2.Items.Add("Year"); 
     DropDownList5.Items.Add("Year"); 
     int year = 0; 

     for (int y = 0; y <= DateTime.Now.Year; y++) 
     { 

      if (y >= 2015) 
      { 
       year = y + 1; 
       Console.WriteLine("year is: " + year); 
       DropDownList2.Items.Add(year.ToString()); 
       DropDownList5.Items.Add(year.ToString()); 
       Console.WriteLine("year is: " + year); 

      } 


     } 
     id = Request.QueryString["EventID"]; 
     eventID.Value = id; 
     con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:MY DATABASE;Integrated Security=True;Connect Timeout=30"); 
     con.Open(); 
     command = con.CreateCommand(); 
     command.CommandText = "SELECT * FROM EventsEnglish WHERE EVENTID = '"+ id + "'"; 
     reader = command.ExecuteReader(); 
     while (reader.Read()) 
     { 
      name.Text = reader.GetValue(1).ToString(); 
      TextArea3.InnerText = reader.GetValue(2).ToString(); 

      string fullDate = reader.GetValue(3).ToString(); 


      string[] subs = fullDate.Split('/'); 
      string month = subs[0]; 
      string day = subs[1]; 
      string yearstart = subs[2]; 


      DropDownList1.Text = subs[0]; 
      DropDownList7.Text = subs[1]; 
      DropDownList2.Text = subs[2]; 


      string fullDateEnd = reader.GetValue(4).ToString(); 
      string[] subsend = fullDateEnd.Split('/'); 
      string monthend = subsend[0]; 
      string dayend = subsend[1]; 
      string yearstartend = subsend[2]; 
      DropDownList4.Text = subsend[0]; 
      DropDownList6.Text = subsend[1]; 
      DropDownList5.Text = subsend[2]; 


      duration.Value = reader.GetValue(5).ToString(); 
      admission.Value = reader.GetValue(6).ToString(); 
      DropDownList3.Text = reader.GetValue(7).ToString(); 
      TextArea4.InnerText = reader.GetValue(8).ToString(); 
      website.Value = reader.GetValue(9).ToString(); 
      venue.Value = reader.GetValue(10).ToString(); 
      propLat = reader.GetValue(11).ToString(); 
      propLan = reader.GetValue(12).ToString(); 
      country.Value = reader.GetValue(13).ToString(); 
      List<string> images = new List<string>(); 

      string imagesDB = reader.GetValue(14).ToString(); 





     } 
     reader.Close(); 

     command.CommandText = null; 


    } 
+2

您的SQL语句很容易出现SQL注入。 –

+0

'实际上,我看不出代码有什么问题'但是我可以在代码中指出太多错误的东西,很差的命名约定,太多不需要/不必要的变量,非常不好的串联字符串查询,错误地处理连接,等等,等等...... –

+1

好,谢谢你的评论。但是,我不认为你提到的可能是更新不起作用的原因。有了这个问题,我只想知道为什么我不能将更新保存到我的数据库。 – user3599431

回答

2

只需在您Page_Load事件添加如果不回发的条件:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     ... your entire Page Load Code Here ... 
    } 
} 

当你点击保存按钮时,一切都会好起来的。但是befo重新保存它将获取整个数据到原始数据。

这就是Page Life cycle的工作原理。