Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

namespace ADO_MVC
{
    public class ADOMVC
    {       
        public int Insert(string query)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con_String"].ToString());
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = query;
            int res = cmd.ExecuteNonQuery();
            conn.Close();
            return res;
        }
    }
}

Note : The error is given in SqlConnection ("con_String")
Posted
Updated 6-Oct-15 2:43am
v2
Comments
Member 11392252 6-Oct-15 8:26am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ADO_MVC.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

return View();
}
public ActionResult InsertResult()
{
ADO_MVC.Models.Info data = new Models.Info();
data.Fname = Request["fname"];
data.Lname = Request["lname"];

int res = data.Insert();
if(res>0)
{
Response.Write("Insert Successfull....");

}
else
{
return RedirectToAction("Index", "Home");
}
return View();
}

public ActionResult About()
{
ViewBag.Message = "Your app description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
}
}
This is the HomeController

@model ADO_MVC.Models.Info
@{
ViewBag.Title = "InsertResult";
}

InsertResult


<html>
<body>
@using (Html.BeginForm("InsertResult", "Home"))
{
<fieldset>
My Information
<table>
<tr>
<td>@Html.LabelFor(model => model.ID)</td>
<td>@Html.TextBox("id")</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.Fname)</td>
<td>@Html.TextBox("fname")</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.Lname)</td>
<td>@Html.TextBox("lname")</td>
</tr>
<tr>
<td>@Html.TextBox("btn_submit", "Insert Data", new { type = "submit" })</td>
</tr>
</table>
</fieldset>
}

</body>
</html>
This is the Index Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ADO_MVC.Models
{
public class Info
{
public int ID { get; set; }
public string Fname { get; set; }
public string Lname { get; set; }

public int Insert(Info data)
{
string query = "insert into myinfo values('"+data.Fname+"','"+data.Lname+"')";
ADOMVC ado = new ADOMVC();
int res = ado.Insert(query);
return res;
}
public int Insert()
{
string query = "insert into myinfo values('" + Fname + "','" + Lname + "')";
ADOMVC ado = new ADOMVC();
int res = ado.Insert(query);
return res;
}


}

}
And this is the Model Info.cs
ZurdoDev 6-Oct-15 8:31am    
Which line of code causes it?
Member 11392252 6-Oct-15 8:33am    
In SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con_String"].ToString());
(Here)
ZurdoDev 6-Oct-15 8:40am    
Then ConfigurationManager.ConnectionStrings["con_String"] is null.
F-ES Sitecore 6-Oct-15 8:49am    
Check the connection string is defined in the right place, ie the web.config file in the site root. If your code is in a separate project that projects app.config file is NOT used.

1 solution

Check whether connection string configured properly or not in web.config, in your case it return null that is the reason it's throwing null reference error. Please debug the program and check line by line and found the issue.
 
Share this answer
 
Comments
Member 11392252 6-Oct-15 9:06am    
<add name="ADO_mvc" connectionstring="Data source=.\sqlexpress; Initial Catalog=ADO_mvc; Integrated Security=true ">
This is my Connection string.

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