This code down here has errors when I run it in visual studio 2022. Kindly help out. I am quite new to MVC architecture
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; // Required for using Dataset , Datatable and Sql
using System.Data.SqlClient; // Required for Using Sql
using System.Configuration; // for Using Connection From Web.config
using BussinessObject; // for acessing bussiness object class
namespace DataAccess
{
public class UserDA
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Myconstr"].ToString());
public int AddUserDetails(UserBO ObjBO) // passing Bussiness object Here
{
try
{
/* Because We will put all out values from our (UserRegistration.aspx) To in Bussiness object and then Pass it to Bussiness logic and then to DataAcess this way the flow carry on*/
SqlCommand cmd = new SqlCommand("sprocUserinfoInsertUpdateSingleItem", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", ObjBO.Name);
cmd.Parameters.AddWithValue("@Address", ObjBO.address);
cmd.Parameters.AddWithValue("@EmailID", ObjBO.EmailID);
cmd.Parameters.AddWithValue("@Mobilenumber", ObjBO.Mobilenumber);
con.Open();
int Result = cmd.ExecuteNonQuery();
cmd.Dispose();
return Result;
}
catch
{
throw;
}
}
}
}
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 4, 2023, 2:18 AM EDT
public int AddUserDetails(UserBO ObjBO) { try { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Myconstr"].ToString())) { using (SqlCommand cmd = new SqlCommand("sprocUserinfoInsertUpdateSingleItem", con)) { cmd.CommandType = CommandType.StoredProcedure; // Use SqlParameter to specify parameter data types explicitly cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 100)).Value = ObjBO.Name; cmd.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar, 200)).Value = ObjBO.address; cmd.Parameters.Add(new SqlParameter("@EmailID", SqlDbType.VarChar, 100)).Value = ObjBO.EmailID; cmd.Parameters.Add(new SqlParameter("@Mobilenumber", SqlDbType.VarChar, 15)).Value = ObjBO.Mobilenumber; con.Open(); int Result = cmd.ExecuteNonQuery(); return Result; } } } catch { throw; } }Cymon Maxwell8 reputationPosted Aug 4, 2023, 6:45 AM EDT
The two responses seem not to work. I am getting the following errors:
Severity Code Description Project File Line Suppression State
Error CS1069 The type name 'ConnectionStringSettings' could not be found in the namespace 'System.Configuration'. This type has been forwarded to assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. DataAccessLayer C:\Users\ICT\source\repos\DataAccessLayer\UserDA.cs 13 Active
Severity Code Description Project File Line Suppression State
Error CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly. DataAccessLayer C:\Users\ICT\source\repos\DataAccessLayer\UserDA.cs 14 Active
Severity Code Description Project File Line Suppression State
Error CS1061 'object' does not contain a definition for 'ConnectionStrings' and no accessible extension method 'ConnectionStrings' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) DataAccessLayer C:\Users\ICT\source\repos\DataAccessLayer\UserDA.cs 13 Active
Severity Code Description Project File Line Suppression State
Error CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly. DataAccessLayer C:\Users\ICT\source\repos\DataAccessLayer\UserDA.cs 14 Active
Severity Code Description Project File Line Suppression State
Error CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly. DataAccessLayer C:\Users\ICT\source\repos\DataAccessLayer\UserDA.cs 30 Active
Severity Code Description Project File Line Suppression State
Warning MSB3290 Failed to create the wrapper assembly for type library "{bee4bfec-6683-3e67-9167-3c0cbc68f40a}". Type library 'System' was exported from a CLR assembly and cannot be re-imported as a CLR assembly. DataAccessLayer C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 2977
Cr BhargaviRank #420 · 5.9K reputationPosted Aug 3, 2023, 9:43 PM EDT
Please try the below code:
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using BussinessObject; namespace DataAccess { public class UserDA { private readonly string connectionString; public UserDA() { connectionString = ConfigurationManager.ConnectionStrings["Myconstr"].ToString(); } public int AddUserDetails(UserBO ObjBO) { try { using SqlConnection con = new SqlConnection(connectionString); using SqlCommand cmd = new SqlCommand("sprocUserinfoInsertUpdateSingleItem", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Name", ObjBO.Name); cmd.Parameters.AddWithValue("@Address", ObjBO.Address); cmd.Parameters.AddWithValue("@EmailID", ObjBO.EmailID); cmd.Parameters.AddWithValue("@Mobilenumber", ObjBO.MobileNumber); con.Open(); int Result = cmd.ExecuteNonQuery(); return Result; } catch (Exception ex) { // Handle the exception here or throw it to the calling code. throw; } } } }