Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have code here written but in the if statements to select the response.content type based on the file extension of the file in the database, the 'ext' declared string is giving me an error. How do I fix this?

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "view":
                        string filename = e.CommandArgument.ToString();
                        string path = Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"] + "/" + filename);
                        String ext;
                        SqlConnection conn;
                        SqlCommand comm;
                        String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                        conn = new SqlConnection(connectionString);
                        comm = new SqlCommand("SELECT Incident.File_Extension FROM Incident	JOIN User_Acc ON user_acc.User_id=incident.user_id JOIN business ON incident.business_id=business.Business_id JOIN incident_type ON incident_type.incident_type_id = incident.incident_type_id WHERE Business.Business_Name = @BusinessName", conn);
                        comm.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar).Value = Session["BusinessName"];
                        conn.Open();
                        SqlDataReader reader = comm.ExecuteReader();
                        while (reader.Read())
                        {
                          ext = reader["File_Extension"].ToString();                            
                        }
                        byte[] bts = System.IO.File.ReadAllBytes(path);
                        Response.Clear();
                        Response.ClearHeaders();
                        if ( ext == ".mov")
                        {
                            Response.ContentType = "video/quicktime";
                        }
                        else if (ext == ".jpg")
                        {
                            Response.ContentType = "image";
                        }
                        Response.WriteFile(path);
                        Response.Flush();
                        Response.End();
                        break;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);

            }
        }
Posted

The Solution 1 will help to formally compile the application, but it will leave it defective. You need to address the root cause of the problem. Think why it is not allowed to work with unassigned variables. A dummy value will allow your code to skip both if ... else if statements of your code, which will not assign anything to Response.ContentType. The purpose of the syntax rule was to avoid this situation. It would be better if you did not assign string.Empty but added else to your if, to assign something for the cases when none of the "extensions" match.

Also, you need to take ext.ToLower(), to compare in case-insensitive way.

—SA
 
Share this answer
 
Simply assign a dummy value to ext variable when you declare it.
This way:
C#
String ext = String.Empty;


Hope this helps.

[Edit]
If I were you, I would change the way I compare strings; specifically these lines:
C#
if ( ext == ".mov")

C#
else if (ext == ".jpg")

which I would translate to:
C#
if (ext.Equals(".mov", StringComparison.InvariantCultureIgnoreCase))

C#
else if (ext.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase))

repectively.
[/Edit]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Feb-14 17:12pm    
I voted 4, but...
Formally, you advice will help the application to compile formally, but it will leave it defective.
I explained it in my answer, please see. Another problem of your advice is case-sensitive string comparison.
—SA
phil.o 27-Feb-14 2:43am    
You're right, thank you Sergey.
But I don't get you about the case-sensitive string comparison: I actually advised to make a case-insensitive one, instead. Or am I missing something?
Sergey Alexandrovich Kryukov 27-Feb-14 2:53am    
Say, ext equals to .Jpg, then your comparison will return false. It will be case insensitive if you compare not ext, but ext.ToLower()........

Ah, sorry, I failed to notice *IgnoreCase in StringComparison.InvariantCultureIgnoreCase...
You are right about case-insensitive...

—SA
phil.o 27-Feb-14 2:54am    
Right :) I really began to think I needed to go back to bed ^^
Sergey Alexandrovich Kryukov 27-Feb-14 2:59am    
:-)

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