how to pass generic list parameter into a method?











up vote
0
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




















  • 1




    Always include the exact error message, not just a mention about one
    – Sami Kuhmonen
    Nov 10 at 9:39















up vote
0
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




















  • 1




    Always include the exact error message, not just a mention about one
    – Sami Kuhmonen
    Nov 10 at 9:39













up vote
0
down vote

favorite









up vote
0
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















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















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 10:41









quant

1,59211526




1,59211526










asked Nov 10 at 9:34









Sachin Kumar

42




42








  • 1




    Always include the exact error message, not just a mention about one
    – Sami Kuhmonen
    Nov 10 at 9:39














  • 1




    Always include the exact error message, not just a mention about one
    – Sami Kuhmonen
    Nov 10 at 9:39








1




1




Always include the exact error message, not just a mention about one
– Sami Kuhmonen
Nov 10 at 9:39




Always include the exact error message, not just a mention about one
– Sami Kuhmonen
Nov 10 at 9:39












1 Answer
1






active

oldest

votes

















up vote
7
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
    Nov 10 at 12:38










  • I would personally also do a null-check on the argument.
    – Tom
    Nov 10 at 13:30










  • @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
    Nov 10 at 19:06











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',
autoActivateHeartbeat: false,
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
});


}
});














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















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
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
    Nov 10 at 12:38










  • I would personally also do a null-check on the argument.
    – Tom
    Nov 10 at 13:30










  • @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
    Nov 10 at 19:06















up vote
7
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
    Nov 10 at 12:38










  • I would personally also do a null-check on the argument.
    – Tom
    Nov 10 at 13:30










  • @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
    Nov 10 at 19:06













up vote
7
down vote










up vote
7
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 Nov 10 at 9:38









Jon Skeet

1074k67478648396




1074k67478648396












  • 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
    Nov 10 at 12:38










  • I would personally also do a null-check on the argument.
    – Tom
    Nov 10 at 13:30










  • @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
    Nov 10 at 19:06


















  • 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
    Nov 10 at 12:38










  • I would personally also do a null-check on the argument.
    – Tom
    Nov 10 at 13:30










  • @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
    Nov 10 at 19:06
















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
Nov 10 at 12:38




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
Nov 10 at 12:38












I would personally also do a null-check on the argument.
– Tom
Nov 10 at 13:30




I would personally also do a null-check on the argument.
– Tom
Nov 10 at 13:30












@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
Nov 10 at 19:06




@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
Nov 10 at 19:06


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




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















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini