Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing simple student management system using ASP .Net by using 3 tier architecture. I have placed my data layer in db_connection.cs file under APP Code folder along with other class files those are acting as the business logic layer.

the code of db_connection.cs is

C#
public class db_connection
{
    public SqlConnection con;
    public SqlDataAdapter adp;
    public DataSet ds = new DataSet();
    public SqlCommand cmd;

	public db_connection()
	{
		
	}

    public void connection()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["ds"].ConnectionString);
        con.Open();
    }
 

    public int inupdel(string sql)
    {
        connection();
        cmd = new SqlCommand();
        return cmd.ExecuteNonQuery();

    }
}


And the students_details.cs file code --

public class student_details
{
    db_connection db = new db_connection();
	public student_details()
	{
    }


    public int insert(string name, string address, string city, string state, int pin, int dept_id, DateTime doa, int coursefee)
    {
       
            db.cmd = new SqlCommand("spCreateStudentDetails");
        db.cmd.CommandType = CommandType.StoredProcedure;
         
        db.cmd.Parameters.AddWithValue("@StudentName", name);
        db.cmd.Parameters.AddWithValue("@StudentAddress", address);
        db.cmd.Parameters.AddWithValue("@StudentCity", city);
        db.cmd.Parameters.AddWithValue("@StudentState", state);
        db.cmd.Parameters.AddWithValue("@StudentPIN", pin);
        db.cmd.Parameters.AddWithValue("@StudentDeptID", dept_id);
        db.cmd.Parameters.AddWithValue("@StudentAddmissionDate", doa);
        db.cmd.Parameters.AddWithValue("@StudentCourseFee", coursefee);
    
        student_details s = new student_details();
     
      //  return db.inupdel("insert into StudentDetails values ('"+name+"','"+address+"','"+city+"', '"+state+"','"+pin+"','"+dept_id+"','"+doa+"','"+coursefee+"')");
    }
}


When ever I am returning the sql it's working fine, but could not able to understand how to return the store procedure to the inupdel() in the db_connection.cs file. Please help. Thanks in advance.
Posted
Updated 18-May-14 10:43am
v2

I didn't test the following, but I think the idea is clear:

using(var conn = new SqlConnection(connStr)) {
 using(var cmd = new SqlCommand(storedProcedureName, conn) {
   cmd.CommandType = CommandType.StoredProcedure;
   // parameters
   return cmd.ExecuteNonQuery();
 }
}


there are some examples
here[^]
and
here[^]
 
Share this answer
 
I just need to pass exec spname inside student_details.cs's inupdel().
I.E. return db.inupdel("exec spCreateStudentDetails <parameters>"). That's it. Thanks @Vedat Ozan Oner for your reply.
 
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