Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i using a class file and i need help how to assine each value to them
like
C#
id =some value
name=some values
  var customer1 = new List<customer>();
            //   customer output=null;
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from SalesMan where salesmanname=@salesmanname and code=@usercode", con);
            cmd.Parameters.AddWithValue("@salesmanname", salesmanname);
            cmd.Parameters.AddWithValue("@usercode", usercode);
            SqlDataReader customer11 = cmd.ExecuteReader();
            while (customer11.Read())
            {
                customer c1 = new customer();
                c1.id = Convert.ToInt32(customer11.GetSqlValue(0).ToString());
                c1.companyrefid = Guid.Parse(customer11.GetSqlValue(0).ToString());
               
                customer1.Add(c1);
            }
       //     return customer1;</customer>
Posted
Updated 31-May-14 0:31am
v3
Comments
Maciej Los 31-May-14 6:16am    
?
[no name] 31-May-14 6:18am    
Just like that: id = some_value; That is how you assign values in C#. What does some class file or SQL or columns have to do with your "question"?
Nandhini Devi 31-May-14 6:23am    
var customer1 = new List<customer>();
// customer output=null;
con.Open();
SqlCommand cmd = new SqlCommand("Select * from SalesMan where salesmanname=@salesmanname and code=@usercode", con);
cmd.Parameters.AddWithValue("@salesmanname", salesmanname);
cmd.Parameters.AddWithValue("@usercode", usercode);
SqlDataReader customer11 = cmd.ExecuteReader();
while (customer11.Read())
{
customer c1 = new customer();
c1.id = Convert.ToInt32(customer11.GetSqlValue(0).ToString());
c1.companyrefid = Guid.Parse(customer11.GetSqlValue(0).ToString());

customer1.Add(c1);
}
// return customer1;
[no name] 31-May-14 6:34am    
And? You do realize that we can't read your mind right? We don't have access to your database so there is no possible way we can know what it is that you think is not working with your code unless you tell us. First thing that strikes me as odd about your code is that you are getting the same value from the same column in your database and trying to do 2 different things with it. If you are only wanting 2 columns from your database, why are you selecting *? Doesn't that seem sort of wasteful to you? Your code won't even compile anyway, so you would need to fix those errors first.
Nandhini Devi 31-May-14 6:40am    
i am getting values but dnt know how to store in list .i want those values for further process.how to do that ?

1 solution

Try this, it's a little clearer:
C#
List<customer> customers = new List<customer>();
con.Open();
using (SqlCommand cmd = new SqlCommand("Select Id, CompanyRefId from SalesMan where salesmanname=@salesmanname and code=@usercode", con))
    {
    cmd.Parameters.AddWithValue("@salesmanname", salesmanname);
    cmd.Parameters.AddWithValue("@usercode", usercode);
    using (SqlDataReader reader = cmd.ExecuteReader())
        {
        while (reader.Read())
            {
                customer c1 = new customer();
                c1.id = (int) reader["Id"];
                c1.companyrefid = (Guid) reader["CompanyRefId"];
                customers.Add(c1);
            }
        }
Provided your SQL table contains the two columns "Id" and "CompanyRefId", and they are INT and UNIQUEID respectively, you need no other conversion.
 
Share this answer
 
Comments
Nandhini Devi 31-May-14 6:53am    
i want to return those values in list but
List<customer> customers = new List<customer>();
con.Open();
using (SqlCommand cmd = new SqlCommand("Select Id, CompanyRefId from SalesMan where salesmanname=@salesmanname and code=@usercode", con))
{
cmd.Parameters.AddWithValue("@salesmanname", salesmanname);
cmd.Parameters.AddWithValue("@usercode", usercode);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
customer c1 = new customer();
c1.id = (int) reader["Id"];
c1.companyrefid = (Guid) reader["CompanyRefId"];
customers.Add(c1);
}
return customers;// when i wrote this only id getting return .but companyrefid is added in c1 how to get that value also ?
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900