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

Jay
Jay used Ask the Experts™
on
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??
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Team Lead
Commented:
you forgot to provide parameter: @compAddressId with its value before you executing the command?
Jay

Author

Commented:
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)'
Ryan ChongSoftware Team Lead

Commented:
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...
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

AndyAinscowFreelance programmer / Consultant

Commented:
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>");
            }
        }
Ryan ChongSoftware Team Lead

Commented:
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
Jay

Author

Commented:
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

AndyAinscowFreelance programmer / Consultant

Commented:
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'.
Ryan ChongSoftware Team Lead

Commented:
i changed the Update to this but nothing worked
what's the error now?
Ryan ChongSoftware Team Lead

Commented:
any response here?
Jay

Author

Commented:
I think it was because of the parameter !! thanks btw
AndyAinscowFreelance programmer / Consultant

Commented:
Did you understand my comment in the end?
Jay

Author

Commented:
Yea haha
Jay

Author

Commented:
Thanks for helping !!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial