Results 1 to 4 of 4

Thread: Cannot Convert Type

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Cannot Convert Type

    Error Message:
    DatabaseClass.cs(102,12): error CS0029: Cannot implicitly convert type 'System.Data.DataSet' to 'System.Data.SqlClient.SqlDataReader'
    Code:
    public DataSet db_query(string SqlStatement, bool isStoredProc)
    	{
    		DataSet dSet = new DataSet();
    
    		try
    		{
    			if(SqlStatement == "")
    			{
    				MessageBox.Show("SQL Statment is empty.");
    			}
    			
    			if(isStoredProc == true)
    			{
    				String strQuery = "EXEC " + SqlStatement.Trim();
    				SqlDataAdapter sAdapter = new SqlDataAdapter(strQuery,myConnection);
    				sAdapter.Fill(dSet,SqlStatement);
    			}
    			else
    			{
    				string strQuery = SqlStatement.Trim();
    				SqlDataAdapter sAdapter = new SqlDataAdapter(strQuery,myConnection);
    				sAdapter.Fill(dSet,SqlStatement);
    			}
    		}
    		catch(Exception exp)
    		{
    			MessageBox.Show("Error fetching records for the query:- " + SqlStatement + ". Possible cause:- " + exp.ToString() );
    		}	
    				
    		return(dSet);
    	}
    
    public static void Main()
    	{
    		string myConnectionString = ("server=localhost;database=pubs;user id=steve;password=jensen;");
    		Database myDB = new Database();
    		SqlDataReader reader;
    		
    		
    		db_open(myConnectionString); 
    		reader = myDB.db_query("SELECT * FROM authors", false);
    		
    		while(reader.Read()) 
    	   {
    		  Console.WriteLine(reader["au_lname"]);
    	   }
    		
    		db_close();
    	}
    Obviously the issue is in reader = myDB.db_query, since db_query returns a DataSet and I am trying to read the returned DataSet with the SqlReader...Any ideas on how to do this???
    Last edited by Memnoch1207; Sep 30th, 2003 at 12:19 PM.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Why would you want to do that? If you are going to read the data with a datareader might as well just return a datareader object.

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Why not this:
    Code:
    public static void Main()	
    {
    	string myConnectionString = ("server=localhost;database=pubs;user id=steve;password=jensen;");
    	Database myDB = new Database();
    	DataSet myDS = new DataSet();
    		
    		
    	db_open(myConnectionString); 
    	myDS = myDB.db_query("SELECT * FROM authors", false);
    	foreach(DataRow dr in myDS.Tables(0).Rows)
    	{
    	       Console.WriteLine(dr["au_lname"]);
    	}
    		
    	db_close();
    }

  4. #4

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    instead of "public DataSet db_query", I just changed it to public "SqlReader db_query"...that way I am not running into the conversion issue...everything seems to be working properly now...It opens the connection, queries the database, returns the records, and closes the connection.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width