fetching records using datareader and updation

N

nasirmajor

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 email='(e-mail address removed)'";
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=@lastname where
email='(e-mail address removed)'";
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
 
M

Marina Levit [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top