473,385 Members | 1,359 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.

SqlCommand UPDATE problem...

Hello All,

I have two pages that are very similar. One is working, one is not. Here
is the code for both:

Page 1 (Working):

protected void btn_update_Click(object sender, EventArgs e)
{
Int32 item_id = Convert.ToInt32(ViewState["item_id"]);
Int32 news_id = Convert.ToInt32(ViewState["news_id"]);

string sql = "UPDATE web_items SET item_title = @title, item_body
= @body, item_descriptor = @descriptor " +
"WHERE item_id = @item";

SqlConnection con = new SqlConnection(* My Connection String *);
SqlCommand id_cmd = new SqlCommand(sql, con);

id_cmd.Parameters.Add("@title", SqlDbType.NVarChar).Value =
tb_title.Text;
id_cmd.Parameters.Add("@body", SqlDbType.NText).Value =
fb_item.Text;
id_cmd.Parameters.Add("@descriptor", SqlDbType.NText).Value =
fb_descriptor.Text;
id_cmd.Parameters.Add("@item", SqlDbType.Int).Value = item_id;

con.Open();
Int32 count = id_cmd.ExecuteNonQuery();
con.Close();

if ( count 0 )
{
// Step 2
id_cmd.Parameters.Clear();
id_cmd.CommandText = "UPDATE web_news SET news_category_id =
@cat_id, news_published = @publish, news_user_id = @user_id " +
"WHERE news_id = @news";

id_cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value =
Convert.ToInt32(dd_cat.SelectedValue);
id_cmd.Parameters.Add("@publish", SqlDbType.Bit).Value =
cb_publish.Checked;
id_cmd.Parameters.Add("@user_id", SqlDbType.Int).Value =
Convert.ToInt32(dd_author.SelectedValue);
id_cmd.Parameters.Add("@news", SqlDbType.Int).Value = news_id;

con.Open();
count = id_cmd.ExecuteNonQuery();
con.Close();

if (count 0)
Response.Redirect("news.aspx?status=updated");
else
litResult.Text = "An error has occurred, please see site
Administrator.";
}
else
litResult.Text = "An error has occurred, please see site
Administrator.";
}

Page 2 : (Not Working)

protected void btn_update_Click(object sender, EventArgs e)
{
Int32 item_id = Convert.ToInt32(ViewState["item_id"]);
Int32 product_id = Convert.ToInt32(ViewState["product_id"]);

string sql = "UPDATE web_items SET item_title = @title, item_body
= @body " +
"WHERE item_id = @item";

SqlConnection con = new SqlConnection(* My Connection String*);
SqlCommand id_cmd = new SqlCommand(sql, con);

id_cmd.Parameters.Add("@title", SqlDbType.NVarChar).Value =
tb_title.Text;
id_cmd.Parameters.Add("@body", SqlDbType.NText).Value =
fb_item.Text;
id_cmd.Parameters.Add("@item", SqlDbType.Int).Value = item_id;

con.Open();
Int32 count = id_cmd.ExecuteNonQuery();
con.Close();

if (count 0)
{
// Step 2
id_cmd.Parameters.Clear();
id_cmd.CommandText = "UPDATE web_products SET product_name =
@name " +
"WHERE product_id = @product";

id_cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value =
tb_title.Text;
id_cmd.Parameters.Add("@product", SqlDbType.Int).Value =
product_id;

con.Open();
count = id_cmd.ExecuteNonQuery();
con.Close();

if (count 0)
Response.Redirect("products.aspx?status=updated");
else
litResult.Text = "An error has occurred, please see site
Administrator.";
}
else
litResult.Text = "An error has occurred, please see site
Administrator.";

}

The strange part about it not working is that it redirects to the
"Updated" page like both updates were successful, but the data is not
changing. I have checked the DB to confirm this.

Thank You in advance.

NUZZI
May 8 '07 #1
6 2637
On May 8, 9:37 pm, Nuzzi <n...@smokemytool.com_nospamwrote:
Hello All,

I have two pages that are very similar. One is working, one is not. Here
is the code for both:

Page 1 (Working):

protected void btn_update_Click(object sender, EventArgs e)
{
Int32 item_id = Convert.ToInt32(ViewState["item_id"]);
Int32 news_id = Convert.ToInt32(ViewState["news_id"]);

string sql = "UPDATE web_items SET item_title = @title, item_body
= @body, item_descriptor = @descriptor " +
"WHERE item_id = @item";

SqlConnection con = new SqlConnection(* My Connection String *);
SqlCommand id_cmd = new SqlCommand(sql, con);

id_cmd.Parameters.Add("@title", SqlDbType.NVarChar).Value =
tb_title.Text;
id_cmd.Parameters.Add("@body", SqlDbType.NText).Value =
fb_item.Text;
id_cmd.Parameters.Add("@descriptor", SqlDbType.NText).Value =
fb_descriptor.Text;
id_cmd.Parameters.Add("@item", SqlDbType.Int).Value = item_id;

con.Open();
Int32 count = id_cmd.ExecuteNonQuery();
con.Close();

if ( count 0 )
{
// Step 2
id_cmd.Parameters.Clear();
id_cmd.CommandText = "UPDATE web_news SET news_category_id =
@cat_id, news_published = @publish, news_user_id = @user_id " +
"WHERE news_id = @news";

id_cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value =
Convert.ToInt32(dd_cat.SelectedValue);
id_cmd.Parameters.Add("@publish", SqlDbType.Bit).Value =
cb_publish.Checked;
id_cmd.Parameters.Add("@user_id", SqlDbType.Int).Value =
Convert.ToInt32(dd_author.SelectedValue);
id_cmd.Parameters.Add("@news", SqlDbType.Int).Value = news_id;

con.Open();
count = id_cmd.ExecuteNonQuery();
con.Close();

if (count 0)
Response.Redirect("news.aspx?status=updated");
else
litResult.Text = "An error has occurred, please see site
Administrator.";
}
else
litResult.Text = "An error has occurred, please see site
Administrator.";
}

Page 2 : (Not Working)

protected void btn_update_Click(object sender, EventArgs e)
{
Int32 item_id = Convert.ToInt32(ViewState["item_id"]);
Int32 product_id = Convert.ToInt32(ViewState["product_id"]);

string sql = "UPDATE web_items SET item_title = @title, item_body
= @body " +
"WHERE item_id = @item";

SqlConnection con = new SqlConnection(* My Connection String*);
SqlCommand id_cmd = new SqlCommand(sql, con);

id_cmd.Parameters.Add("@title", SqlDbType.NVarChar).Value =
tb_title.Text;
id_cmd.Parameters.Add("@body", SqlDbType.NText).Value =
fb_item.Text;
id_cmd.Parameters.Add("@item", SqlDbType.Int).Value = item_id;

con.Open();
Int32 count = id_cmd.ExecuteNonQuery();
con.Close();

if (count 0)
{
// Step 2
id_cmd.Parameters.Clear();
id_cmd.CommandText = "UPDATE web_products SET product_name =
@name " +
"WHERE product_id = @product";

id_cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value =
tb_title.Text;
id_cmd.Parameters.Add("@product", SqlDbType.Int).Value =
product_id;

con.Open();
count = id_cmd.ExecuteNonQuery();
con.Close();

if (count 0)
Response.Redirect("products.aspx?status=updated");
else
litResult.Text = "An error has occurred, please see site
Administrator.";
}
else
litResult.Text = "An error has occurred, please see site
Administrator.";

}

The strange part about it not working is that it redirects to the
"Updated" page like both updates were successful, but the data is not
changing. I have checked the DB to confirm this.

Thank You in advance.

NUZZI
Are you sure that the values of the controls (tb_title.Text,
fb_item.Text etc) were changed? Try to debug to see what values they
have.
May 8 '07 #2
On Tue, 08 May 2007 16:18:37 -0400, Alexey Smirnov
<al************@gmail.comwrote:
Are you sure that the values of the controls (tb_title.Text,
fb_item.Text etc) were changed? Try to debug to see what values they
have.

Hello Alexey,

Thanks for the response. Yes, I am sure the values are changing. It is
the damnedest thing. I have tried so many different things and cannot get
it. The only real difference is on the non-working page I am doing some
DB stuff on page_load, but making sure to close all connections and
readers, etc. I am ready to pull hair out...and there is not much left.

Thanks,

NUZZI

May 8 '07 #3
On May 8, 11:18 pm, Nuzzi <n...@smokemytool.com_nospamwrote:
On Tue, 08 May 2007 16:18:37 -0400, Alexey Smirnov

<alexey.smir...@gmail.comwrote:
Are you sure that the values of the controls (tb_title.Text,
fb_item.Text etc) were changed? Try to debug to see what values they
have.

Hello Alexey,

Thanks for the response. Yes, I am sure the values are changing. It is
the damnedest thing. I have tried so many different things and cannot get
it. The only real difference is on the non-working page I am doing some
DB stuff on page_load, but making sure to close all connections and
readers, etc. I am ready to pull hair out...and there is not much left.

Thanks,

NUZZI
Well, if I were you, I would debug it (F11). The code looks good, and
if you see the products.aspx?status=updated at the end, the code is
working. It probably means that you update another row or something
like this.

Try to debug.

Try to set hard code the values.

Try to add Response.Write("Hello I'm Here").

Just try :-)

May 8 '07 #4
In addition, run SQL profiler and see what commands are in fact issued
to the database. Copy them and execute them in SQL Management studio.
Does it work there?

Roland
May 9 '07 #5
On Wed, 09 May 2007 00:30:10 -0400, Roland Dick <br*****@web.dewrote:
In addition, run SQL profiler and see what commands are in fact issued
to the database. Copy them and execute them in SQL Management studio.
Does it work there?

Roland
Thanks Roland and Alexey. I got it. It was a misunderstanding about the
sequence of events. I had code in the Page_Load that filled the fields.
The Page_Load was actually being implemented prior to the Button_Click
event and refilling the fields with old data and then resetting the old
data back in. So, like Alexey's first question about the fields
changing...they really weren't even though I was changing them manually.
I just protected it by using the IsPostBack variable.

Thanks Agains,

NUZZI
May 10 '07 #6
On May 10, 6:38 pm, Nuzzi <n...@smokemytool.com_nospamwrote:
On Wed, 09 May 2007 00:30:10 -0400, Roland Dick <bris...@web.dewrote:
In addition, run SQL profiler and see what commands are in fact issued
to the database. Copy them and execute them in SQL Management studio.
Does it work there?
Roland

Thanks Roland and Alexey. I got it. It was a misunderstanding about the
sequence of events. I had code in the Page_Load that filled the fields.
The Page_Load was actually being implemented prior to the Button_Click
event and refilling the fields with old data and then resetting the old
data back in. So, like Alexey's first question about the fields
changing...they really weren't even though I was changing them manually.
I just protected it by using the IsPostBack variable.

Thanks Agains,

NUZZI
great :-)

May 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: joao dias | last post by:
hi all, sorry this is a bit lenghty... I am getting stucked with an unexpected behaviour of visual studio when I insert an sqlcommand data control in a aspx page, and after configuring it to run...
2
by: Andre Ranieri | last post by:
I'm having trouble with what should be a simple task; beginning and committing T-SQL transactions using the SQLClient. I'm using a SqlCommand (cmd) to begin the transaction and delete records from...
1
by: Jason Huang | last post by:
Hi, In the ASP.Net, it seems it can update data in different ways. It is my impression that we can use the SqlCommand or SqlDataAdapter to update data. But I'm wondering which method is better,...
2
by: Tracey | last post by:
Sorry for the repeated post. I tried to update a record in database using SqlCommand.ExecuteNonQuery( ) method (I failed using SqlDataAdapter). I traced the above statement and found that it returned...
1
by: Tom | last post by:
Hello guys Please have a look on following paragraph from ".NET Data Access Architecture Guide". ''''''''''' Although you can repeatedly use the same SqlCommand object to execute the same...
4
by: Billy | last post by:
My Busn tier returns a DataSet and I want to fill a DataAdapter in the presentation layer with that dataset. Problem is that if I create a DataAdapter in the presentation layer i need to specify a...
0
by: Leanne | last post by:
I am doing customization for Microsoft Pos. I am working on installation package now. I added some new tables and stored procuedures and generated SQL Script for that. During the installation...
4
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
For MS SQL Server... I am used to declaring local variables in my SQL queries... Declare @MyInt int, @MyChar varchar(33) Parameters were idenfitied with a colon... Where ModDate :MyDate But,...
6
by: Frank Hauptlorenz | last post by:
Hello, I'm trying to send an SqlCommand to a WCF-Service. For this I'm using the following DataContract: public class SqlCommandComposite { SqlCommand cmd = new SqlCommand();
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.