Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. method parameters error

method parameters error

Scheduled Pinned Locked Moved C#
help
16 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Abhinav S

    Remove the return from inside the while loop and place it outside. Return a collection of ZoneCodes instead of doing what you have done above.

    SqlConnection con1 = new SqlConnection();
    con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
    string sql1 = "select " + colu + " from " + tbl + " where " + cond1 + " ='" + value1 + "' and " + cond2 + " = " + value2 + "";
    SqlCommand cmd1 = new SqlCommand(sql1, con1);
    con1.Open();
    SqlDataReader dr = cmd1.ExecuteReader();
    List objList = new List();
    while (dr.Read())
    {
    objList.Add(dr["ZoneCode"].ToString(););
    }
    return objList;
    con1.Close();

    You method signature will be

    public List ReturnString(string tbl, string colu, string cond1, string value1, string cond2, string value2)

    Sorry. I cannot include this code in the

    tags. The generics part is getting messed up.

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #5

    A slight correction to this code moves the line con1.Close before the return - it's unreachable code, but you could always set ExecuteReader with the parameter CommandBehavior.CloseConnection to shut the connection automatically. I'd also suggest wrapping the code in using blocks to trigger disposal of the objects.

    using (SqlConnection con1 = new SqlConnection())
    {
    con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
    string sql1 = "select " + colu + " from " + tbl + " where " + cond1 + " ='" + value1 + "' and " + cond2 + " = " + value2 + "";
    using (SqlCommand cmd1 = new SqlCommand(sql1, con1))
    {
    con1.Open();
    using (SqlDataReader dr = cmd1.ExecuteReader())
    {
    List<string> objList = new List<string>();
    while (dr.Read())
    {
    objList.Add(dr["ZoneCode"].ToString(););
    }
    return objList;
    }
    }
    }

    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

    My blog | My articles | MoXAML PowerToys | Onyx

    A A 2 Replies Last reply
    0
    • P Pete OHanlon

      A slight correction to this code moves the line con1.Close before the return - it's unreachable code, but you could always set ExecuteReader with the parameter CommandBehavior.CloseConnection to shut the connection automatically. I'd also suggest wrapping the code in using blocks to trigger disposal of the objects.

      using (SqlConnection con1 = new SqlConnection())
      {
      con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
      string sql1 = "select " + colu + " from " + tbl + " where " + cond1 + " ='" + value1 + "' and " + cond2 + " = " + value2 + "";
      using (SqlCommand cmd1 = new SqlCommand(sql1, con1))
      {
      con1.Open();
      using (SqlDataReader dr = cmd1.ExecuteReader())
      {
      List<string> objList = new List<string>();
      while (dr.Read())
      {
      objList.Add(dr["ZoneCode"].ToString(););
      }
      return objList;
      }
      }
      }

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #6

      Thanks - I completely missed the close() statement. :)

      P 1 Reply Last reply
      0
      • A Abhinav S

        Thanks - I completely missed the close() statement. :)

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #7

        No problems. It's the joy of coding directly in the html textbox. :-D

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        A 1 Reply Last reply
        0
        • P Pete OHanlon

          No problems. It's the joy of coding directly in the html textbox. :-D

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #8

          Pete O'Hanlon wrote:

          No problems. It's the joy of coding directly in the html textbox

          I messed up the "dry run". :)

          1 Reply Last reply
          0
          • P Pete OHanlon

            A slight correction to this code moves the line con1.Close before the return - it's unreachable code, but you could always set ExecuteReader with the parameter CommandBehavior.CloseConnection to shut the connection automatically. I'd also suggest wrapping the code in using blocks to trigger disposal of the objects.

            using (SqlConnection con1 = new SqlConnection())
            {
            con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
            string sql1 = "select " + colu + " from " + tbl + " where " + cond1 + " ='" + value1 + "' and " + cond2 + " = " + value2 + "";
            using (SqlCommand cmd1 = new SqlCommand(sql1, con1))
            {
            con1.Open();
            using (SqlDataReader dr = cmd1.ExecuteReader())
            {
            List<string> objList = new List<string>();
            while (dr.Read())
            {
            objList.Add(dr["ZoneCode"].ToString(););
            }
            return objList;
            }
            }
            }

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            A Offline
            A Offline
            Abdul Rhman Alsri
            wrote on last edited by
            #9

            List<string> ZoneCode = ReturnZoneCode("select ZoneCode from AdZone where ID = 20 and AccountID=530");
            TextBox1.Text = Convert.ToString(ZoneCode.ToString());

            In the code above I called the function(Note: I edited the signature) and I want to bind the returned List into a textbox. Problem what I have that the Textbox is not bounded the the value returned from the call, but it had the following: "System.Collections.Generic.List`1[System.String]"

            P 1 Reply Last reply
            0
            • A Abdul Rhman Alsri

              List<string> ZoneCode = ReturnZoneCode("select ZoneCode from AdZone where ID = 20 and AccountID=530");
              TextBox1.Text = Convert.ToString(ZoneCode.ToString());

              In the code above I called the function(Note: I edited the signature) and I want to bind the returned List into a textbox. Problem what I have that the Textbox is not bounded the the value returned from the call, but it had the following: "System.Collections.Generic.List`1[System.String]"

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #10

              That's because ToString returns this type of information on a list. If you want to display the values, you need to actually bind to an individual item.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              A 1 Reply Last reply
              0
              • P Pete OHanlon

                That's because ToString returns this type of information on a list. If you want to display the values, you need to actually bind to an individual item.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                A Offline
                A Offline
                Abdul Rhman Alsri
                wrote on last edited by
                #11

                how? give me code in c#

                P 1 Reply Last reply
                0
                • A Abdul Rhman Alsri

                  how? give me code in c#

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #12

                  Well, suppose you wanted to display a list of all of the items in a textbox, you could use:

                  private void BindZones(List<string> zones)
                  {
                  StringBuilder sb = new StringBuilder();
                  foreach (string item in zones)
                  {
                  sb.AppendFormat("{0}{1}", item, Environment.NewLine);
                  }
                  TextBox1.Text = sb.ToString();
                  }

                  To be honest though - you would be better off displaying this in a ListBox rather than a Textbox.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  A 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Well, suppose you wanted to display a list of all of the items in a textbox, you could use:

                    private void BindZones(List<string> zones)
                    {
                    StringBuilder sb = new StringBuilder();
                    foreach (string item in zones)
                    {
                    sb.AppendFormat("{0}{1}", item, Environment.NewLine);
                    }
                    TextBox1.Text = sb.ToString();
                    }

                    To be honest though - you would be better off displaying this in a ListBox rather than a Textbox.

                    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                    My blog | My articles | MoXAML PowerToys | Onyx

                    A Offline
                    A Offline
                    Abdul Rhman Alsri
                    wrote on last edited by
                    #13

                    I got a null value. please help me to return a string and not

                    List

                    I'd like to change to the previous one... :)

                    P 1 Reply Last reply
                    0
                    • A Abdul Rhman Alsri

                      I got a null value. please help me to return a string and not

                      List

                      I'd like to change to the previous one... :)

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #14

                      Your code is missing - please show it.

                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                      My blog | My articles | MoXAML PowerToys | Onyx

                      A 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        Your code is missing - please show it.

                        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                        My blog | My articles | MoXAML PowerToys | Onyx

                        A Offline
                        A Offline
                        Abdul Rhman Alsri
                        wrote on last edited by
                        #15

                        I want the function to return a string and not a list string Function:

                        public List<string> ReturnZoneCode(string sql1)
                        {
                        using (SqlConnection con1 = new SqlConnection())
                        {
                        con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
                        //string sql1 = "select " + colu + " from " + tbl + " where " + cond1 + " ='" + value1 + "' and " + cond2 + " = " + value2 + "";
                        using (SqlCommand cmd1 = new SqlCommand(sql1, con1))
                        {
                        con1.Open();
                        using (SqlDataReader dr = cmd1.ExecuteReader())
                        {
                        List<string> objList = new List<string>();
                        while (dr.Read())
                        {
                        objList.Add(dr["ZoneCode"].ToString());
                        }
                        return objList;
                        }
                        }
                        }

                        P 1 Reply Last reply
                        0
                        • A Abdul Rhman Alsri

                          I want the function to return a string and not a list string Function:

                          public List<string> ReturnZoneCode(string sql1)
                          {
                          using (SqlConnection con1 = new SqlConnection())
                          {
                          con1.ConnectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ToString();
                          //string sql1 = "select " + colu + " from " + tbl + " where " + cond1 + " ='" + value1 + "' and " + cond2 + " = " + value2 + "";
                          using (SqlCommand cmd1 = new SqlCommand(sql1, con1))
                          {
                          con1.Open();
                          using (SqlDataReader dr = cmd1.ExecuteReader())
                          {
                          List<string> objList = new List<string>();
                          while (dr.Read())
                          {
                          objList.Add(dr["ZoneCode"].ToString());
                          }
                          return objList;
                          }
                          }
                          }

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #16

                          Use a StringBuilder instead of a list.

                          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                          My blog | My articles | MoXAML PowerToys | Onyx

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups