I am developing my application in Visual Studio 2012 with .NET Framework 4.5 and using an MDF file as local db. Problem here is changes made to data at runtime are not getting saved permanently. However, I am able to retrieve data at runtime even save it but they are not displayed in Server explorer. I have already set the "Copy to Output directory" property of MDF file to "Copy if newer" but problem still exists. How do i solve it?
This is the connection string in my application's app.config. I had earlier tried Seperate data set .xsd file but to no avail.
<connectionStrings>
<add name="EA_Logistics_Control.Properties.Settings.LogisticsLocalDBCS"
connectionString="Data Source=(LocalDB)\v11.0
AttachDbFilename=|DataDirectory|\LocalDBs\LogisticsLocalDB.mdf;Integrated
Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Some might suggest to :
- Use SQLExpress as server instead of LocalDB or
- Try to connect to LocalDB programmatically and replace the bin\Debug with "folder_name\file_name.mdf"
but then they are not actual solutions, just alternative. Can anyone please explain what is happening here? and how do I solve it?
As requested, C# code is:
DateTime dt = DateTime.Now;
string CDate = dt.Month + "/" + dt.Day + "/" + dt.Year;
int norid = 0, neeid = 0, cfrom = 0, cto=0;
SqlConnection SCon = new SqlConnection(ConfigurationManager.ConnectionStrings["EA_Logistics_Control.Properties.Settings.LogisticsLocalDBCS"].ConnectionString);
if (SCon.State == ConnectionState.Closed)
SCon.Open();
SqlCommand SCom = new SqlCommand("INSERT INTO Consignments VALUES(@CNo, @CDate, @InvoiceNo, @SignorID, @SigneeID, @TravelFrom, @TravelTo, @Packages, @Contents, @VehicleNo, @Rate, @Weight, @Amount, '')", SCon);
SCom.Parameters.AddWithValue("CNo", TB_IC_CN.Text);
SCom.Parameters.AddWithValue("CDate", CDate);
SCom.Parameters.AddWithValue("InvoiceNo", TB_IC_O_Inv.Text);
SCom.Parameters.AddWithValue("SignorID", norid);
SCom.Parameters.AddWithValue("SigneeID", neeid);
SCom.Parameters.AddWithValue("TravelFrom", cfrom);
SCom.Parameters.AddWithValue("TravelTo", cto);
SCom.Parameters.AddWithValue("Packages", TB_IC_NUM_O_Pkgs.Text);
SCom.Parameters.AddWithValue("Contents", TB_IC_Desc.Text);
SCom.Parameters.AddWithValue("VehicleNo", TB_IC_O_Veh.Text);
SCom.Parameters.AddWithValue("Rate", TB_IC_NUM_O_Rate.Text);
SCom.Parameters.AddWithValue("Weight", TB_IC_NUM_O_Wt.Text);
SCom.Parameters.AddWithValue("Amount", TB_IC_NUM_Amt.Text);
int resRows = SCom.ExecuteNonQuery();
if (resRows > 0)
{
MessageBox.Show("New entry has been inserted.");
}
It does show me a message box and I have also seen number of rows affected while debugging.