I built a simple test app with one form when submitting the form the form data should be saved in database
namespace WebApplication3.Controllers
{
public class Home-controller : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Customer customer)
{
// string ATNMEntities = ConfigurationManager.ConnectionStrings["ATNMEntities"].ConnectionString;
using (SqlConnection con = new SqlConnection("data source=A3LABPCLENOVO\\SQLEXPRESS;initial catalog=ATNM;integrated security=True"))
{
string query = "INSERT INTO Customers(Customerid,Name, Country) VALUES(@Customerid,@Name, @Country)";
query += " SELECT SCOPE_IDENTITY()";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Customerid", customer.Customerid);
cmd.Parameters.AddWithValue("@Name", customer.Name);
cmd.Parameters.AddWithValue("@Country", customer.Country);
// customer.Customerid = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
return View(customer);
}
}
}
Connection string:
<connectionStrings>
<add name="ATNMEntities"
connectionString="metadata=res://*/Models.AdoEntityDataModel1.csdl|res://*/Models.AdoEntityDataModel1.ssdl|res://*/Models.AdoEntityDataModel1.msl;provider= System.Data.EntityClient;provider connection string="data source=A3LABPCLENOVO\SQLEXPRESS;initial catalog=ATNM;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>