0

I'm trying to put form variables into a database, however, I'm still VERY new at these things and I'm just trying random copy pasted code from the internet to learn. This is the only thing giving me problems

@using WebMatrix.Data;
@using WebMatrix.WebData;
@using System.Data.SqlClient;


@{
    ViewBag.Title = "Recruta";

}

@{ 

    var Nome = "";
    var Email = "";
    var Tel = "";
    var Adress = "";
    var Gender = "";

    Nome = Request.Form["Nome"];
    Email = Request.Form["Email"];
    Tel = Request.Form["Tel"];
    Adress = Request.Form["Adress"];
    Gender = Request.Form["Gender"];

    var db = Database.Open("Usuarios");
    var insertCommand = "INSERT INTO Usuarios (Nome, Email, Tel, Adress, Gender) Values(@0, @1, @2, @3, @4)";
    db.Execute(insertCommand, Nome, Email, Tel, Adress, Gender);
    Response.Redirect("~/Usuarios");
        }

This is the information given by vs:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: The database 'c:\users\softinsa\documents\visual studio 2015\Projects\Best_prototype_01\Best_prototype_01\App_Data\Usuarios.mdf' cannot be opened because it is version 851. This server supports version 706 and earlier. A downgrade path is not supported.

Cannot attach the file 'c:\users\softinsa\documents\visual studio 2015\Projects\Best_prototype_01\Best_prototype_01\App_Data\Usuarios.mdf' as database 'c:\users\softinsa\documents\visual studio 2015\Projects\Best_prototype_01\Best_prototype_01\App_Data\Usuarios.mdf'.

And this is where the error lies apparently:

  db.Execute(insertCommand, Nome, Email, Tel, Adress, Gender);

I apologize if I'm posting a dumb question, as I said, I'm still very fresh in asp.net and it's endevours.

I appreciate you reading this and hopefully you can help.

EDIT: I tried using the solutions on The database cannot be opened because it is version 782. This server supports version 706 and earlier. A downgrade path is not supported and none of them worked for me, I'm using VS Community edition 2015, by default the connections are already (LocalDB)\MSSQLLocalDB so that isn't the issue here.

Community
  • 1
  • 1

1 Answers1

2

First, never ever code business logic inside your ASP.NET MVC views.

Second, the exception is quite clear:

The database '...' cannot be opened because it is version 851. This server supports version 706 and earlier. A downgrade path is not supported.

You don't have the right version of SQL Server running on that system. You are trying to open a newer database file format version than you have installed. Either install the correct version of SQL Server, or downgrade the database file format.

This might be related to your use of Visual Studio 2015 update 2.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • It's WebMatrix, they [actually encourage putting logic in views in their tutorials](http://www.asp.net/web-pages/overview/data/5-working-with-data). Then again, the MVC tutorials use Entity models as View models... – CodeCaster May 09 '16 at 09:49
  • Thanks for your answer, how would I go about downgrading the databse file format? I have no clue which version of SQL Server I need to have in order to fix the error, so I think downgrading would be the safer approach. – Ruben Pereira May 09 '16 at 09:52
  • I guess you can do that from SQL Server Management Studio, if you have installed it. – Patrick Hofman May 09 '16 at 10:30