OS: Windows XP Pro
Languages: C#, SQL Server, ASP.NET
Program: Visual Studio .NET 2003
Hi,
I'm having an error compiling my code, I get the error:
Cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'
on Line 51. Can anyone help me interpret this error and point me in the right direction to fix this Thanks in advanced!
The code is below:
Line 1: using System;
Line 2: using System.Data;
Line 3: using System.Data.SqlClient;
Line 4: using System.Web;
Line 5: using System.Web.UI;
Line 6: using System.Web.UI.WebControls;
Line 7: using System.Web.UI.HtmlControls;
Line 8: using System.IO;
Line 9:
Line 10:
Line 11: namespace songDL
Line 12: {
Line 13:
Line 14: public class Download : System.Web.UI.Page
Line 15: {
Line 16:
Line 17: // This call is required by the ASP.NET Web Form Designer.
Line 18: override protected void OnInit(EventArgs e)
Line 19: {
Line 20: InitializeComponent();
Line 21: base.OnInit(e);
Line 22: }
Line 23:
Line 24: private void InitializeComponent()
Line 25: {
Line 26: this.Load += new System.EventHandler(this.Page_Load);
Line 27: }
Line 28:
Line 29: // Instructions to initialize the page when it loads here...
Line 30: private void Page_Load(object sender, System.EventArgs e)
Line 31: {
Line 32: // connect to database using the following credentials
Line 33: string dataString = " my connection string";
Line 34:
Line 35: //use this SQL query to get the appropriate file
Line 36: //string SQL = "SELECT Song, SongSize, SongType FROM SongDetails WHERE SongID=" + Request.QueryString["SongID"];
Line 37:
Line 38: string SQL = "SELECT Song, SongSize, SongType FROM SongDetails WHERE SongID=" + Request.QueryString["SongID"];
Line 39:
Line 40: //creates the connection object to database
Line 41: SqlConnection dataConnection = new SqlConnection(dataString);
Line 42:
Line 43: // creates the sql command to run in the DB
Line 44: SqlCommand dataExecute = new SqlCommand(SQL, dataConnection);
Line 45:
Line 46: // opens the connection
Line 47: dataConnection.Open();
Line 48:
Line 49: // creates reader for database results
Line 50: //SqlDataReader contentDataReader = dataExecute.ExecuteReader();
Line 51: SqlDataReader contentDataReader = dataExecute.ExecuteNonQuery();
Line 52:
Line 53: // reads the database
Line 54: contentDataReader.Read();
Line 55:
Line 56: // clear the buffer
Line 57: Response.Clear();
Line 58:
Line 59: // set content type - tell browser what type of file is being sent from the database
Line 60: Response.ContentType = (string)contentDataReader["SongType"];
Line 61:
Line 62: // stream the audio into the broswer and start playing
Line 63: Response.OutputStream.Write((byte[])contentDataReader["Song"], 0, (int)contentDataReader["SongSize"]);
Line 64:
Line 65: // close connection to the database
Line 66: dataConnection.Close();
Line 67:
Line 68: Response.End(); // end of the page
Line 69: }
Line 70: }
Line 71: }
Line 72:
Line 2: using System.Data;
Line 3: using System.Data.SqlClient;
Line 4: using System.Web;
Line 5: using System.Web.UI;
Line 6: using System.Web.UI.WebControls;
Line 7: using System.Web.UI.HtmlControls;
Line 8: using System.IO;
Line 9:
Line 10:
Line 11: namespace songDL
Line 12: {
Line 13:
Line 14: public class Download : System.Web.UI.Page
Line 15: {
Line 16:
Line 17: // This call is required by the ASP.NET Web Form Designer.
Line 18: override protected void OnInit(EventArgs e)
Line 19: {
Line 20: InitializeComponent();
Line 21: base.OnInit(e);
Line 22: }
Line 23:
Line 24: private void InitializeComponent()
Line 25: {
Line 26: this.Load += new System.EventHandler(this.Page_Load);
Line 27: }
Line 28:
Line 29: // Instructions to initialize the page when it loads here...
Line 30: private void Page_Load(object sender, System.EventArgs e)
Line 31: {
Line 32: // connect to database using the following credentials
Line 33: string dataString = " my connection string";
Line 34:
Line 35: //use this SQL query to get the appropriate file
Line 36: //string SQL = "SELECT Song, SongSize, SongType FROM SongDetails WHERE SongID=" + Request.QueryString["SongID"];
Line 37:
Line 38: string SQL = "SELECT Song, SongSize, SongType FROM SongDetails WHERE SongID=" + Request.QueryString["SongID"];
Line 39:
Line 40: //creates the connection object to database
Line 41: SqlConnection dataConnection = new SqlConnection(dataString);
Line 42:
Line 43: // creates the sql command to run in the DB
Line 44: SqlCommand dataExecute = new SqlCommand(SQL, dataConnection);
Line 45:
Line 46: // opens the connection
Line 47: dataConnection.Open();
Line 48:
Line 49: // creates reader for database results
Line 50: //SqlDataReader contentDataReader = dataExecute.ExecuteReader();
Line 51: SqlDataReader contentDataReader = dataExecute.ExecuteNonQuery();
Line 52:
Line 53: // reads the database
Line 54: contentDataReader.Read();
Line 55:
Line 56: // clear the buffer
Line 57: Response.Clear();
Line 58:
Line 59: // set content type - tell browser what type of file is being sent from the database
Line 60: Response.ContentType = (string)contentDataReader["SongType"];
Line 61:
Line 62: // stream the audio into the broswer and start playing
Line 63: Response.OutputStream.Write((byte[])contentDataReader["Song"], 0, (int)contentDataReader["SongSize"]);
Line 64:
Line 65: // close connection to the database
Line 66: dataConnection.Close();
Line 67:
Line 68: Response.End(); // end of the page
Line 69: }
Line 70: }
Line 71: }
Line 72:

conversion error
Ido Flatow
SqlDataReader contentDataReader = dataExecute.ExecuteNonQuery();
ExecuteNonQuery of ICommand Interface returns an integer that shows how many records were affected...
Use
SqlDataReader contentDataReader = dataExecute.ExecuteReader();
instead:)
Isolated