Click here to Skip to main content
15,886,565 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 saved permanently. 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 and associated DataAdapters to save data but to no avail.

XML
<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:
1. Use SQLExpress as server instead of LocalDB or
2. 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 alternatives. Can anyone please explain what is happening here? and how do I solve it?

C# code is:

C#
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.
Posted
Updated 30-Apr-15 21:23pm
v2
Comments
Akshay Raut 1-May-15 3:29am    
People who downvoted my questions, thank you for at least looking at it but it would be more helpful if you also pointed out what was so obvious about this problem or better provide a solution because not all of us here are experts. We can't learn if we can't make mistakes.

1 solution

I received help from some people on this problem and solution was that I was checking for existence of data in MDF file of my project folder through Server Explorer, what I should have done instead was to check the MDF file in bin\Debug folder where my data were permanent. I did it by creating a new connection in Server Explorer to output MDF file. I wrongly assumed that updated MDF file will overwrite the one in project folder as it is supposed to be feature of "Copy if newer". Actually it copies original file to bin folder rather than other way around. Quite obviously, the MDF fill will be empty for fresh installation and after deployment, application on user side will only refer to the deployed file i.e. similar to the one in bin\Debug.
 
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