i have multiple checkboxes on my form.i am trying to add them with stored procedure but it gives me Procedure or function konut_ekle has too many arguments specified

protected void Button1_Click1(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
        SqlCommand cmd;
        con.Open();
        string cmdstr = "konut_ekle";
        cmd = new SqlCommand(cmdstr, con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@kategori", TextBox1.Text);
        cmd.Parameters.AddWithValue("@tip", TextBox2.Text);
        cmd.Parameters.AddWithValue("@il", TextBox3.Text);
        cmd.Parameters.AddWithValue("@ilce", TextBox4.Text);
        cmd.Parameters.AddWithValue("@semt", TextBox5.Text);
        cmd.Parameters.AddWithValue("@fiyat", TextBox7.Text);
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                cmd.Parameters.AddWithValue("@ozellik", CheckBoxList1.Items[i].Value);
            }
        }
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
    }
ALTER PROCEDURE konut_ekle
@kategori varchar(50),
@tip varchar(50),
@il varchar(50),
@ilce varchar(50),
@semt varchar(50),
@fiyat varchar(50),
@ozellik varchar(50)
AS


SET NOCOUNT ON


	
	insert into ilan (kategori,tip,il,ilce,semt)
	values (@kategori,@tip,@il,@ilce,@semt)
	
	declare @ilanid varchar(50)

	set @ilanid = scope_identity()

	insert into konut(ilanid,fiyat) values (@ilanid,@fiyat)
	
	insert into ozellikler(ilanid,ozellik) values (@ilanid,@ozellik)
	
	RETURN

Here is your problem. Loop adds more than parameters originally a stored procedure has.

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                cmd.Parameters.AddWithValue("@ozellik", CheckBoxList1.Items[i].Value);
            }
        }

You should concatenate the checkboxlist item's values and then assign a concatenated string as parameter value.

protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
        SqlCommand cmd;
        con.Open();
        string cmdstr = "konut_ekle";
        cmd = new SqlCommand(cmdstr, con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@kategori", TextBox1.Text);
        cmd.Parameters.AddWithValue("@tip", TextBox2.Text);
        cmd.Parameters.AddWithValue("@il", TextBox3.Text);
        cmd.Parameters.AddWithValue("@ilce", TextBox4.Text);
        cmd.Parameters.AddWithValue("@semt", TextBox5.Text);
        cmd.Parameters.AddWithValue("@fiyat", TextBox7.Text);

       // String for concatenate.
        String s="";
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                s=s + "  " + CheckBoxList1.Items[i].Value;
            }
        }
    cmd.Parameters.AddWithValue("@ozellik", s);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
    }

its working but it records all values in same row.i want to add these row by row and only for ozellikler table.not for all rows

ilanid ozellik
101 ADSL
101 Phone
101 TV

your connection string is wrong...

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString

instead use....

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].toString());

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.