user3599431
user3599431

Reputation: 43

Cannot update local database

I have an issue I've been trying to solve for two days, but couldn't figure out the solution for it. I have a web form to show an event details from the database .. and it shows them correctly, the problem is, when the user tries to update a field and clicks on a save button, then, the web form just saves the same data that has been retrieved from the database.

For example, as seen in picture below the event name called : "test1", and if the user changed to test2, it won't change, it will be stuck with test1

enter image description here

Actually, I couldn't see anything wrong with codes, they just don't save the updates without showing any errors.

The codes below are behind saving updated button:

 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();
        }
    }

For more information, this is my page load codes:

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;


    }

Upvotes: 0

Views: 66

Answers (1)

UJS
UJS

Reputation: 861

Simply add if not PostBack condition in you Page_Load Event :

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

When you are clicking on the save button .Everything is going ok .But before saving it is getting entire data to the Original data.

This is how Page Life cycle works.

Upvotes: 2

Related Questions