locked
System.Data.SqlClient.SqlException: 'Procedure or function labelNames has too many arguments specified.' RRS feed

  • Question

  • Hi

    I am new to c# -wpf and I apologize for dumb question in advance 

                                                             

    class FindControlsInAControl    can be found below

            public  IEnumerable<T> FindallChildren<T>(DependencyObject obj) where T : DependencyObject
            {
                if (obj != null)
                {
                    if (obj is T)
                        yield return obj as T;

                    foreach (DependencyObject child in LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>())
                        foreach (T c in FindallChildren<T>(child))
                            yield return c;
                }
            }

    part of a class to fill content of labels in a form (for different languages)

           public void labelsTexts( Window LoadedWin , int lanCode)
            {
                string ProcedureName = "labelNames";
                IEnumerable labelsInForm;
                FindControlsInAControl findLabelsTexts = new FindControlsInAControl();
                labelsInForm= findLabelsTexts.FindallChildren<_Label>(LoadedWin);


                using (var connection = new SqlConnection(ConnString))
                {
                    connection.Open();

                    using (var command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = ProcedureName;
                        SqlDataAdapter sda = new SqlDataAdapter(command);
                        DataTable dt = new DataTable();
                        int numofLabels = labelsInForm.Cast<_Label>().Count();
                        for (int i = 0; i < numofLabels - 1; i++)
                        {

                            var j = labelsInForm.Cast<_Label>().ElementAt(i);
                            command.Parameters.AddWithValue("@LanCode", lanCode);
                            command.Parameters.AddWithValue("@labelCode", j.lblCode);
                            command.ExecuteNonQuery();

                            sda.Fill(dt);
                            ((_Label)j).Content = dt.Rows[0].ItemArray[0].ToString();

                        }
                        connection.Close();
                    }

                }
            }

    sql procedure can be found below

    ALTER PROCEDURE labelNames
    -- Add the parameters for the stored procedure here
    @LanCode int = 1, 
    @labelCode int = 1
    AS
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

        -- Insert statements for procedure here
    SELECT labelText from [language-labels]   where languageCode= @LanCode  and labelCode= @labelCode
    END

    I checked couple of times input variables to sql procedure (2 var) and variables in my code (2 var) but I get the error

    is there a better way to have texts of controls in a software according to defined language by user?

    Best Regards

    Saturday, May 12, 2018 3:05 PM

Answers

  • I think you just need to clear the parameters in the loop:

    for (int i = 0; i < numofLabels - 1; i++)
    {
        var j = labelsInForm.Cast<_Label>().ElementAt(i);
        command.Parameters.Clear();
        command.Parameters.AddWithValue("@LanCode", lanCode);
        command.Parameters.AddWithValue("@labelCode", j.lblCode);
        command.ExecuteNonQuery();
    
        // etc...
    }


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by 3ib Saturday, May 12, 2018 6:42 PM
    Saturday, May 12, 2018 4:54 PM

All replies

  • Try re-organising the program, moving the ‘for’ line:

    . . .
    connection.Open();
    for( int i = 0; i < numofLabels - 1; i++)
    {
       using( var command = connection.CreateCommand())
       {
          . . .
       }
    }
    connection.Close();
    . . .


    Saturday, May 12, 2018 4:46 PM
  • I think you just need to clear the parameters in the loop:

    for (int i = 0; i < numofLabels - 1; i++)
    {
        var j = labelsInForm.Cast<_Label>().ElementAt(i);
        command.Parameters.Clear();
        command.Parameters.AddWithValue("@LanCode", lanCode);
        command.Parameters.AddWithValue("@labelCode", j.lblCode);
        command.ExecuteNonQuery();
    
        // etc...
    }


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by 3ib Saturday, May 12, 2018 6:42 PM
    Saturday, May 12, 2018 4:54 PM
  • It worked. thank you very much
    Saturday, May 12, 2018 6:42 PM
  • Awesome! You're welcome! Glad I could help!  =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, May 12, 2018 10:15 PM