Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
An exception of type 'System.InvalidOperationException' occurred in System.Data.dll


What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
    public class ProductController : Controller
    {
        String cs = @"Data Source = DESIGNER\\SQLEXPRESS; Initial Catalog = MvcCrudDB; Integrated Security = True";

        [HttpGet]
        public ActionResult Index()
        {
            DataTable dtblProduct = new DataTable();
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("SELECT*FROM Product",con);
                da.Fill(dtblProduct);
                con.Close();
            }
            return View(dtblProduct);
        }
Posted
Updated 26-Apr-18 5:52am
v3

1 solution

Based on DbDataAdapter.Fill Method (DataTable) (System.Data.Common)[^] that exception is thrown if the source table is invalid.

So the chances are you don't have a table named Product in database MvcCrudDB or perhaps it's in another schema than dbo.
 
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