Victor Charles
asked on
Error with entering records in SQL Table using ASP.NET MVC
Hello,
I am getting the following error when attempting to add more than one record to a table.
System.Data.SqlClient.SqlE xception: 'Violation of PRIMARY KEY constraint 'PK__AOP6__3214EC075FC6748 8'. Cannot insert duplicate key in object 'dbo.AOP6'. The duplicate key value is (0).
The statement has been terminated.'
Any ideas what is causing this error?
I am using ASP.NET MVC.
Code:
using System;
using System.Collections.Generic ;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DataLibrary.DataAccess
{
public static class SqlDataAccess
{
public static string GetConnectionString(string connectionName = "MVCDemoDB")
{
return ConfigurationManager.Conne ctionStrin gs[connect ionName].C onnectionS tring;
}
public static List<T> LoadData<T>(string sql)
{
using (IDbConnection cnn = new SqlConnection(GetConnectio nString()) )
{
return cnn.Query<T>(sql).ToList() ;
}
}
public static int SaveData<T>(string sql, T data)
{
using (IDbConnection cnn = new SqlConnection(GetConnectio nString()) )
{
return cnn.Execute(sql, data); *****ERROR Line*****
}
}
}
}
Thanks,
Victor
I am getting the following error when attempting to add more than one record to a table.
System.Data.SqlClient.SqlE
The statement has been terminated.'
Any ideas what is causing this error?
I am using ASP.NET MVC.
Code:
using System;
using System.Collections.Generic
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DataLibrary.DataAccess
{
public static class SqlDataAccess
{
public static string GetConnectionString(string
{
return ConfigurationManager.Conne
}
public static List<T> LoadData<T>(string sql)
{
using (IDbConnection cnn = new SqlConnection(GetConnectio
{
return cnn.Query<T>(sql).ToList()
}
}
public static int SaveData<T>(string sql, T data)
{
using (IDbConnection cnn = new SqlConnection(GetConnectio
{
return cnn.Execute(sql, data); *****ERROR Line*****
}
}
}
}
Thanks,
Victor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER