basically i would like to add up the total size of all the files for a particular user and display it in a label...this is what i have so far:

SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True");


            SqlCommand myFileCommand = new SqlCommand("SELECT SUM (fileSize) FROM aspnet_Files WHERE fileOwner = '" + this.User.Identity.Name + "'", myConnection);


           lblTotalSpaceUsed.Text = >>>result of the sql statement here?<<<


just wondering how you set the label text to the result of the sql statement, thanks for help


Use execute scalar as it returns the value of first row of the first column of the result set like so:

string result =  (string) myFileCommand.ExecuteScalar();
lblTotalSpaceUsed.Text = result;


System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.

how can i convert the database fileSize of type "int" to string??

k i just solved it with:

Int32 result =  (Int32) myFileCommand.ExecuteScalar();

lblTotalSpaceUsed.Text = result.ToString();

thanks again for the help ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.