how to pass generic list parameter into a method?











up vote
1
down vote

favorite












I am getting phone contacts into a list<> and saving it in a database.
Below is my code.



This is my method to get the contacts-List



protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
try {
SetContentView(Resource.Layout.Main);
TextView txtcount = this.FindViewById<TextView>(Resource.Id.textView1);

List<PersonContact> a1 = GetPhoneContacts();

Phone gp = new Phone();

gp.insertContact(a1);
} catch (System.Exception ex) {
alert(ex.Message);
}
}


Via the following method I am trying to store contacts in database



[WebMethod]
public string insertContact<T>(List<PersonContact> a) {
OpenConnection();
if (a.Count > 0) {
for (int i = 0; i < a.Count; i++) {
string str = "insert into phone_contact (FirstName,LastName,PhoneNumber)values('" + a[i].FirstName + "','" + a[i].LastName + "','" + a[i].PhoneNumber + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
}
return "1";
} else {
return "1";
}
}

public class PersonContact {
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}


I am getting an error while passing parameter



 gp.insertContact(a1);









share|improve this question









New contributor




Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Always include the exact error message, not just a mention about one
    – Sami Kuhmonen
    16 hours ago















up vote
1
down vote

favorite












I am getting phone contacts into a list<> and saving it in a database.
Below is my code.



This is my method to get the contacts-List



protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
try {
SetContentView(Resource.Layout.Main);
TextView txtcount = this.FindViewById<TextView>(Resource.Id.textView1);

List<PersonContact> a1 = GetPhoneContacts();

Phone gp = new Phone();

gp.insertContact(a1);
} catch (System.Exception ex) {
alert(ex.Message);
}
}


Via the following method I am trying to store contacts in database



[WebMethod]
public string insertContact<T>(List<PersonContact> a) {
OpenConnection();
if (a.Count > 0) {
for (int i = 0; i < a.Count; i++) {
string str = "insert into phone_contact (FirstName,LastName,PhoneNumber)values('" + a[i].FirstName + "','" + a[i].LastName + "','" + a[i].PhoneNumber + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
}
return "1";
} else {
return "1";
}
}

public class PersonContact {
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}


I am getting an error while passing parameter



 gp.insertContact(a1);









share|improve this question









New contributor




Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Always include the exact error message, not just a mention about one
    – Sami Kuhmonen
    16 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am getting phone contacts into a list<> and saving it in a database.
Below is my code.



This is my method to get the contacts-List



protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
try {
SetContentView(Resource.Layout.Main);
TextView txtcount = this.FindViewById<TextView>(Resource.Id.textView1);

List<PersonContact> a1 = GetPhoneContacts();

Phone gp = new Phone();

gp.insertContact(a1);
} catch (System.Exception ex) {
alert(ex.Message);
}
}


Via the following method I am trying to store contacts in database



[WebMethod]
public string insertContact<T>(List<PersonContact> a) {
OpenConnection();
if (a.Count > 0) {
for (int i = 0; i < a.Count; i++) {
string str = "insert into phone_contact (FirstName,LastName,PhoneNumber)values('" + a[i].FirstName + "','" + a[i].LastName + "','" + a[i].PhoneNumber + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
}
return "1";
} else {
return "1";
}
}

public class PersonContact {
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}


I am getting an error while passing parameter



 gp.insertContact(a1);









share|improve this question









New contributor




Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am getting phone contacts into a list<> and saving it in a database.
Below is my code.



This is my method to get the contacts-List



protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
try {
SetContentView(Resource.Layout.Main);
TextView txtcount = this.FindViewById<TextView>(Resource.Id.textView1);

List<PersonContact> a1 = GetPhoneContacts();

Phone gp = new Phone();

gp.insertContact(a1);
} catch (System.Exception ex) {
alert(ex.Message);
}
}


Via the following method I am trying to store contacts in database



[WebMethod]
public string insertContact<T>(List<PersonContact> a) {
OpenConnection();
if (a.Count > 0) {
for (int i = 0; i < a.Count; i++) {
string str = "insert into phone_contact (FirstName,LastName,PhoneNumber)values('" + a[i].FirstName + "','" + a[i].LastName + "','" + a[i].PhoneNumber + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
}
return "1";
} else {
return "1";
}
}

public class PersonContact {
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}


I am getting an error while passing parameter



 gp.insertContact(a1);






c# asp.net xamarin.forms






share|improve this question









New contributor




Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 15 hours ago









quant

8831824




8831824






New contributor




Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 17 hours ago









Sachin Kumar

62




62




New contributor




Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Sachin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Always include the exact error message, not just a mention about one
    – Sami Kuhmonen
    16 hours ago














  • 1




    Always include the exact error message, not just a mention about one
    – Sami Kuhmonen
    16 hours ago








1




1




Always include the exact error message, not just a mention about one
– Sami Kuhmonen
16 hours ago




Always include the exact error message, not just a mention about one
– Sami Kuhmonen
16 hours ago












1 Answer
1






active

oldest

votes

















up vote
6
down vote













Your method is generic, as it introduces a new type parameter T. That's what the <T> at the end of the method name means.



However, you don't use T anywhere - so just make it a non-generic method:



public string InsertContact(List<PersonContact> a)


At the same time, I would very strongly urge you to change the way you're doing database access: it's currently vulnerable to SQL injection attacks. Instead, you should use parameterized SQL: have one parameter for each of FirstName, LastName and PhoneNumber.



You're also returning "1" regardless of the input. Your method could be written more simply as:



// Consider renaming to InsertContacts, as it's not just dealing with a single
// contact
public string InsertContact(List<PersonContact> contacts)
{
// You should almost certainly use a using statement here, to
// dispose of the connection afterwards
OpenConnection();
foreach (var contact in contacts)
{
// Insert the contact. Use a using statement for the SqlCommand too.
}
return "1";
}


That's assuming you need the value returned at all - if you're always returning the same value, why not just make it a void method?






share|improve this answer





















  • You have valid points but I do not think you have answered the question. Instead you did a code review. Sami is correct in asking for the error.
    – Aldert
    13 hours ago










  • I would personally also do a null-check on the argument.
    – Tom
    13 hours ago










  • @Aldert: I believe I have answered the question, because I strongly expect that that error would be that the type argument can't be inferred. I agree that the question would be better if it included the error message, but I'm pretty confident about what the problem is, and that my answer fixes it.
    – Jon Skeet
    7 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Sachin Kumar is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237656%2fhow-to-pass-generic-list-parameter-into-a-method%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote













Your method is generic, as it introduces a new type parameter T. That's what the <T> at the end of the method name means.



However, you don't use T anywhere - so just make it a non-generic method:



public string InsertContact(List<PersonContact> a)


At the same time, I would very strongly urge you to change the way you're doing database access: it's currently vulnerable to SQL injection attacks. Instead, you should use parameterized SQL: have one parameter for each of FirstName, LastName and PhoneNumber.



You're also returning "1" regardless of the input. Your method could be written more simply as:



// Consider renaming to InsertContacts, as it's not just dealing with a single
// contact
public string InsertContact(List<PersonContact> contacts)
{
// You should almost certainly use a using statement here, to
// dispose of the connection afterwards
OpenConnection();
foreach (var contact in contacts)
{
// Insert the contact. Use a using statement for the SqlCommand too.
}
return "1";
}


That's assuming you need the value returned at all - if you're always returning the same value, why not just make it a void method?






share|improve this answer





















  • You have valid points but I do not think you have answered the question. Instead you did a code review. Sami is correct in asking for the error.
    – Aldert
    13 hours ago










  • I would personally also do a null-check on the argument.
    – Tom
    13 hours ago










  • @Aldert: I believe I have answered the question, because I strongly expect that that error would be that the type argument can't be inferred. I agree that the question would be better if it included the error message, but I'm pretty confident about what the problem is, and that my answer fixes it.
    – Jon Skeet
    7 hours ago















up vote
6
down vote













Your method is generic, as it introduces a new type parameter T. That's what the <T> at the end of the method name means.



However, you don't use T anywhere - so just make it a non-generic method:



public string InsertContact(List<PersonContact> a)


At the same time, I would very strongly urge you to change the way you're doing database access: it's currently vulnerable to SQL injection attacks. Instead, you should use parameterized SQL: have one parameter for each of FirstName, LastName and PhoneNumber.



You're also returning "1" regardless of the input. Your method could be written more simply as:



// Consider renaming to InsertContacts, as it's not just dealing with a single
// contact
public string InsertContact(List<PersonContact> contacts)
{
// You should almost certainly use a using statement here, to
// dispose of the connection afterwards
OpenConnection();
foreach (var contact in contacts)
{
// Insert the contact. Use a using statement for the SqlCommand too.
}
return "1";
}


That's assuming you need the value returned at all - if you're always returning the same value, why not just make it a void method?






share|improve this answer





















  • You have valid points but I do not think you have answered the question. Instead you did a code review. Sami is correct in asking for the error.
    – Aldert
    13 hours ago










  • I would personally also do a null-check on the argument.
    – Tom
    13 hours ago










  • @Aldert: I believe I have answered the question, because I strongly expect that that error would be that the type argument can't be inferred. I agree that the question would be better if it included the error message, but I'm pretty confident about what the problem is, and that my answer fixes it.
    – Jon Skeet
    7 hours ago













up vote
6
down vote










up vote
6
down vote









Your method is generic, as it introduces a new type parameter T. That's what the <T> at the end of the method name means.



However, you don't use T anywhere - so just make it a non-generic method:



public string InsertContact(List<PersonContact> a)


At the same time, I would very strongly urge you to change the way you're doing database access: it's currently vulnerable to SQL injection attacks. Instead, you should use parameterized SQL: have one parameter for each of FirstName, LastName and PhoneNumber.



You're also returning "1" regardless of the input. Your method could be written more simply as:



// Consider renaming to InsertContacts, as it's not just dealing with a single
// contact
public string InsertContact(List<PersonContact> contacts)
{
// You should almost certainly use a using statement here, to
// dispose of the connection afterwards
OpenConnection();
foreach (var contact in contacts)
{
// Insert the contact. Use a using statement for the SqlCommand too.
}
return "1";
}


That's assuming you need the value returned at all - if you're always returning the same value, why not just make it a void method?






share|improve this answer












Your method is generic, as it introduces a new type parameter T. That's what the <T> at the end of the method name means.



However, you don't use T anywhere - so just make it a non-generic method:



public string InsertContact(List<PersonContact> a)


At the same time, I would very strongly urge you to change the way you're doing database access: it's currently vulnerable to SQL injection attacks. Instead, you should use parameterized SQL: have one parameter for each of FirstName, LastName and PhoneNumber.



You're also returning "1" regardless of the input. Your method could be written more simply as:



// Consider renaming to InsertContacts, as it's not just dealing with a single
// contact
public string InsertContact(List<PersonContact> contacts)
{
// You should almost certainly use a using statement here, to
// dispose of the connection afterwards
OpenConnection();
foreach (var contact in contacts)
{
// Insert the contact. Use a using statement for the SqlCommand too.
}
return "1";
}


That's assuming you need the value returned at all - if you're always returning the same value, why not just make it a void method?







share|improve this answer












share|improve this answer



share|improve this answer










answered 16 hours ago









Jon Skeet

1066k66578198357




1066k66578198357












  • You have valid points but I do not think you have answered the question. Instead you did a code review. Sami is correct in asking for the error.
    – Aldert
    13 hours ago










  • I would personally also do a null-check on the argument.
    – Tom
    13 hours ago










  • @Aldert: I believe I have answered the question, because I strongly expect that that error would be that the type argument can't be inferred. I agree that the question would be better if it included the error message, but I'm pretty confident about what the problem is, and that my answer fixes it.
    – Jon Skeet
    7 hours ago


















  • You have valid points but I do not think you have answered the question. Instead you did a code review. Sami is correct in asking for the error.
    – Aldert
    13 hours ago










  • I would personally also do a null-check on the argument.
    – Tom
    13 hours ago










  • @Aldert: I believe I have answered the question, because I strongly expect that that error would be that the type argument can't be inferred. I agree that the question would be better if it included the error message, but I'm pretty confident about what the problem is, and that my answer fixes it.
    – Jon Skeet
    7 hours ago
















You have valid points but I do not think you have answered the question. Instead you did a code review. Sami is correct in asking for the error.
– Aldert
13 hours ago




You have valid points but I do not think you have answered the question. Instead you did a code review. Sami is correct in asking for the error.
– Aldert
13 hours ago












I would personally also do a null-check on the argument.
– Tom
13 hours ago




I would personally also do a null-check on the argument.
– Tom
13 hours ago












@Aldert: I believe I have answered the question, because I strongly expect that that error would be that the type argument can't be inferred. I agree that the question would be better if it included the error message, but I'm pretty confident about what the problem is, and that my answer fixes it.
– Jon Skeet
7 hours ago




@Aldert: I believe I have answered the question, because I strongly expect that that error would be that the type argument can't be inferred. I agree that the question would be better if it included the error message, but I'm pretty confident about what the problem is, and that my answer fixes it.
– Jon Skeet
7 hours ago










Sachin Kumar is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Sachin Kumar is a new contributor. Be nice, and check out our Code of Conduct.













Sachin Kumar is a new contributor. Be nice, and check out our Code of Conduct.












Sachin Kumar is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237656%2fhow-to-pass-generic-list-parameter-into-a-method%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly