Avatar of Jay
Jay

asked on 

Data not updated in database even though it says "Update successful"

Im using web service, where the data in project B has to be updated in project A,

Heres how i did for the DAL class in project B
 public DataSet UpdateBook(String baddress, String bpostalcode, String bcountry, String saddress, String spostalcode, String scountry, String bcompany, String bcity, String scompany, String scity)
        {
            int result = 0;
            DataSet updateAdd;
            SqlDataAdapter da;
            string queryStr = "UPDATE CompanyAddress"
                + "SET BillingAddress=@billingaddress, BillingPostalCode=@billingpostalcode, BillingCountry=@billingcountry, ShippingAddress=@shippingaddress, ShippingPostalCode=@shippingpostalcode, ShippingCountry=@scountry, BillingCompany=@billingcompany, BillingCity=@billingcity, ShippingCompany=@shippingcompany, ShippingCity=@shippingcity"
                + "WHERE CompAddressId=@compAddressId";
            SqlConnection conn = new SqlConnection(_connStr);
            updateAdd = new DataSet();
            SqlCommand cmd = new SqlCommand(queryStr, conn);
            try
            {
                da = new SqlDataAdapter(queryStr.ToString(), conn);
                da.Fill(updateAdd);
                cmd.Parameters.AddWithValue("@billingaddress", baddress);
                cmd.Parameters.AddWithValue("@billingpostalcode", bpostalcode);
                cmd.Parameters.AddWithValue("@billingcountry", bcountry);
                cmd.Parameters.AddWithValue("@shippingaddress", saddress);
                cmd.Parameters.AddWithValue("@shippingpostalcode", spostalcode);
                cmd.Parameters.AddWithValue("@scountry", scountry);
                cmd.Parameters.AddWithValue("@billingcompany", bcompany);
                cmd.Parameters.AddWithValue("@billingcity", bcity);
                cmd.Parameters.AddWithValue("@shippingcompany", scompany);
                cmd.Parameters.AddWithValue("@shippingcity", scity);
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return updateAdd;
        }

Open in new window

And the code behind for project A
 protected void btnUpdateAddress_Click(object sender, EventArgs e)
        {
            BllCompAddressBook updateAdd = new BllCompAddressBook();
            int result = 0;
            if (result == 0)
            {
                DataSet ds = updateAdd.UpdateBookList(tb_bAddress.Text, tb_bPostalCode.Text, tb_bCountry.Text, tb_sAddress.Text, tb_sPostalCode.Text, tb_sCountry.Text, tb_bCompany.Text, tb_bCity.Text, tb_sCompany.Text, tb_sCity.Text);
                Response.Write("<script>alert('Update successful');</script>");
            }
            else
            {
                Response.Write("<script>alert('Update NOT successful.');</script>");
            }
        }

Open in new window

Any idea why??
ASP.NETDatabasesC#

Avatar of undefined
Last Comment
Jay
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jay
Jay

ASKER

Like this?
  public DataSet UpdateBook(int caddressid, String baddress, String bpostalcode, String bcountry, String saddress, String spostalcode, String scountry, String bcompany, String bcity, String scompany, String scity)
        {
            int result = 0;
            DataSet updateAdd;
            SqlDataAdapter da;
            string queryStr = "UPDATE CompanyAddress"
                + "SET BillingAddress=@billingaddress, BillingPostalCode=@billingpostalcode, BillingCountry=@billingcountry, ShippingAddress=@shippingaddress, ShippingPostalCode=@shippingpostalcode, ShippingCountry=@scountry, BillingCompany=@billingcompany, BillingCity=@billingcity, ShippingCompany=@shippingcompany, ShippingCity=@shippingcity"
                + "WHERE CompAddressId=@compAddressId";
            SqlConnection conn = new SqlConnection(_connStr);
            updateAdd = new DataSet();
            SqlCommand cmd = new SqlCommand(queryStr, conn);
            try
            {
                da = new SqlDataAdapter(queryStr.ToString(), conn);
                da.Fill(updateAdd);
                cmd.Parameters.AddWithValue("@compAddressId", caddressid);
                cmd.Parameters.AddWithValue("@billingaddress", baddress);
                cmd.Parameters.AddWithValue("@billingpostalcode", bpostalcode);
                cmd.Parameters.AddWithValue("@billingcountry", bcountry);
                cmd.Parameters.AddWithValue("@shippingaddress", saddress);
                cmd.Parameters.AddWithValue("@shippingpostalcode", spostalcode);
                cmd.Parameters.AddWithValue("@scountry", scountry);
                cmd.Parameters.AddWithValue("@billingcompany", bcompany);
                cmd.Parameters.AddWithValue("@billingcity", bcity);
                cmd.Parameters.AddWithValue("@shippingcompany", scompany);
                cmd.Parameters.AddWithValue("@shippingcity", scity);
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return updateAdd;
        }

Open in new window

I did that, but my project A has an error!!
there are red lines at UpdateBookList(1); part
I dont have a textbox id, so i did it like:
 protected void btnUpdateAddress_Click(object sender, EventArgs e)
        {
            BllCompAddressBook updateAdd = new BllCompAddressBook();
            int result = 0;
            if (result == 0)
            {
                DataSet ds = updateAdd.UpdateBookList(1);
                ds = updateAdd.UpdateBookList(Convert.ToInt32(ds.Tables[0].Rows[0][0]) , tb_bAddress.Text, tb_bPostalCode.Text, tb_bCountry.Text, tb_sAddress.Text, tb_sPostalCode.Text, tb_sCountry.Text, tb_bCompany.Text, tb_bCity.Text, tb_sCompany.Text, tb_sCity.Text);
                Response.Write("<script>alert('Update successful');</script>");
            }
            else
            {
                Response.Write("<script>alert('Update NOT successful.');</script>");
            }
        }

Open in new window

The error is : There is no argument given that corresponds to the required formal parameter 'baddress' of 'BllCompAddressBook.UpdateBookList(int, string, string, string, string, string, string, string, string, string, string)'
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

the error is with UpdateBookList, try debug it.

but do you want to execute it? or you want to execute the UpdateBook?

both are different methods...
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

This code is very odd.  The first section of code ( "Update Successful" ) will always be run, the ( "Update NOT Successful" ) never runs.

protected void btnUpdateAddress_Click(object sender, EventArgs e)
        {
            BllCompAddressBook updateAdd = new BllCompAddressBook();
           int result = 0;
            if (result == 0)
           {
                DataSet ds = updateAdd.UpdateBookList(tb_bAddress.Text, tb_bPostalCode.Text, tb_bCountry.Text, tb_sAddress.Text, tb_sPostalCode.Text, tb_sCountry.Text, tb_bCompany.Text, tb_bCity.Text, tb_sCompany.Text, tb_sCity.Text);
                Response.Write("<script>alert('Update successful');</script>");
            }
            else
            {
                Response.Write("<script>alert('Update NOT successful.');</script>");
            }
        }
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

the ( "Update NOT Successful" ) never runs.

reason being you put that output in the “else” condition.

in your code, you got:

>>int result = 0;
>>            if (result == 0)

it wil always execute whatever within that condition. things not in that condition with a else statement wil not be executed
Avatar of Jay
Jay

ASKER

i changed the Update to this but nothing worked :(
        public int UpdateBook(int addressid, String baddress, String bpostalcode, String bcountry, String saddress, String spostalcode, String scountry, String bcompany, String bcity, String scompany, String scity)
        {
            StringBuilder sql;
            SqlCommand sqlCmd;
            int result;
            result = 0;
            sql = new StringBuilder();
            sql.AppendLine("UPDATE CompanyAddress");
            sql.AppendLine(" ");
            sql.AppendLine("SET BillingAddress=@billingaddress, BillingPostalCode=@billingpostalcode, BillingCountry=@billingcountry, ShippingAddress=@shippingaddress, ShippingPostalCode=@shippingpostalcode, ShippingCountry=@scountry, BillingCompany=@billingcompany, BillingCity=@billingcity, ShippingCompany=@shippingcompany, ShippingCity=@shippingcity");
            sql.AppendLine(" ");
            sql.AppendLine("WHERE CompAddressId=@compAddressId");
            SqlConnection conn = dbConn.GetConnection();
            try
            {
                sqlCmd = new SqlCommand(sql.ToString(), conn);
                sqlCmd.Parameters.AddWithValue("@compAddressId", addressid);
                sqlCmd.Parameters.AddWithValue("@billingaddress", baddress);
                sqlCmd.Parameters.AddWithValue("@billingpostalcode", bpostalcode);
                sqlCmd.Parameters.AddWithValue("@billingcountry", bcountry);
                sqlCmd.Parameters.AddWithValue("@shippingaddress", saddress);
                sqlCmd.Parameters.AddWithValue("@shippingpostalcode", spostalcode);
                sqlCmd.Parameters.AddWithValue("@scountry", scountry);
                sqlCmd.Parameters.AddWithValue("@billingcompany", bcompany);
                sqlCmd.Parameters.AddWithValue("@billingcity", bcity);
                sqlCmd.Parameters.AddWithValue("@shippingcompany", scompany);
                sqlCmd.Parameters.AddWithValue("@shippingcity", scity);
                result = sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            finally
            {
                conn.Close();
            }

            return result;
        }

Open in new window


And also
   protected void btnUpdateAddress_Click(object sender, EventArgs e)
        {
            BllCompAddressBook updateAdd = new BllCompAddressBook();
            int result = 0;
            if (result == 0) { 
                updateAdd.UpdateBookList(int.Parse(lblId.Text), tb_bAddress.Text, tb_bPostalCode.Text, tb_bCountry.Text, tb_sAddress.Text, tb_sPostalCode.Text, tb_sCountry.Text, tb_bCompany.Text, tb_bCity.Text, tb_sCompany.Text, tb_sCity.Text);
          Response.Write("<script>alert('Update successful');</script>");
            }
            //else
            //{
            //    Response.Write("<script>alert('Update NOT successful.');</script>");
            //}
        }

Open in new window

Reason why i have updateboklist is that im using webservice !! so in the BLL folder i have
        DalCompAddressBook Address = new DalCompAddressBook();
public int UpdateBookList(int caddressid, string baddress, string bpostalcode, string bcountry, string saddress, string spostalcode, string scountry, string bcompany, string bcity, string scompany, string scity)
        {
            return Address.UpdateBook(caddressid, baddress, bpostalcode, bcountry, saddress, spostalcode, scountry, bcompany, bcity, scompany, scity);
        }

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

You didn't understand my previous comment:
            int result = 0;
            if (result == 0) {

The if statement is always true.  Your code to display the 'Update NOT Successful' will never run in that function, it will always show 'Update Successful'.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

i changed the Update to this but nothing worked
what's the error now?
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

any response here?
Avatar of Jay
Jay

ASKER

I think it was because of the parameter !! thanks btw
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Did you understand my comment in the end?
Avatar of Jay
Jay

ASKER

Yea haha
Avatar of Jay
Jay

ASKER

Thanks for helping !!
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo