Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two tables in my database : Business and User_Acc.

Business has the following columns:
Business_ID
Business_Name
Address_Line_1
Address_Line_2
Address_Line_3
County
Provence
Owner_Name
Vat_No
Telephone

User_Acc has the following columns:
User_ID
Username
Password
Roles
Email
Business_ID

I have a relationship set up that links both tables through the business_ID

I have a webform using C# and Asp.net that will allow the user to input details into textboxes to modify the record already in the database - i.e update the business details and if they change their password update the password in the User_acc table

Here is my procedure but it is not working:
C#
ALTER PROCEDURE dbo.SaveBusinessChanges
(
	@Business_Name varchar(50),
	 @Address_Line_1 varchar(50), 
	 @Address_Line_2 varchar(50),
	  @Address_Line_3 varchar(50),
	   @County varchar(50),
	    @Provence varchar(50),
		 @Telephone varchar(50),
		 @Username varchar(50),
		   @PasswordNew varchar(50),
		   @Email varchar(50)
		   )
	
AS
BEGIN TRANSACTION


UPDATE
    T1
SET
    T1.Business_Name = @Business_Name
FROM
    Business T1
INNER JOIN User_Acc T2
ON	
	T1.Business_ID = T2.Business_ID
WHERE
	T2.Username = @Username

UPDATE
    T1
SET
    T1.Address_Line_1 = @Address_Line_1
FROM
    Business T1
INNER JOIN User_Acc T2
ON	
	T1.Business_ID = T2.Business_ID
WHERE
	T2.Username = @Username

UPDATE
    T1
SET
    T1.Address_Line_2 = @Address_Line_2
FROM
    Business T1
INNER JOIN User_Acc T2
ON	
	T1.Business_ID = T2.Business_ID
WHERE
	T2.Username = @Username

UPDATE
    T1
SET
    T1.Address_Line_3 = @Address_Line_3
FROM
    Business T1
INNER JOIN User_Acc T2
ON	
	T1.Business_ID = T2.Business_ID
WHERE
	T2.Username = @Username

UPDATE
    T1
SET
    T1.County = @County
FROM
    Business T1
INNER JOIN User_Acc T2
ON	
	T1.Business_ID = T2.Business_ID
WHERE
	T2.Username = @Username

UPDATE
    T1
SET
    T1.Provence = @Provence
FROM
    Business T1
INNER JOIN User_Acc T2
ON	
	T1.Business_ID = T2.Business_ID
WHERE
	T2.Username = @Username
		
UPDATE
    T1
SET
    T1.Telephone = @Telephone
FROM
    Business T1
INNER JOIN User_Acc T2
ON	
	T1.Business_ID = T2.Business_ID
WHERE
	T2.Username = @Username

UPDATE
    T2
SET
    T2.Email = @Email
FROM
    User_Acc T2
INNER JOIN Business T1
ON	
	T2.Business_ID = T1.Business_ID
WHERE
	T2.Username = @Username

UPDATE
    T2
SET
    T2.Password = @PasswordNew
FROM
    User_Acc T2
INNER JOIN Business T1
ON	
	T2.Business_ID = T1.Business_ID
WHERE
	T2.Username = @Username
	
	COMMIT


The record is identified through the link between Business_ID and a parameter @Username which takes the value from the Session variable 'User'

How should I go about solving this issue?
Posted
Updated 19-Feb-14 5:46am
v2
Comments
CHill60 19-Feb-14 11:47am    
"but it is not working" ... what do you mean by "not working" - what happens, why are the results different to what you expect, what error is thrown?
Member 10609511 19-Feb-14 11:53am    
I don't get any error codes - I am calling the stored procedure from within Visual Studio 2010. It simply isn't updating any fields for that specific record in my database hence why I think there is something wrong with my procedure?

1 solution

Ok ... first steps to finding out what's wrong try ...
Print the contents of @Username (If it's empty then it's the way you're handling the session variable)

Assuming that was fine then run this sql
SELECT * FROM User_Acc T2 WHERE Username = 'xxxxx'
replacing the xxxxx with the actual value of the username.
Check that there is a row returned (If not then it's your data at fault - there are no matches)

Assuming that worked then try the following sql
SELECT * FROM Business T1 INNER JOIN User_Acc T2 ON T1.Business_ID = T2.Business_ID

(If nothing is returned then it's the linking of your tables that is at fault)

Once you get it working you need to change the procedure to make it a little more efficient (and easier to read!!) ... you can update more than one column at a time if the updates are based on the same criteria.
For example all of your T1 updates can be expressed as
SQL
UPDATE T1
SET T1.Business_Name = @Business_Name, Address_Line_1 = @Address_Line_1, Address_Line_2 = @Address_Line_2,
Address_Line_3 = @Address_Line_3, County = @County, Provence = @Provence, Telephone = @Telephone
FROM  Business T1
INNER JOIN User_Acc T2 ON T1.Business_ID = T2.Business_ID
WHERE T2.Username = @Username


[EDIT - Added suggestions for C# code - see comments in the code]
Caveats as before - I haven't been able to test this
protected void SaveChanges(object sender, EventArgs e)
{
    SqlConnection conn;
    SqlCommand comm;
    SqlCommand comm1;
    String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    conn = new SqlConnection(connectionString);
    comm = new SqlCommand("SELECT * from Business a, User_Acc c Where c.Username = @Username AND c.Business_ID = a.Business_ID", conn);
    comm.Parameters.Add("@Username", System.Data.SqlDbType.VarChar).Value = Session["User"];
    conn.Open();
    SqlDataReader reader = comm.ExecuteReader();
    while (reader.Read())
    {
        if (txtOldPassword.Text.Equals(reader["Password"]))
        {
            // I'm not confortable using the same connection as the read - especially as we appear to 
            // be inside a reader loop so I'll create a new connection for the stored proc update
            SqlConnection connWrite;
            connWrite = new SqlConnection(connectionString);
            // For info on the using statement see http://msdn.microsoft.com/en-us/library/htd05whh.aspx
            using (SqlCommand comm1 = new SqlCommand("exec SaveBusinessChanges @Business_Name,@Address_Line_1,@Address_Line_2,@Address_Line_3,@County,@Provence,@Telephone,@Username,@PasswordNew,@Email", connWrite))
            {
                // Let the command know it is to run a Stored Procedure
                comm1.CommandType = CommandType.StoredProcedure;
                // This bit unchanged from OPs code
                comm1.Parameters.AddWithValue("@Business_Name", txtChangeBusinessName.Text);
                comm1.Parameters.AddWithValue("@Email", txtChangeBusinessEmail.Text);
                comm1.Parameters.AddWithValue("@Telephone", txtChangeBusinessTelephone.Text);
                comm1.Parameters.AddWithValue("@Address_Line_1", txtChangeBusinessAddress.Text);
                comm1.Parameters.AddWithValue("@Address_Line_2", txtChangeBusinessAddress2.Text);
                comm1.Parameters.AddWithValue("@Address_Line_3", txtChangeBusinessAddress3.Text);
                comm1.Parameters.AddWithValue("@Provence", DDLProvince.Text);
                comm1.Parameters.AddWithValue("@County", DDLCounty.Text);
                comm1.Parameters.AddWithValue("@Username", Session["User"]);
                comm1.Parameters.AddWithValue("@PasswordNew", txtChangeBusinessPassword.Text);
                // You need to execute the command to run the stored procedure
                comm1.ExecuteNonQuery();
            } // Because I've used a "using" statement I don't need connWrite.Close();
        }
    }
    reader.Close();
    conn.Close();
}

Link to the MS reference on the
using Statement[^] [Edit 2 - corrected the link]

[Edit 3] - My (very) bad. I'd declared comm1 outside of the using statement - meaning it still had scope after that statement completed. *facepalm* See bold and strike through in correction above.
While I'm here I should point out that the GUI elements should really be separated away from the Database elements - I would probably have the text passed through as parameters to methods on a separate class.
 
Share this answer
 
v4
Comments
Member 10609511 19-Feb-14 12:23pm    
I got both select statements to work and return the data linked correctly as per your syntax, however, when I modify the procedure with your code it still is not making the changes to the database with the updated information from the textboxes

Here is the procedure code:

ALTER PROCEDURE dbo.SaveBusinessChanges
(
@Business_Name varchar(50),
@Address_Line_1 varchar(50),
@Address_Line_2 varchar(50),
@Address_Line_3 varchar(50),
@County varchar(50),
@Provence varchar(50),
@Telephone varchar(50),
@Username varchar(50),
@PasswordNew varchar(50),
@Email varchar(50)
)

AS
BEGIN TRANSACTION


UPDATE T1
SET T1.Business_Name = @Business_Name, Address_Line_1 = @Address_Line_1, Address_Line_2 = @Address_Line_2,
Address_Line_3 = @Address_Line_3, County = @County, Provence = @Provence, Telephone = @Telephone
FROM Business T1
INNER JOIN User_Acc T2 ON T1.Business_ID = T2.Business_ID
WHERE T2.Username = @Username

UPDATE
T2
SET
T2.Email = @Email, Password = @PasswordNew
FROM
User_Acc T2
INNER JOIN Business T1
ON
T2.Business_ID = T1.Business_ID
WHERE
T2.Username = @Username



COMMIT

and here is my C# code

protected void SaveChanges(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
SqlCommand comm1;
String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

conn = new SqlConnection(connectionString);

comm = new SqlCommand("SELECT * from Business a, User_Acc c Where c.Username = @Username AND c.Business_ID = a.Business_ID", conn);
comm.Parameters.Add("@Username", System.Data.SqlDbType.VarChar).Value = Session["User"];
conn.Open();
SqlDataReader reader = comm.ExecuteReader();

while (reader.Read())
{

if (txtOldPassword.Text.Equals(reader["Password"]))
{

comm1 = new SqlCommand("exec SaveBusinessChanges @Business_Name,@Address_Line_1,@Address_Line_2,@Address_Line_3,@County,@Provence,@Telephone,@Username,@PasswordNew,@Email", conn);


comm1.Parameters.AddWithValue("@Business_Name", txtChangeBusinessName.Text);
comm1.Parameters.AddWithValue("@Email", txtChangeBusinessEmail.Text);
comm1.Parameters.AddWithValue("@Telephone", txtChangeBusinessTelephone.Text);
comm1.Parameters.AddWithValue("@Address_Line_1", txtChangeBusinessAddress.Text);
comm1.Parameters.AddWithValue("@Address_Line_2", txtChangeBusinessAddress2.Text);
comm1.Parameters.AddWithValue("@Address_Line_3", txtChangeBusinessAddress3.Text);
comm1.Parameters.AddWithValue("@Provence", DDLProvince.Text);
comm1.Parameters.AddWithValue("@County", DDLCounty.Text);
comm1.Parameters.AddWithValue("@Username", Session["User"]);
comm1.Parameters.AddWithValue("@PasswordNew", txtChangeBusinessPassword.Text);
}

}
reader.Close();
conn.Close();
}
CHill60 20-Feb-14 7:29am    
Sorry for the delay. You don't appear to have a comm1.ExecuteNonQuery(); so the Stored Procedure isn't actually being fired. I'll update my solution soon with some further suggestions
CHill60 20-Feb-14 7:48am    
Solution updated
[no name] 20-Feb-14 8:01am    
Good analysis
CHill60 21-Feb-14 3:57am    
Thank you ... your comment brought me back here which is when I spotted my error (Edit 3) so a double thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900