When reading data from a database using the Page_Load event and then updating the database, the data doesn't get updated as described here.
By including
if (!Page.IsPostback)
in before the reader allows data to be updated in other areas on the page/form. Below are examples:
Update doesn't work:
==================================================================
protected void Page_Load(object sender, EventArgs e)
{
DataList1.DataBind();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
SqlConnection sqlConn = new SqlConnection("connection");
SqlCommand sqlComm = new SqlCommand("UPDATE blog_Postings SET title = @title, body = @body WHERE (id = @id)", sqlConn);
sqlComm.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
sqlComm.Parameters["@id"].Value = intID;
sqlComm.Parameters.Add(new SqlParameter("@title", SqlDbType.NVarChar));
sqlComm.Parameters["@title"].Value = strTitle;
sqlComm.Parameters.Add(new SqlParameter("@body", SqlDbType.NVarChar));
sqlComm.Parameters["@body"].Value = strBody;
sqlComm.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
sqlComm.Dispose();
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
When the DataList is updating, the function protected void Page_Load(object sender, EventArgs e) runs again/refreshes which retrieves the old values in the database.
Update works:
==================================================================
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataList1.DataBind();
}
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
SqlConnection sqlConn = new SqlConnection("connection");
SqlCommand sqlComm = new SqlCommand("UPDATE blog_Postings SET title = @title, body = @body WHERE (id = @id)", sqlConn);
sqlComm.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
sqlComm.Parameters["@id"].Value = intID;
sqlComm.Parameters.Add(new SqlParameter("@title", SqlDbType.NVarChar));
sqlComm.Parameters["@title"].Value = strTitle;
sqlComm.Parameters.Add(new SqlParameter("@body", SqlDbType.NVarChar));
sqlComm.Parameters["@body"].Value = strBody;
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
sqlComm.Dispose();
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
So, as you can see by not validating the postback will causes data not to update which will lead into hours of frustration. Enjoy...
(Posted using Windows Live Writer)
posted on Tuesday, October 17, 2006 5:01 PM
Feedback
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/18/2007 12:12 PM |
ook
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
12/8/2007 3:26 PM |
We offer the largest collection of polyphonic ringtones, monophonic ringtones, mobile videos, color wallpapers, color screensavers, real sounds.
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
12/24/2007 12:28 PM |
If you are looking for the replica watch and information about it, you came to the right place.
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
12/24/2007 12:28 PM |
If you are looking for the replica watch and information about it, you came to the right place.
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
7/16/2008 12:41 PM |
bu ahmette tam salak yaa..
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
7/25/2008 1:02 PM |
very very nice
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
10/26/2008 3:03 AM |
thanks so much
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/22/2008 5:40 PM |
oyun indir
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/22/2008 5:41 PM |
oyun indir
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/22/2008 5:42 PM |
çet
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/22/2008 5:43 PM |
chat
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/22/2008 5:43 PM |
sohpet
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/22/2008 5:55 PM |
vps
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/24/2008 4:16 PM |
Thank You
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
12/1/2008 12:18 AM |
Thanks for this much texts
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
1/19/2009 2:31 PM |
This sh*tload of drivers also causes SBS 2003 performance reports to be blank.
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
2/9/2009 2:15 AM |
great
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
2/21/2009 1:49 AM |
Thank's Very Good..
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
3/18/2009 11:21 AM |
thanks
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
3/21/2009 1:18 PM |
Thanks..
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
4/2/2009 11:14 AM |
asq wq w as qw qwas qwq was q
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
4/2/2009 11:15 AM |
hjgasjh wueyt jhgsd jhdgas agjshsa
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
4/2/2009 11:15 AM |
sahgdfasdhgyrtwyerwhgfhgafshgan nbvsa
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
4/2/2009 11:16 AM |
fads fgsa gadgas gasfd a sgfadsga asfdag f ghghfhd yteyt ghfsa
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
4/5/2009 10:04 PM |
Thanks for sharing and information. To explore the beautiful
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
4/5/2009 10:05 PM |
Thanks for sharing and information. To explore the beautiful
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
4/5/2009 10:05 PM |
Thanks for sharing and information. To explore the beautiful
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
9/4/2009 1:51 PM |
Thank's Very much
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
9/4/2009 1:52 PM |
Thank's Very much
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
9/4/2009 1:52 PM |
Very much
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
9/4/2009 2:48 PM |
Thank you! You often write very interesting articles.
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
10/11/2009 12:06 AM |
thank you
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
10/11/2009 12:06 AM |
thank for share
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
10/22/2009 11:41 PM |
EKLEMEYECEK OLURSANIZ Lütfen PM ATIN !
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
10/26/2009 4:26 AM |
buradan doom gecti kardes ekleyip gectim
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/3/2009 12:54 AM |
thxx
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/8/2009 8:21 AM |
I like very much the writings and pictures and explanations in your adress so I look forward to see your next writings.
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/15/2009 4:42 AM |
How are you
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/15/2009 7:22 PM |
How are you
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
11/15/2009 8:46 PM |
Hii are you
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
12/9/2009 6:25 PM |
sinemalar
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
12/21/2009 11:42 PM |
hi how are you all
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
1/11/2010 4:30 PM |
OKEY OYUNU OYNA
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
1/11/2010 4:30 PM |
okey oyna
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
1/20/2010 3:52 AM |
Thanks
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
2/14/2010 4:51 PM |
tahnks you
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
2/14/2010 4:52 PM |
thank you admin
2/24/2010 1:26 PM |
Thank you for great article.
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
2/27/2010 9:17 PM |
thankss
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
3/7/2010 1:43 AM |
thanks..
# re: :: Tip/Trick: Updating a database when using Page_Load to load data ::
3/15/2010 7:28 PM |
thank you admin