简体   繁体   中英

Problem with saving data in tables in asp.net with c#

Hello i have a problem with my application today it encountered a major problem :

I'm using vs2008 prof with it's sql server 2005 express edition.

The problem is that when i try to make an update on a row in one table the update.executeNonQuery() returns the value 1 that means that there was 1 row updated but the row is not updated.

If i try to modify the value manually by opening the table data and modifying a field it works and it saves but otherwise it does nothing.

I also tried to run it on a different machine with the same software kit and the same thing also tried to run it by IIS7 and the problem is not going away.

Can anybody give some suggestions of what should i do or if anyone encountered this problem i really need the help and i will highly appreciate it!

Later Edit : -- Added the source code

    int user_id = (int)Session["UserId"];
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    con.Open();
    SqlCommand update = new SqlCommand("update profil_candidat set nume = @pnume, prenume = @pprenume, data_nasterii = @pdata_nasterii, id_oras = @pid_oras , adresa = @padresa where id_utilizator=@pid_utilizator",con);
    update.Parameters.Add("pnume", SqlDbType.VarChar);
    update.Parameters.Add("pprenume", SqlDbType.VarChar);
    update.Parameters.Add("pdata_nasterii", SqlDbType.VarChar);
    update.Parameters.Add("pid_oras", SqlDbType.VarChar);
    update.Parameters.Add("padresa", SqlDbType.VarChar);
    update.Parameters.Add("pid_utilizator",SqlDbType.Int);
    int an = Convert.ToInt32(AnDropDownList.SelectedItem.Value);
    int luna = Convert.ToInt32(LunaDropDownList.SelectedItem.Value);
    int zi = Convert.ToInt32(ZiDropDownList.SelectedItem.Value);
    DateTime data = new DateTime(an, luna, zi);
    update.Parameters["pnume"].Value = TextBoxNume.Text;
    update.Parameters["pprenume"].Value = TextBoxPrenume.Text;
    update.Parameters["pdata_nasterii"].Value = data;
    update.Parameters["pid_oras"].Value = DropDownList1.SelectedItem.Value;
    update.Parameters["padresa"].Value = TextBoxAdresa.Text;
    update.Parameters["pid_utilizator"].Value = int.Parse(Session["UserId"].ToString());
    update.ExecuteNonQuery();
    con.Close();

Later edit

Issue solved seems that in fact it was related to :

C# Update Table using SqlCommand.Parameters ASP.NET

somebody forgot to put the form initialization in the page load in an if(Page.IsPostBack) and there was nothing wrong with the dbase but the parameters sent in the statement were always preloaded on event... still thank you for the prompt answers !

The last time I ran into this it was a permissions issue. For whatever reason SQL server wasn't returning the proper error message and instead was indicating that the underlying table was being updated... even though it wasn't.

Connect to your SQL Express instance using the exact same credentials as you have in your web.config. Then execute the same query and see what happens.

If executeNonQuery is returning 1, then it probably is updating successfully somewhere , otherwise you should get an exception thrown or it would return 0 . Check your connection string in your web.config and verify that your site is pointing to the database that you think it is.

Update :

The part of your connection string Data Source=.\\SQLEXPRESS means you are connecting to the SQL Server instance named SQLEXPRESS on localhost, so if you're deploying your project to a different computer, it will no longer be connecting to the same .mdf file that's stored on your computer, it will be using the .mdf file on the other computer. See this MSDN blog article for more details on working with a User Instance database and connecting to it from Server Management Studio:

Connecting to SQL Express User Instances in Management Studio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM