By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

fetching records using datareader and updation

P: n/a
Dear All,
im fetching record from database using datareader, and is displaying
them in the textboxes.
when user change the text fields and then presses the update button the
record should be updated but what happen is: form is postedback but no
change happen to database and also textboxes return to there original
state i.e previous record(enableviewstate is false/same result with
true).
small code is here:
----------------------------------------------------------
<script runat="server">

SqlConnection c = new SqlConnection("user id=sa;password=;Initial
Catalog=jobsdbbackup;Data Source=localhost;Integrated Security=SSPI;");
protected void Page_Load(object sender, EventArgs e)
{

string email = "";
string firstname = "";
string lastname = "";

string strOleDb = "SELECT email,firstname,lastname FROM
tbl_jobreg where em*********@hotmail.com'";
SqlCommand command = new SqlCommand(strOleDb, c);
c.Open();
SqlDataReader Reader;
Reader = command.ExecuteReader();
while (Reader.Read())
{
email = Reader.GetString(0);
firstname = Reader.GetString(1);
lastname = Reader.GetString(2);
txtemail.Text = email;
txtfirstname.Text = firstname;
txtlastname.Text = lastname;

}
c.Close();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
c.Open();
String u = "Update tbl_jobreg set
email=@email,firstname=@firstname,lastname=@lastna me where
em*********@hotmail.com'";
SqlCommand co = new SqlCommand(u, c);

co.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar,
50));
co.Parameters["@email"].Value = txtemail.Text;
co.Parameters.Add(new SqlParameter("@firstname",
SqlDbType.VarChar, 50));
co.Parameters["@firstname"].Value = txtfirstname.Text;
co.Parameters.Add(new SqlParameter("@lastname",
SqlDbType.VarChar, 50));
co.Parameters["@lastname"].Value = txtlastname.Text;

try
{
co.ExecuteNonQuery();
Label1.Text = "Record Updated";

}
catch (SqlException exp)
{
Label1.Text = exp.Message.ToString();

}
c.Close();
}
</script>

what should i change to make it happen alright.
Thanks in advance
nasir

Jul 10 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Put the code in Page_Load in an 'if' block that is only executing if it is
not a postback. Right now on every request, the data is retrieved, and on
postback that overwrites whatever the user entered.

<na********@yahoo.comwrote in message
news:11**********************@s13g2000cwa.googlegr oups.com...
Dear All,
im fetching record from database using datareader, and is displaying
them in the textboxes.
when user change the text fields and then presses the update button the
record should be updated but what happen is: form is postedback but no
change happen to database and also textboxes return to there original
state i.e previous record(enableviewstate is false/same result with
true).
small code is here:
----------------------------------------------------------
<script runat="server">

SqlConnection c = new SqlConnection("user id=sa;password=;Initial
Catalog=jobsdbbackup;Data Source=localhost;Integrated Security=SSPI;");
protected void Page_Load(object sender, EventArgs e)
{

string email = "";
string firstname = "";
string lastname = "";

string strOleDb = "SELECT email,firstname,lastname FROM
tbl_jobreg where em*********@hotmail.com'";
SqlCommand command = new SqlCommand(strOleDb, c);
c.Open();
SqlDataReader Reader;
Reader = command.ExecuteReader();
while (Reader.Read())
{
email = Reader.GetString(0);
firstname = Reader.GetString(1);
lastname = Reader.GetString(2);
txtemail.Text = email;
txtfirstname.Text = firstname;
txtlastname.Text = lastname;

}
c.Close();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
c.Open();
String u = "Update tbl_jobreg set
email=@email,firstname=@firstname,lastname=@lastna me where
em*********@hotmail.com'";
SqlCommand co = new SqlCommand(u, c);

co.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar,
50));
co.Parameters["@email"].Value = txtemail.Text;
co.Parameters.Add(new SqlParameter("@firstname",
SqlDbType.VarChar, 50));
co.Parameters["@firstname"].Value = txtfirstname.Text;
co.Parameters.Add(new SqlParameter("@lastname",
SqlDbType.VarChar, 50));
co.Parameters["@lastname"].Value = txtlastname.Text;

try
{
co.ExecuteNonQuery();
Label1.Text = "Record Updated";

}
catch (SqlException exp)
{
Label1.Text = exp.Message.ToString();

}
c.Close();
}
</script>

what should i change to make it happen alright.
Thanks in advance
nasir

Jul 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.