Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai


I have a stored procedure "sampledata1".In the code behind file i am inserting the values into table by using stored procedure.But i am getting error "Procedure or function sampledata1 has too many arguments specified.".Below is the code.


Stored procedure;

create procedure sampledata1
(
@incidentnumber int,
@priority varchar(5),
@primaryimpactedarea varchar(50),
@secondaryimpactedarea varchar(50)
)
as
BEGIN
insert into Incidents(IncidentNumber,Priority,PrimaryImpactedArea,SecondaryImpactedArea)values(@incidentnumber,@priority,@primaryimpactedarea,@secondaryimpactedarea)
END


Button click event in codebehind:

SqlConnection con = new SqlConnection("Data Source=CDC015;Initial Catalog=Dashboard;User ID=Rash;Password=1111*");
       con.Open();
       SqlCommand cmd = new SqlCommand("sampledata1", con);
       cmd.CommandType = CommandType.StoredProcedure;
       try
       {

           cmd.Parameters.AddWithValue("@incidentnumber", txtIncidentNumber.Text);
           cmd.Parameters.AddWithValue("@priority", txtPriority.Text);
           string n = string.Empty;
           foreach (ListItem li in lstPrimaryImpactedArea.Items)
           {
               if (li.Selected == true)
               {
                   n += lstPrimaryImpactedArea.SelectedItem.Value;
                   cmd.Parameters.AddWithValue("@primaryimpactedarea", n);
               }
           }


           //cmd.Parameters.AddWithValue("@primaryimpactedarea",lstPrimaryImpactedArea.SelectedItem.Text);
           cmd.Parameters.AddWithValue("@secondaryimpactedarea",lstSecondaryImpactedArea.SelectedItem.Text);




            cmd.ExecuteNonQuery();
       }
       catch
       {
           throw;
       }
       finally
       {
           cmd.Dispose();
           con.Close();
           con.Dispose();
       }


Thanks in advance
Posted
Updated 11-Dec-13 18:41pm
v2
Comments
♥…ЯҠ…♥ 12-Dec-13 0:31am    
Debug and see how many parameters are passed to the stored proc?
Member 10234093 12-Dec-13 0:33am    
I have debugged the code.Arguments that are passed in SP is 4 only.In code behind also only 4 arguments.Maybe in foreach loop there is some mistake.Could you plz clarify this.

Check This line is correct or not:-
you have mentioned sampledata1 Sp and you passing that parameters to sampledata2.
SqlCommand cmd = new SqlCommand("sampledata2", con);
 
Share this answer
 
Comments
Member 10234093 12-Dec-13 0:28am    
sry.It is sampledata1 only.But still i am getting that error.I think in foreach loop it is mistake.Can u plz help me
TrushnaK 12-Dec-13 0:40am    
ya that is problem
what you want to do in foreach loop.
Member 10234093 12-Dec-13 1:40am    
To select multiple items in listbox
Hi,

Lemme guess you are iterating parameters inside foreach,
try the below code and check
C#
foreach (ListItem li in lstPrimaryImpactedArea.Items)
            {
                if (li.Selected == true)
                {
                    n += lstPrimaryImpactedArea.SelectedItem.Value;
                   //cmd.Parameters.AddWithValue("@primaryimpactedarea", n); Deprecate it and assign after loop
                }
            }
cmd.Parameters.AddWithValue("@primaryimpactedarea", n);


Updated as per your comments:

You can use SelectedItems to get desired selected items from listbox like this
C#
for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    n += ListBox1.Items[i].Value;
                }
            }


Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v5
Comments
Member 10234093 12-Dec-13 0:50am    
Thanks a lot.But one more issue is there.suppose if we are selecting multiple items in listbox foreach loop is getting only the first selected items from listbox.others are not displaying in table.
♥…ЯҠ…♥ 12-Dec-13 1:10am    
Updated my solution... try it and lemme know
Member 10234093 12-Dec-13 1:12am    
But selecteditems option is not available.
♥…ЯҠ…♥ 12-Dec-13 1:33am    
Updated my solution.... try it
Member 10234093 12-Dec-13 1:40am    
Thank you so much.Now it's working

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