Link to home
Create AccountLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

Not all code paths return a value

Not all code paths return a value
- I am just not seeing it? -
        private string getimages(int id, string Type)
        {
 
                try
                {
                    string newPath = "";
                    string conString = "server=.\\SQLEXPRESS;Database=LocalDB;Trusted_Connection=True;";
                    SqlConnection conn = new SqlConnection(conString);
                    SqlDataAdapter da = new SqlDataAdapter("spx_Pull", conn);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.AddWithValue("@id", currentID);
                    da.SelectCommand.Parameters.AddWithValue("@Type", Type);
                    DataSet ds = new DataSet("read");
                    conn.Open();
                    da.Fill(ds, "read");
                    conn.Close();
                    byte[] MyData = new byte[0];
                    DataRow myRow;
                    myRow = ds.Tables["read"].Rows[0];
                    MyData = (byte[])myRow["Document_File"];
                    string name = myRow["filename"].ToString();
                    int ArraySize = new int();
                    ArraySize = MyData.GetUpperBound(0);
                    if (ArraySize > -1)
                    {
                        string temp = ConfigurationManager.AppSettings["Path"].ToString();
                        string path = temp + name;
                        File.WriteAllBytes(path, MyData);
                        byte[] pdf = (byte[])myRow["Document_File"];
                        System.Drawing.Image image;
                        string ext;
                        using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
                        {
                            image = System.Drawing.Image.FromStream(fs);

                        }
                        if (Type == "MOB")
                        {
                            if (pdf.Length > 0)
                            {
                               newPath = path;
                            }
                            return (string)newPath;
                        }
                        if (Type == "APP")
                        {
                            if (pdf.Length > 0)
                            {
                                newPath = path;
                            }
                            return (string)newPath;
                        }
                        return newPath;
                        conn.Close();
                        }
                }
                catch (Exception exShowImage)
                {
                    //MessageBox.Show(exShowImage.Message.ToString());
                }
            }
    }

Open in new window

Avatar of p_davis
p_davis

put a return in a finally or outside of the try
Avatar of r3nder

ASKER

sorry didnt work - any other suggestion??
you put a return in a finally and outside of the try/catch? that should work
oh.... try one in the catch statement
Avatar of r3nder

ASKER

nah! not there either - sam eerror
                    using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
                    {
                        image = System.Drawing.Image.FromStream(fs);

                    }
                    if (Type == "MOB")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                        return (string)newPath;
                    }
                    if (Type == "APP")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                        return (string)newPath;
                    }
                    return newPath;
                    conn.Close();
                }

            }

            catch (Exception exShowImage)
            {
                return (string)newPath;
                //MessageBox.Show(exShowImage.Message.ToString());
            }
            
        }                    using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
                    {
                        image = System.Drawing.Image.FromStream(fs);

                    }
                    if (Type == "MOB")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                        return (string)newPath;
                    }
                    if (Type == "APP")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                        return (string)newPath;
                    }
                    return newPath;
                    conn.Close();
                }

            }

            catch (Exception exShowImage)
            {
                return (string)newPath;
                //MessageBox.Show(exShowImage.Message.ToString());
            }
            
        }

Open in new window

i meant in combination with the finally and one outside of the try/catch
side note:
return newPath;
                    conn.Close();

will make that conn.Close() will NOT be executed!
it needs to be the other way round:
conn.Close();
return newPath;
finally, I would suggest that you put 1 single return newPath at the very end of the procedure, and not from within all and every if/else statement.


                    using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
                    {
                        image = System.Drawing.Image.FromStream(fs);

                    }
                    if (Type == "MOB")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                    }
                    if (Type == "APP")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                    }
                    conn.Close();
                }

            }

            catch (Exception exShowImage)
            {
                //MessageBox.Show(exShowImage.Message.ToString());
            }
            
        }                    using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
                    {
                        image = System.Drawing.Image.FromStream(fs);

                    }
                    if (Type == "MOB")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                    }
                    if (Type == "APP")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                    }
                    conn.Close();
                }

            }

            catch (Exception exShowImage)
            {
                //MessageBox.Show(exShowImage.Message.ToString());
            }
            return (string)newPath;
            
        }
                                            

Open in new window

Avatar of r3nder

ASKER

error-control cannot leave a finally clause
                    if (Type == "MOB")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                        return (string)newPath;
                    }
                    if (Type == "APP")
                    {
                        if (pdf.Length > 0)
                        {
                            newPath = path;
                        }
                        return (string)newPath;
                    }
                    return newPath;
                    conn.Close();
                }

            }

            catch (Exception exShowImage)
            {
                return (string)newPath;
                //MessageBox.Show(exShowImage.Message.ToString());
            }
            finally
            {
                return (string)newPath;
            }
            return newPath;
            
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of r3nder

ASKER

I took the return out of finally and it worked - THANKS p_davis