473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2265
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daves | last post by:
trying to get a boolean value from DataReader (that is, result from a SQL query); boolean MyBool = MyDataReader.GetBoolean(3); well it works using that number 3 but of course I would like to...
2
by: yatharth | last post by:
Hi Mike I have Datagrid and i am fetching data around about 1 million into it , i think which is not good deal this makes my system slow.Please tell the way so that i can fetch data in my...
3
by: Andrew Bourne | last post by:
I would like to insert multiple records into a table based on a query returned from the same table. For example a datareader returns 3 records containing the following records user1 file1...
0
by: Shujun Huang | last post by:
Hi, I am working on converting Informix database to Postgre. I have one question for fetching records using PostgreSQL. The record I am fetching is a variable size text string. Before fetching...
5
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE...
1
by: Bhavesh | last post by:
Hi Bruce, Thanks For Reply. U were right, Needed to pass string , but also need to pass size of Data( instead of 16, passed actual length of data). So that worked for me & didn't get any...
4
by: LamSoft | last post by:
I've got some perfermance issue while fetching data from MSSQL I have a gridview, the datasource is a objectdatasource, and the objectdatasource is pointed to the dataset. The Select Query in...
2
by: punitshrivastava | last post by:
Hello friends, I am working in php .i have one problem regarding to updation . Code for updation i use is : <?php $fname = $_POST; $lname = $_POST; $find = $_POST; echo "$find"; ?> ...
0
by: punitshrivastava | last post by:
Hi to All Senior's, I am working in Asp.net with C#. I am newbie in C#. So i want to fetch mutiple images from database .Database i am using is Sql Server. For this i code: //get the image id...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.