0

I wrote a very simple method. It saves data from class DayWeather to the database. Method checks if line with that day exist in table and update her or create a new line.

I am doing it by adding new class for LINQ and move table from Server Inspector to the constructor. It generate new class WeatherTBL.

Method itself looks like this:

public static void SaveDayWeather(DayWeather day)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var existingDay =
              (from d in db.WeatherTBL
               where d.DateTime.ToString() == day.Date.ToString()
               select d).SingleOrDefault<WeatherTBL>();
            if (existingDay != null)
            {
                existingDay.Temp = day.Temp;
                existingDay.WindSpeed = day.WindSpeed;
                existingDay.Pressure = day.Pressure;
                existingDay.Humidity = day.Humidity;
                existingDay.Cloudiness = day.Cloudiness;
                existingDay.TypeRecip = day.TypeRecip;
                db.SubmitChanges();
            }
            else
            {
                WeatherTBL newDay = new WeatherTBL();
                newDay.DateTime = day.Date;
                newDay.Temp = day.Temp;
                newDay.WindSpeed = day.WindSpeed;
                newDay.Pressure = day.Pressure;
                newDay.Humidity = day.Humidity;
                newDay.Cloudiness = day.Cloudiness;
                newDay.TypeRecip = day.TypeRecip;
                db.WeatherTBL.InsertOnSubmit(newDay);
                db.SubmitChanges();
            }
        }
    }

When I tried to call him from UnitTest project:

  [TestMethod]
    public void TestDataAccess()
    {
        DayWeather day = new DayWeather(DateTime.Now);
        DataAccessClass.SaveDayWeather(day);
    }

It write, that test has passed successfully. But if look into table, it has`t chanched. No error messages shows. Does anyone know whats the problem?

P.S. Sorry for my bad English.

UDP Problem was in that: "...db maybe copied to the debug or release folder at every build, overwriting your modified one". Thanks @Silvermind

8
  • I am not sure why it is not storing the data. However your unit test is just calling the method but not using assert to validate the result. Commented May 5, 2013 at 19:21
  • @ARS, He don`t have result for validate. Method returns void. And I check the result into table... Commented May 5, 2013 at 19:30
  • I see. You can still write a method to retrieve the data and perform an assert. Anyway that is a different topic of discussion. Can you check the connection string to see if the data is inserted into the right db. Commented May 5, 2013 at 19:39
  • Besides the point offered by @ARS about the connection string, are your entity properties mapped? Commented May 5, 2013 at 19:50
  • 1
    If you did this 2.Drag the Customers node from Server Explorer/Database Explorer onto the O/R Designer surface. than they are automatically mapped, but if you change anything int the database after that you should redo it, or change Source in the dbml properties-page for that Entity, Also: your db maybe copied to the debug or release folder at every build, overwriting your modified one. Are you also looking at the right file (Check your Release/Debug folder)? Commented May 5, 2013 at 20:10

3 Answers 3

9

I wrote simple method to save employee details into Database.

private void AddNewEmployee()
{
    using (DataContext objDataContext = new DataContext())
    {                
        Employee objEmp = new Employee();
        // fields to be insert
        objEmp.EmployeeName = "John";
        objEmp.EmployeeAge = 21;
        objEmp.EmployeeDesc = "Designer";
        objEmp.EmployeeAddress = "Northampton";                
        objDataContext.Employees.InsertOnSubmit(objEmp);
        // executes the commands to implement the changes to the database
        objDataContext.SubmitChanges();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Please try with lambda expression. In your code, var existingDay is of type IQueryable In order to insert or update, you need a variable var existingDay of WeatherTBL type. Hence try using below..

 var existingDay =
 db.WeatherTBL.SingleOrDefault(d => d.DateTime.Equals(day.Date.ToString()));

 if(existingDay != null)
  {
   //so on...
  }

Hope it should work..

Comments

0

Linq to SQL

        Detail tc = new Detail();


        tc.Name = txtName.Text;
        tc.Contact = "92"+txtMobile.Text;
        tc.Segment = txtSegment.Text;
        var datetime = DateTime.Now;
        tc.Datetime = datetime;
        tc.RaisedBy = Global.Username;
        dc.Details.InsertOnSubmit(tc);

        try
        {


            dc.SubmitChanges();
            MessageBox.Show("Record inserted successfully!");
            txtName.Text = "";
            txtSegment.Text = "";
            txtMobile.Text = "";

        }


        catch (Exception ex)
        {
            MessageBox.Show("Record inserted Failed!");

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.