Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the following code to update my project's "Courses" table. But its not working. Please suggest me, whats wrong I'm doing..

What I have tried:

private void Updating()
{
try
{
string ConnectionString = @"Data Source= D3ll\SQLEXPRESS; Initial Catalog=GMS; Integrated Security=True";
SqlConnection Connection = new SqlConnection(ConnectionString);
Connection.Open();
string Query = "UPDATE Courses SET Title='" + TitleTextBox.Text + "', Fee='" + FeeTextBox.Text + "', Description='" + DescriptionTextBox.Text + "' WHERE CourseID='" + CourseIDTextBox.Text + "'";
SqlCommand Command = new SqlCommand(Query, Connection);
Command.ExecuteNonQuery();
Connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Oops!! Something is wrong!!");
}
}
Posted
Updated 28-Jul-16 1:04am
Comments
Beginner Luck 28-Jul-16 4:59am    
I love to sql injection your sql query. please fix that first
pinGOL2305 28-Jul-16 6:05am    
Please help me to prevent SQL injection. I don't have enough knowledge about but just know the name of that attack. I'm a beginner..
Please, help me!!
vatsa_gaurav 28-Jul-16 7:11am    
Use Paramaters and better to write it in stored Proc

Loads of things!
The first and biggest, is this: never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

The second is: we don't know - we don't have access to your data, so we can't tell.
But you do, and you have tools to help you work out what the problems is.
Start by putting a breakpoint on the line:
C#
MessageBox.Show("Oops!! Something is wrong!!");

And change the line:
C#
Command.ExecuteNonQuery();
To:
C#
int count = Command.ExecuteNonQuery();
And put a breakpoint on this line as well:
C#
Connection.Close();

Now run your app in the debugger, and see which breakpoint it reaches.
If it gets the to MessageBox, then look at ex and it's Message, InnerException and see exactly what they say.
If it gets to the Connection.Close, then look at what is in count. Is it 1? Or is it 0? The return value is the number of rows the command affected, so if it's one then it worked and you need to look elsewhere. If it's zero, then it probably means that your WHERE clause didn't match any rows - so start looking at the values you pass to SQL.

We can't do any of that for you, but it should be pretty simple for you to do, and see what you get.
 
Share this answer
 
I have successfully resolved that by following code;

C#
private void Updating()
        {
            try
            {
                string connectionString = @"Data Source= D3ll\SQLEXPRESS; Initial Catalog=GMS; Integrated Security=True";
                SqlConnection Connection = new SqlConnection(connectionString);
                SqlCommand Command = new SqlCommand();
                Command.Connection = Connection;
                Connection.Open();
                Command.CommandText = "UPDATE Courses SET Title = @ttl, Fee = @fe, Description= @dscptn WHERE CourseID = @id";

                Command.Parameters.AddWithValue("@ttl", TitleTextBox.Text);
                Command.Parameters.AddWithValue("@fe", FeeTextBox.Text);
                Command.Parameters.AddWithValue("@dscptn", DescriptionTextBox.Text);
                Command.Parameters.AddWithValue("@id", CourseIDTextBox.Text);
                int NEW = Command.ExecuteNonQuery();
                Connection.Close();

                MessageBox.Show("!! (" + NEW + ") new Course Information has been UPDATED successfully!!");

                if (NEW > 0)
                    ClearText();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Oops!! Something is wrong!!");
            }
        }
 
Share this answer
 

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