InitializeComponent versus page_load

  • Thread starter Thread starter enantiomer
  • Start date Start date
E

enantiomer

I am running into some problems when running a pretty simple asp.net
page that was mostly generated from vs.net.

vs generated the code that sets up my sqlConnection, SqlDataAdapter,
and DataSet objects for my aspx.cs class. When trying to fill my
DataSet object in Page_Load with the SqlDataAdapter that was
initialized in InitializeComponent, it doesn't actually fill the
DataSet with anything (I check to make sure that the Rows.Count is >
0).

When I move over the initialization of my SqlDataAdapter up to
Page_Load method, the dataset can then be filled. I was under the
impression that InitializeComponent(which is called in OnInit) would
be called before Page_Load, and thus mySqlDataAdater would be properly
initialized to Fill my DataSet... Anybody have any ideas? Below is a
snippet of code that I use to initialize and fill with... Thanks,

Jon

__________begin code snippet__________________

String temp = String.Format("SELECT username, password, usertype, id,
name, address, city, state FROM [user] WHERE (username = '{0}') AND
(password = '{1}')",
(string)userNameBox.Text,(string)passwordBox.Text);
sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(temp,
sqlConnection1);

ds = new System.Data.DataSet();
sqlDataAdapter1.Fill(ds, "user");

if(ds.Tables["user"].Rows.Count > 0 && IsPostBack)
{ ... do stuff ....

___________end code snippet______________________
 
Hi,

Take a look at the comment before InitializeComponent. this code is
manipulate by the designer and every time you change controls on
designer surface the InitializeComponent content is recreate, erasing
your code. This is the reason why you shouldn’t write any code inside
InitializeComponent and write it inside Page_Load event.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
Back
Top