Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
try
            {
                string updatequery = string.Format("Update Facultyleave set Faculty_ID = '{0}', LeaveType = '{1}', TotalBalance = '{2}', Month ='{3}', Days= '{4}', LeaveDate= '{5}', LeaveTime= '{6}', EndDate = '{7}', EndTime = '{8}', SanctionedLeave = '{9}', AvailedLeave = '{10}', Balance = '{11}', Reason = '{12}'" +
                    " where LeaveID = '{13}' ", txtID.Text.Trim(), cboLeaveType.Text.Trim(), txttotalleave.Text.Trim(), cboMonth.Text, NoOfDays.Text,
                    dtpLeavedate.Value.ToString("yyyy-MM-dd"), dtpLeaveTime.Value.ToString("HH:mm:ss"), dtpEndDate.Value.ToString("yyyy-MM-dd"),
                    dtpEndTime.Value.ToString("HH:mm:ss"), txtLSanctioned.Text, txtLAvailed.Text.Trim(),
                    txtBalance.Text, txtReasons.Text, Convert.ToString(dgvFacultyLeaveList.CurrentRow.Cells[0].Value).Trim());

                bool result = DatabaseAccess.UpdateData(updatequery);

                if (result)
                {
                    MessageBox.Show("Updated Successfully.");
                    EnableComponents();
                    FillGrid("");
                }
                else
                {
                    MessageBox.Show("Unexpected error occurred. Please contact the administrator.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            } 


What I have tried:

i try alot of other codes but its not updating the database all the values are corret
Posted
Comments
Richard MacCutchan 9-Dec-23 8:41am    
Have you previously validated all the input parameters that you are trying to store? By the look of that code you have not, so you could be using any random information. And why are you using two text strings to store a date and time instead of a proper DateTime type?
[no name] 9-Dec-23 12:28pm    
Incremental development: no parms, 1 parm, 2 parms, n parms.
Dave Kreskowiak 9-Dec-23 14:54pm    
The major problem with this code is you're using string concatentation to build the SQL query. ALWAYS USER PARAMETERIZED QUERIES! NEVER USER STRING CONCATENTATION!

Also, why are you storing Dates and Times as strings? You're only begging for problems when you go to search on those fields and when you sort on them.

1 solution

It's recommended to use parameterized queries to avoid SQL injection attacks and improve code readability for better debugging. There are some links are provided in above comments for your reference, here is an example to start.
C#
string updatequery = "UPDATE Facultyleave SET Faculty_ID = @FacultyID, LeaveType = @LeaveType, TotalBalance = @TotalBalance, Month = @Month, Days = @Days, LeaveDate = @LeaveDate, LeaveTime = @LeaveTime, EndDate = @EndDate, EndTime = @EndTime, SanctionedLeave = @SanctionedLeave, AvailedLeave = @AvailedLeave, Balance = @Balance, Reason = @Reason WHERE LeaveID = @LeaveID";
using (SqlCommand cmd = new SqlCommand(updatequery, "SqlConnection"))
{
    cmd.Parameters.AddWithValue("@FacultyID", txtID.Text.Trim());
    cmd.Parameters.AddWithValue("@LeaveType", cboLeaveType.Text.Trim());
    // Add other parameters similarly with correct datatype and value...

    cmd.Parameters.AddWithValue("@LeaveID", Convert.ToString(dgvFacultyLeaveList.CurrentRow.Cells[0].Value).Trim());

    bool result = DatabaseAccess.UpdateData(cmd);

    // Rest of your code..
}
 
Share this answer
 
Comments
Dave Kreskowiak 11-Dec-23 15:37pm    
I don't like .AddWithValue because it looks at the datatype of the value to determine how the value is going to be formatted. Newb's like to use strings for numeric ID values and datetimes, so those will get passed as strings instead of the type that they should be, like an integer or Date parameter.

I prefer specifying the datatype in the parameter so it is expected for the query and if you try to pass in a bad value type, like a string for an integer, this will be caught at compiler and/or runtime.
Richard Deeming 12-Dec-23 3:39am    
AddWithValue is still better than concatenating the parameter value into the SQL command. And going the whole hog of creating the parameter, setting its data type, size, precision, scale, and value, and then adding it to the command will likely scare the newbs and make them stick to the vulnerable code because it's "easier". :)
farzana Mengal 12-Dec-23 3:47am    
Thank You Everyone my problem is solved.
M Imran Ansari 12-Dec-23 12:49pm    
You're Welcome! We are glad to hear that.

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