无法更新本地数据库

时间:2016-12-20 06:56:20

标签: c# asp.net

我有一个问题,我已经尝试解决了两天,但无法找到解决方案。 我有一个Web表单来显示数据库中的事件详细信息..并且它正确显示它们,问题是,当用户尝试更新字段并单击保存按钮时,Web表单只保存相同的数据已从数据库中检索到。

例如,如下图所示,事件名称称为:" test1",如果用户更改为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;


    }

1 个答案:

答案 0 :(得分:2)

如果不是PostBack条件,只需添加Page_Load事件:

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

当您点击保存按钮时。一切正常。但在保存之前,将整个数据传输到原始数据。

这就是Page Life cycle的工作方式。

相关问题