Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void update_Click(object sender, EventArgs e)
{
    try
    {
        string con = @"Data Source=|DataDirectory|\students.sdf;Persist Security Info = True";
        SqlCeConnection conn = new SqlCeConnection(con);
        conn.Open();
        string command = "update info_ash SET name = @Name; roll = @Roll; hometown=@HomeTown;twitter=@twitter;birth_year=@Birth_Year;birth_month=Birth_Month; birth_Date = @Birth_Date; b_g = @Blood_Group; religion = @Religion; facebook = @Facebook_id; mobile = @Contact_no; email = @email; where id='" + search.Text + "'";
        SqlCeCommand cmd = new SqlCeCommand(command, conn);
        cmd.Parameters.AddWithValue("@Name", name.Text);
        cmd.Parameters.AddWithValue("@Roll", roll.Text);
        cmd.Parameters.AddWithValue("@Birth_Date", birth_date.Text);
        cmd.Parameters.AddWithValue("@Blood_Group", blood_group.Text);
        cmd.Parameters.AddWithValue("@Birth_Month", birth_month.Text);
        cmd.Parameters.AddWithValue("@Birth_Year", birth_year.Text);
        cmd.Parameters.AddWithValue("@Religion", religion.Text);
        cmd.Parameters.AddWithValue("@Facebook_id", facebook.Text);
        cmd.Parameters.AddWithValue("@Contact_no", mobile.Text);
        cmd.Parameters.AddWithValue("@HomeTown", home_town.Text);
        cmd.Parameters.AddWithValue("@email", email.Text);
        cmd.Parameters.AddWithValue("@twitter", twitter.Text);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Update Successfully Accomplished.");
        conn.Close();
    }
    catch
    {
        MessageBox.Show("Error occured");
    }

I have tried this code to update my local database (.sdf). Its showing update successful. But, it does not change the data in database. What is the problem with the code & how to get rid of it????
Posted
Updated 17-May-14 0:02am
v2

replace ; with ,

C#
string command = "update info_ash SET name = @Name, roll = @Roll, hometown=@HomeTown, twitter=@twitter, birth_year=@Birth_Year, birth_month=Birth_Month, birth_Date = @Birth_Date, b_g = @Blood_Group, religion = @Religion, facebook = @Facebook_id, mobile = @Contact_no, email = @email where id=@id";


and add the parameter id with all other parameters

C#
cmd.Parameters.AddWithValue("@id", search.Text);
 
Share this answer
 
v2
replace ";" with ","


C#
 string command = "update info_ash SET name = @Name, roll = @Roll, hometown=@HomeTown,twitter=@twitter,birth_year=@Birth_Year,birth_month=Birth_Month, birth_Date = @Birth_Date, b_g = @Blood_Group, religion = @Religion, facebook = @Facebook_id, mobile = @Contact_no, email = @email where id='" + search.Text + "'";
SqlCeCommand cmd = new SqlCeCommand(command, conn);
 
Share this answer
 
v2
Comments
DamithSL 17-May-14 7:46am    
this is incorrect email = @email, where
Nirav Prabtani 17-May-14 9:21am    
thanks for comment....i have updated it.. :)
Member 10825062 17-May-14 17:20pm    
It does not differ... :(
Please ensure that you are passing correct id in update query. This can be the case of not updating the record.

I would like to you to always use stored procedure instead of inline queries to reduce the chances of SQL Injection.
 
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