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
 
N

Nick Malik

Hello Viktor,

I assume that you are new to the idea of creating multiple tiers in your
code. With the intent of being constructive, I'd suggest, first, that you
should create objects that can handle the updates and inserts for you, and
your ASP pages should use those objects to fill the web pages (and to update
the data).

Secondly, I haven't seen the syntax you are using, where you provide an
UPDATE statement, and parameters with types. That is new to me. Since you
are clearly able to create stored procedures (as you did so in your first
statement), why not create a stored proc that will perform the UPDATE
statement and pass the parameters. Then you will be able to test your
stored procedure simply by calling it in SQL Query Analyzer. This is one of
the better ways to debug database issues.

Third, in your Update statement, you state: >>UPDATE HAHA.USERS <<
Does the user id "HAHA" own the database table Users? This is not normally
a good idea. You want your database tables owned by the dbo. Go into SQL
Enterprise Manager and look at the table owner. It is possible that you
have two tables... one owned by dbo and the other owned by HAHA. If the
stored procedure that you use for query is looking up data from the table
owned by 'dbo' and the Update is updating data in the table owned by 'HAHA',
then you will get the behavior you describe.

If this is the case, change the Update statement to "Update Users ..."
instead of "Update HAHA.Users ..."

Good luck and I hope this helps,
---- Nick

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

Top