Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

So im trying to create a class within my application that initiates a connection to sql and also gets and sets values within a table

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;


namespace ConsoleApplication1
{
    public class EQData
    {
        public string x_id { get; set; }
        public string fullname { get; set; }
        public string email { get; set; }
        public EQData()
        {
            SQLConnect sqlcon = new SQLConnect();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select x_id, fullname, email FROM cas_user_ext WHERE fullname = @fullname",
                                                                                    sqlcon.Connection);
            myCommand.Parameters.AddWithValue("@fullname", "Ivan Lubbe");
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                x_id = myReader["x_id"];
                fullname = myReader["fullname"];
                email = myReader["email"];
            }
            sqlcon.Close();
        }
    }
}


This is my code. Honestly i am a little confused i'm looking at 2x tutorials (sample code) and they are confusing me

http://stackoverflow.com/questions/13410210/creating-a-class-to-interact-with-a-sql-database[^]

http://msdn.microsoft.com/en-us/library/ms173109.aspx[^]

I am getting the error "Cannot implicitly convert 'object' to 'string'

The lines i'm using to reference my class is

C#
 var EQData = new EQData();
// EQData.fullname = myReader["Test User"];
 Console.WriteLine(EQData.fullname);
 //string name = EQData.fullname;


I've commented out a few lines as i tested but i'm obviously not getting this right. And there are really no decent tutorials explaining on how to use a class with get and set to read and write data to sql. Any references and help would be greatly appreciated.
Posted
Comments
Ivan Lubbe 11-Sep-14 8:32am    
Would appreciate it that 'if' someone re-wrote my code to just give me a reference or tried to explain to me in detail on where i am going wrong and why :) i need to understand the code and learn from it.

Your issue is this section of code:

C#
x_id = myReader["x_id"];
              fullname = myReader["fullname"];
              email = myReader["email"];

The Data reader stores the objects as "object" types and you are trying to assign them to strings.

You could do:

C#
x_id = myReader["x_id"].ToString();
              fullname = myReader["fullname"].ToString();
              email = myReader["email"].ToString();


But be aware when you are handling different types you need to do the appropriate conversion.

Such as:

If your ID was in fact an int instead of a string
C#
x_id = Int32.Parse(myReader["x_id"]);
 
Share this answer
 
Hi,
problem should be here:
"c#
x_id = myReader["x_id"];

this part of code will return value as object. You can not set variable which is string object value.
Solution:
"c#
x_id = myReader["x_id"].ToString();
fullname = myReader["fullname"].ToString();
email = myReader["email"].ToString();
 
Share this answer
 

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