Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System.Web.Mvc;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/
        
        public void FillDataInTest()
        {
            Test test = new Test(); //Test is entity class
            SqlConnection objConn = new SqlConnection(@"server=DEV;Trusted_Connection=true;" + @"DATABASE=bank;User ID=BReader;Password=br123");
            SqlCommand objCommand = new SqlCommand(@"Select Pub_bank_id,pub_bank_branch FROM parameter_online", objConn);

            SqlDataAdapter da = new SqlDataAdapter(objCommand);
            DataTable dt = new DataTable();

            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count-1; i++)
            {
                test.Id =Convert.ToInt16(dt.Rows[i]["Pub_bank_id"]);
                test.Name = dt.Rows[i]["Pub_bank_id"].ToString();
            }
        }
        public ActionResult Index()
        {
            try
            {
               
               TestDBContext db = new TestDBContext();
               FillDataInTest();
               ViewBag.Id = db.Tests.OrderBy(g => g.Id).ToList();
               ViewBag.Name = db.Tests.OrderBy(a => a.Name).ToList();
                var test = new Test();
                return View(test);
            }
            catch (Exception e)
            {
                
                throw;
            }
        
        }
    }
}

When i going to run this code its throw an exception "{"An error occurred while executing the command definition. See the inner exception for details."}
"

I cant understand why its throw exception
Posted
Updated 27-Feb-11 20:37pm
v2
Comments
Albin Abel 28-Feb-11 2:41am    
what was the inner exception you got. Also you FillDataInTest not return the test and you are passing new Test() to the view which is blank. Is that way you want?
juwel115 28-Feb-11 2:46am    
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MvcApplication2.Models
{
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Limit { get; set; }

}
public partial class TestDBContext : DbContext
{
public DbSet<test> Tests { get; set; }
}
}
Sorry i add db.Tests.Add(test) to fill it. but it throws exception.
jim lahey 28-Feb-11 6:55am    
Your for loop also misses the last DataRow:

for (int i = 0; i < dt.Rows.Count-1; i++) //shouldn't this be i < dt.Rows.Count; ?
{
test.Id =Convert.ToInt16(dt.Rows[i]["Pub_bank_id"]);
test.Name = dt.Rows[i]["Pub_bank_id"].ToString();
}

is that intentional?
[no name] 1-Mar-11 3:34am    
can you post what is line number or what is the error message in exception...

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