Reputation: 167
I am trying a crud operation in MVC from a website tutorial. I write code for save and display data in my webpage. I add connectionstring in my webconfig and follow the instruction as per tutorial. But now the problem is I does not find my data in my sql server database who's connectionstring I provide.
I don't know where it is saving my data I write this code for save and display data
for display data I write this in my controller
public ActionResult Index()
{
var books = from book in _db.Book
select book;
return View(books.ToList());
}
This is for insert data
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
Books book = new Books();
if (ModelState.IsValid)
{
book.Title = collection["Title"].ToString();
book.ISBN = collection["ISBN"].ToString();
book.Price = Convert.ToDecimal(collection["Price"]);
_db.Book.Add(book);
_db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
And in my model I write this code for one class
public class BookDBContext:DbContext
{
public DbSet<Books> Book { get; set; }
}
another class
public class Books
{
public Int64 ID { get; set; }
public String Title { get; set; }
public String ISBN { get; set; }
public Decimal Price { get; set; }
}
and I add connectionstring my webcofig.
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ASPNETMVCApplication-20140628114535;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ASPNETMVCApplication-20140628114535.mdf" providerName="System.Data.SqlClient" />-->
<add name="con" connectionString="Data Source=COMPUTER18-PC\SQLEXPRESS;Intial Catalog=practice;UID=sa;pwd=***;"/>
</connectionStrings>
Where is my data saving?know.I need help about this topic I am not such aware about MVC.
Upvotes: 1
Views: 542
Reputation: 2083
It seems you are using VS12 or VS13.
Go to View -> SQl Server Object Explorer.
Look for server named (LocalDb)\v11.0
expand the database(name given by @Pascalz) in that server, you will find your data in one of the tables.
The template in visual stuio will create the database by default for saving account and membership information.
If you do not see (LocalDb)\v11.0, then try adding it by clicking Add Sql Server icon and connect using windows authentication
Upvotes: 1