Click here to Skip to main content
15,559,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my first class is CustomerDal.cs
C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;

/// <summary>
/// Summary description for Class2
/// </summary>
public class CustomerDal
{
	public CustomerDal()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public int insert(string customer_id,string first_name,string last_name)  // public int insert(string customer_id,string first_name,string last_name)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["learnConnectionString"].ConnectionString);
        string s1 = "insert into customer(customer_id,first_name,last_name) values(@customer_id,@first_name,@last_name) ";
        SqlCommand cmd = new SqlCommand(s1,conn);
        //set the command type
        cmd.CommandType = CommandType.Text;
        try
        {
            cmd.Parameters.Add(new SqlParameter("customer_id", System.Data.SqlDbType.Int, 4));
           cmd.Parameters["@customer_id"].Direction = System.Data.ParameterDirection.Input;
            cmd.Parameters["@customer_id"].Size = 25;
            cmd.Parameters["@customer_id"].Value = customer_id;

            cmd.Parameters.Add(new SqlParameter("first_name", SqlDbType.VarChar));
            cmd.Parameters["@first_name"].Direction = ParameterDirection.Input;
            cmd.Parameters["@first_name"].Size = 25;
            cmd.Parameters["@first_name"].Value = first_name;

            cmd.Parameters.Add(new SqlParameter("last_name", SqlDbType.VarChar));
            cmd.Parameters["@last_name"].Direction = ParameterDirection.Input;
            cmd.Parameters["@last_name"].Size = 25;
            cmd.Parameters["@last_name"].Value = last_name;
            conn.Open();
           return cmd.ExecuteNonQuery();
        }
        catch
        {
          throw;
        }
        finally
        {
           conn.Close();
           cmd.Dispose();
            conn.Dispose();
        }
    }
}

my second class ic CustomerBal.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
public class CustomerBal
{
	public CustomerBal()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public int insert(string customer_id, string first_name, string last_name)
    {
        CustomerDal cdal = new CustomerDal();
        try
        {
            return cdal.insert(customer_id, first_name, last_name);
        }
        catch 
        {
            throw;
        }
        finally
        {
            cdal=null;
        }
    }
}

and my default.aspx page is
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CustomerBal Cbal = new CustomerBal();
        try
        {
            if (Cbal.insert(TextBox1.Text, TextBox2.Text, TextBox3.Text) > 0)
            {
                Label1.Text = "Record Inserted Successfully";
            }
            else
            {
                Label1.Text = "Record not Inserted";
            }
        }
        catch(Exception ex)
        {
            Label1.Text = ex.Message;

        }
        finally 
        {
            Cbal = null;
        }
    }
}

and when i run this program than the error is coming
Error:An SqlParameter with ParameterName '@customer_id' is not contained by this SqlParameterCollection.
please give me solution......
Posted
Updated 3-Apr-13 23:06pm
v2
Comments
Anurag Sinha V 4-Apr-13 4:11am    
your code looks pretty ok..just try to include the actual values in the DAL file...
cmd.Parameters.Add(new SqlParameter("@customer_id",and else))....

-anurag

You are using

cmd.Parameters.Add(new SqlParameter("customer_id", System.Data.SqlDbType.Int, 4));

I think it should be

cmd.Parameters.Add(new SqlParameter("@customer_id", System.Data.SqlDbType.Int, 4));
 
Share this answer
 
Comments
sakshi111 4-Apr-13 4:52am    
thank you very much
the problem is resolved but now a new problem arised
eror is:Invalid object name 'Customer'
Deependra Khangarot 4-Apr-13 6:01am    
Please check your database for table name "Customer".
CSS
thank you very much
the problem is resolved but now a new problem arised
eror is:Invalid object name 'Customer'
 
Share this answer
 
Please check out that the table name which you have specify into the code is exist in your database or not and if it is exist then check out the we.config file for your connection inforamtion.
 
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