Click here to Skip to main content
15,908,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to class library.I am trying to crate data access layer for my -tier website but not able to use sqlcommand or any other ADO.net object.
My code is like below:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
Using System.Configuration;
namespace TEST.DAL
{
    public class AdminDAL
    {
       public static SqlConnection conn=new SqlConnection(ConfigurationManager"dbCon").ConnectionString));
	SqlCommand cmd=new SqlCommand("sp_insert",conn);

...
...
}
}


Now i can not use this cmd object to declare parameters or any attributes.The intellisense for cmd also not coming even after decalring sqlcommand as static i could not be able to use it.Please help me .
Posted

corrected code:

C#
public static SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["yourconnectionstringname"].ConnectionString);
        SqlCommand cmd = new SqlCommand("sp_insert", conn);


note: dont create objects unnecessarily outside the methods.

Access the cmd object inside a method/Function, you could able to see the intellisense now


SqlCommand object should be used inside the methods unless it is common to the entire module.
 
Share this answer
 
Comments
sudevsu 30-Apr-15 14:53pm    
5
Karthik_Mahalingam 30-Apr-15 14:56pm    
both our solutions are almost same :) .. +5
First of all, wrap your logic inside a method. Because that's whart is needed to access the functionality in other classes.
Example-
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
Using System.Configuration;
namespace TEST.DAL
{
    public class AdminDAL
    {
       public static SqlConnection conn=new SqlConnection(ConfigurationManager"dbCon").ConnectionString));
public int ExecuteSPNonQuery()
{
	SqlCommand cmd=new SqlCommand("sp_insert",conn);
.       //rest of the logic here

Now, you should be able to access this method with the instance of the class.

Hope, it helps :)
 
Share this answer
 

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