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?
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
andGetString
- Changed variable names; removed hungarian notation
Well, obviously the code was correctly converted - only the variables used have not been declared in VB, so they are also not declared in the C# code. The following should work better:
[WebMethod()]
public List<Notification> GetNotifications()
{
// Have a look at the code conventions - variable names should not start
// with a capital letter...
List<Notification> Notifications = new List<Notification>();
SqlConnection objSQLConnection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"));
SqlCommand objSQLCommand = new SqlCommand("select * from table1 where col1 = @user", objSQLConnection);
objSQLCommand.Parameters.Add("@user", SqlDbType.VarChar, 5).Value = sUser;
objSQLCommand.Connection.Open();
SqlDataReader objSQLDataReader = objSQLCommand.ExecuteReader();
while (objSQLDataReader.Read()) {
Notifications.Add(new Notification(objSQLDataReader("id"), objSQLDataReader("text")));
}
objSQLDataReader.Close();
objSQLCommand.Connection.Close();
return Notifications;
}
Oh, and don't forget to add the respective using
statements!
EDIT
Made another change to the code - Notifications
is not declared anywhere.
The problem is vb code accesses AppSettings
(and arrays in general) like:
AppSettings("keyname")
and c# does arrays as:
AppSettings["keyname"]
The converter sometimes has trouble telling the difference between an array and a function.
DevelopFusion's VB to C# converter (and others) is a free service, provided at no cost to you. It is a guide. Most of the code you've presented is functional, and you only now need to spend a tenth of them time correcting a few small issues with your code (the code above is syntactically correct, by the way... if you've declared the undeclared variables *hint*). Specifically, you need to declare objSQLCommand
and objSQLDataReader
if you haven't already done so elsewhere:
SqlCommand objSQLCommand;
SqlDataReader objSQLDataReader;
精彩评论