471,591 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,591 software developers and data experts.

fetching records using datareader and updation

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
1 2193
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.

Similar topics

3 posts views Thread by Andrew Bourne | last post: by
reply views Thread by Shujun Huang | last post: by
4 posts views Thread by LamSoft | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.