Click here to Skip to main content
15,918,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am having problems updating my MS SQL database from my C# application. I can read from the database but my updates do not work. Below is an example


I have a value of 'AAA' in field BM_BadgeID. When I run the select statement below it returns 'AAA' as expected to variable LastTran. Then when the Update statement runs it would seem to update the value to 'TEST-7' - however when I inquire on the database it is still set to 'AAA'

If I run this code a 2nd time then the select returns TEST-7 to field LastTran (as though it were updated on the database). However if I end the application and restart the value is back to 'AAA'.

I don't understand what I am doing incorrectly. Any help/advise would be appreciated.

regards
Pat

C#
//Basic SELECT method to populate a DataSet from a SqlDataAdapter
ConnectionStr = globals.MyTrans.p1_RtvConnMSSQLDB_NATIVE;
SqlConnection sqlConn = new SqlConnection(ConnectionStr);
SqlDataAdapter sqlAdapt = new SqlDataAdapter("SELECT * FROM badge_master  WHERE BM_BadgeID='123123'",sqlConn);
SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(sqlAdapt);
DataSet sqlSet = new DataSet();
sqlAdapt.Fill(sqlSet, "badge_master");
DataRow dRow = sqlSet.Tables["badge_master"].Rows[0];
string name = dRow.ItemArray.GetValue(1).ToString();            // Badge Name
string LastTran = dRow.ItemArray.GetValue(11).ToString();       // Last Transaction name
sqlConn.Close();

C#
//Basic UPDATE method with Parameters
ConnectionStr = globals.MyTrans.p1_RtvConnMSSQLDB_NATIVE;
SqlCommand sqlComm = new SqlCommand();
sqlComm = sqlConn.CreateCommand();
sqlComm.CommandText = "Update badge_master set BM_LastTranName='TEST-7' where BM_BadgeID='123123'";
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
Posted
Updated 9-Sep-13 8:30am
v2
Comments
Mojtaba Eng 9-Sep-13 14:39pm    
sqlComm.CommandText = "Update badge_master set BM_LastTranName='TEST-7' where BM_BadgeID='123123'";
1- check if BM_LastTranName datatype is Nvarchar change your query to BM_LastTranName=N'TEST-7'
2- debug your code and get sqlComm.CommandText and try run that in query analyzer or query in Management studio

1 solution

You had no code that associated the connectionstring to the connection. You had no code that associated the SQLCommand object with the SQLConnection object.

Try this
//Basic UPDATE method with Parameters
ConnectionStr = globals.MyTrans.p1_RtvConnMSSQLDB_NATIVE;
sqlConn = new SQLConnection(ConnectonStr);
sqlConn.Open();
SqlCommand sqlComm = new SqlCommand("Update badge_master set BM_LastTranName='TEST-7' where BM_BadgeID='123123'"
,sqlConn);
sqlComm.ExecuteNonQuery();
sqlConn.Close();
 
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