Database tables not reflecting changes made during run-time
Hello. I am currently working on a project that makes several insert, update, and delete queries on a database. Everything works fine while the program is running. For example, after an insert, a Select * query shows that that row was successfully added. However, when I exit the program and return to the database explorer in Visual C# and click "Show Table Data", the item I just added during program execution is not in the table. I have tried doing adaptermanager.Update() and dataset.AcceptChanges() calls as well as using SqlCommand queries directly and none seem to be inserting the data into the actual database on the hard drive. Can you please tell me what I am doing wrong or what I should try instead? The database I am using is a Service-based Database added as a new item from the solution explorer window in Visual C# Express 2008, and is stored in the same folder as the project solution. Please let me know if you need anymore information that will clarify my problem. Thank you and I look forward to your reply.
EricPosted Mar 26, 2008, 7:41 PM
Josip MajicPosted Mar 19, 2008, 4:30 PM
The order you add parameters is very important; try using cmd.Parameters.AddWithValue.
EricPosted Mar 19, 2008, 3:52 PM
Josip MajicPosted Mar 19, 2008, 2:06 PM
EricPosted Mar 19, 2008, 10:59 AM
public bool Add()
{
SqlConnection connection = null;
try
{
//Here is my connection string
connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\TeamManagement.mdf;Integrated Security=True;User Instance=True");
connection.Open();
// Here is my insert command
SqlCommand cmd = new SqlCommand("INSERT INTO Administrator(username, password) Values(@Username, @Password)", connection);
//define the @Username and @Password parameters in the above command
SqlParameter p1 = new SqlParameter();
p1.ParameterName = "@Username";
p1.Value = username; //this is an instance variable of the class
SqlParameter p2 = new SqlParameter();
p2.ParameterName = "@Password";
p2.Value = password; //this is also an instance variable
// add parameters to command object
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
//execute the insert into sql command
cmd.ExecuteNonQuery();
cmd.Dispose();
//close the connection
connection.Close();
return true;
} // end try
.
.
.
} // end of Add method
Thanks again and let me know if you need any addition information about my system.
Josip MajicPosted Mar 19, 2008, 8:28 AM
Did you check your connection string that you're connected to the right database?
Can you post some code so we can verify?