ASP.NET/C# HELP

V

Viktor Popov

Hi ,
I have a form in which I show UserInfo from the DataBase using Stored
Procedure and I populate 5 TextBox controls with this information in this
way:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=haha; Password=*******");
SqlDataAdapter dad = new SqlDataAdapter("prRTUSERINFO", conn);
dad.SelectCommand.CommandType=CommandType.StoredProcedure;
conn.Open();
dad.SelectCommand.Parameters.Add(new SqlParameter("@USRNAM",
SqlDbType.VarChar,20));
dad.SelectCommand.Parameters["@USRNAM"].Value =
Session["usrName"].ToString();
DataSet ds = new DataSet();
dad.Fill(ds, "Users");
NameTB.Text=ds.Tables["Users"].Rows[0][3].ToString();
PhoneTB.Text=ds.Tables["Users"].Rows[0][5].ToString();
AddressTB.Text=ds.Tables["Users"].Rows[0][6].ToString();
CityTB.Text=ds.Tables["Users"].Rows[0][4].ToString();
EmailTB.Text=ds.Tables["Users"].Rows[0][7].ToString();
usrID=Int32.Parse(ds.Tables["Users"].Rows[0][0].ToString());
conn.Close();
}
The user could see the info and change the Text in the TextBoxes.
On the same form I have an InsertButton and when I click it I would like to
update the information in the DataBase with the changed information. If the
info is not changed and the Button is clicked I update the DataBase with the
same info from The TextBoxes. The problem is that when I have tried to
change the text in the TextBoxes and than I clicked the Button nothing
happens. The same info was in the TexBoxes, I mean the info which was in the
TextBoxes when the page was loaded for the first time.
Here it is the code of the Button_Click:
private void InsertBtn_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=haha; Password=******");
SqlDataAdapter dada = new SqlDataAdapter();
dada.UpdateCommand = new SqlCommand("UPDATE HAHA.USERS SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME",conn);
dada.UpdateCommand.CommandType = CommandType.Text;
dada.UpdateCommand.Parameters.Add(new SqlParameter("@NAME",
SqlDbType.VarChar,64));
dada.UpdateCommand.Parameters["@NAME"].Value = NameTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@CITY",
SqlDbType.VarChar,20));
dada.UpdateCommand.Parameters["@CITY"].Value = CityTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@PHONE",
SqlDbType.VarChar,16));
dada.UpdateCommand.Parameters["@PHONE"].Value = PhoneTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@ADDR",
SqlDbType.VarChar,74));
dada.UpdateCommand.Parameters["@ADDR"].Value = AddressTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@EML",
SqlDbType.VarChar,64));
dada.UpdateCommand.Parameters["@EML"].Value = EmailTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@USRNAME",
SqlDbType.VarChar,20));
dada.UpdateCommand.Parameters["@USRNAME"].Value =
Session["usrName"].ToString();
conn.Open();
dada.UpdateCommand.ExecuteNonQuery();
conn.Close();
}

Could you tell me where I am wrong?

Thank you in advance!

Viktor
 
K

Karl

Viktor,
When the "update" button is clicked, the Page_Load fires first. That means
your entire prRTUSERINFO sproc is fired and the values are reloaded into the
textbox - thus overloading anything you typed in. The Save function then
fires, and it saves the values that it just got from the DB.

wrap your code in Page_Load in an !Page.IsPostBack check

private void Page_Load(object sender, System.EventArgs e){
if (!Page.IsPostBack){
..all your fetch code here
}
}

Karl

Viktor Popov said:
Hi ,
I have a form in which I show UserInfo from the DataBase using Stored
Procedure and I populate 5 TextBox controls with this information in this
way:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=haha; Password=*******");
SqlDataAdapter dad = new SqlDataAdapter("prRTUSERINFO", conn);
dad.SelectCommand.CommandType=CommandType.StoredProcedure;
conn.Open();
dad.SelectCommand.Parameters.Add(new SqlParameter("@USRNAM",
SqlDbType.VarChar,20));
dad.SelectCommand.Parameters["@USRNAM"].Value =
Session["usrName"].ToString();
DataSet ds = new DataSet();
dad.Fill(ds, "Users");
NameTB.Text=ds.Tables["Users"].Rows[0][3].ToString();
PhoneTB.Text=ds.Tables["Users"].Rows[0][5].ToString();
AddressTB.Text=ds.Tables["Users"].Rows[0][6].ToString();
CityTB.Text=ds.Tables["Users"].Rows[0][4].ToString();
EmailTB.Text=ds.Tables["Users"].Rows[0][7].ToString();
usrID=Int32.Parse(ds.Tables["Users"].Rows[0][0].ToString());
conn.Close();
}
The user could see the info and change the Text in the TextBoxes.
On the same form I have an InsertButton and when I click it I would like to
update the information in the DataBase with the changed information. If the
info is not changed and the Button is clicked I update the DataBase with the
same info from The TextBoxes. The problem is that when I have tried to
change the text in the TextBoxes and than I clicked the Button nothing
happens. The same info was in the TexBoxes, I mean the info which was in the
TextBoxes when the page was loaded for the first time.
Here it is the code of the Button_Click:
private void InsertBtn_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=haha; Password=******");
SqlDataAdapter dada = new SqlDataAdapter();
dada.UpdateCommand = new SqlCommand("UPDATE HAHA.USERS SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME",conn);
dada.UpdateCommand.CommandType = CommandType.Text;
dada.UpdateCommand.Parameters.Add(new SqlParameter("@NAME",
SqlDbType.VarChar,64));
dada.UpdateCommand.Parameters["@NAME"].Value = NameTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@CITY",
SqlDbType.VarChar,20));
dada.UpdateCommand.Parameters["@CITY"].Value = CityTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@PHONE",
SqlDbType.VarChar,16));
dada.UpdateCommand.Parameters["@PHONE"].Value = PhoneTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@ADDR",
SqlDbType.VarChar,74));
dada.UpdateCommand.Parameters["@ADDR"].Value = AddressTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@EML",
SqlDbType.VarChar,64));
dada.UpdateCommand.Parameters["@EML"].Value = EmailTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@USRNAME",
SqlDbType.VarChar,20));
dada.UpdateCommand.Parameters["@USRNAME"].Value =
Session["usrName"].ToString();
conn.Open();
dada.UpdateCommand.ExecuteNonQuery();
conn.Close();
}

Could you tell me where I am wrong?

Thank you in advance!

Viktor
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top