Search code examples
c#.net.net-3.5asmxvb.net-to-c#

Getting CS1502: The best overloaded method match for <some_method> has some invalid arguments


I currently have the following code:

<%@ WebService Language="C#" Class="Notifications" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Script;
using System.Web.Script.Services;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

public class Notification
{
    public int id;
    public string text;

    public Notification(int m_id, string m_text)
    {
        id = m_id;
        text = m_text;
    }

    public Notification() {}
}

[System.Web.Script.Services.ScriptService]
public class Notifications : System.Web.Services.WebService {
    List<Notification> Notification = new List<Notification>();

    [WebMethod()]
    public List<Notification> GetNotifications()
    {
        var notifications = new List<Notification>();

        using (SqlConnection objSQLConnection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"]))
        {
            using (SqlCommand objSQLCommand = new SqlCommand("select * from table1 where col1 = @user", objSQLConnection))
            {
                objSQLCommand.Parameters.Add("@user", SqlDbType.VarChar, 5).Value = "abc";

                objSQLConnection.Open();
                using (SqlDataReader objSQLDataReader = objSQLCommand.ExecuteReader())
                {

                    while (objSQLDataReader.Read())
                    {
                        notifications.Add(new Notification(objSQLDataReader["id"], objSQLDataReader["text"]));
                    }

                }
            }
        }

        return notifications;
    }

}

Which is giving me the following error:

Compiler Error Message: CS1502: The best overloaded method match for 'Notification.Notification(int, string)' has some invalid arguments

On the line:

notifications.Add(new Notification(objSQLDataReader["id"], objSQLDataReader["text"]));

Anyone know why this is happening?


Solution

  • There seems to be some missing from the function, you never create the Notifications object for example. If you use implicitly created variables in VB, that will most likely not work to convert into C# (and is not encouraged in VB either). Also, I would encourage you to use Using blocks to make sure that everything is closed and disposed properly:

    <WebMethod()> _
    Public Function GetNotifications() As List(Of Notification)
      Dim notifications As New List(Of Notification)()
    
      Using connection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))
        Using command = New SqlCommand("select * from table1 where col1 = @user", connection)
          command.Parameters.Add("@user", SqlDbType.VarChar, 5).Value = sUser;
    
          connection.Open()
          Using reader = command.ExecuteReader()
    
            Dim idIndex As Integer = reader.GetOrdinal("id")
            Dim textIndex As Integer = reader.GetOrdinal("text")
    
            While reader.Read()
              notifications.Add(New Notification(reader.GetInt32(idIndex), reader.GetString(textIndex)))
            End While
    
          End Using
        End Using
      End Using
    
      Return notifications
    End Function
    

    Now it should (ideally) convert into:

    [WebMethod()]
    public List<Notification> GetNotifications() {
      var notifications = new List<Notification>();
    
      using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"])) {
        using (SqlCommand command = new SqlCommand("select * from table1 where col1 = @user", connection)) {
          command.Parameters.Add("@user", SqlDbType.VarChar, 5).Value = sUser;
    
          connection.Open();
          using (SqlDataReader reader = command.ExecuteReader()) {
    
            int idIndex = reader.GetOrdinal("id")
            int textIndex = reader.GetOrdinal("text")
    
            while (reader.Read()) {
              notifications.Add(new Notification(reader.GetInt32(idIndex), reader.GetString(textIndex)));
            }
    
          }
        }
      }
    
      return notifications;
    }
    

    Edit:

    Changes done:

    • Changed to brackets when accessing AppSettings
    • Changed to brackets when accessinfg objSQLDataReader
    • Changed reader code to use GetOrdinal, GetInt32 and GetString
    • Changed variable names; removed hungarian notation