0

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
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by dnanetwork
0

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();
    }
0

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

0

your connection string is wrong...

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

instead use....

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.