Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
any one help this
what is this error am trying to view image from Database

Path cannot be null.
Parameter name: path

this is my code....
C#
 protected void btnMulSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtMulName.Text == "")
            {
                string str1 = "Name field cannot be left blank!!!";
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + str1 + "');", true);
            }
            else
            {
                if (txtMulName.Text == ddlparent.Text)
                {
                    msgmulliteral.Text = "Municipal Name and its Parent Node cannot be same.";
                }

                else
                {

                    if (fileup.HasFile)
                    {
                        file = fileup.FileName;
                        bool ans = checkFileExtension(file);
                        if (ans)
                        {
                            try
                            {
                                int length = fileup.PostedFile.ContentLength;
                                byte[] imgbyte = new byte[length];
                                HttpPostedFile img = fileup.PostedFile;
                                img.InputStream.Read(imgbyte, 0, length);
                                string imagename = txtMulShortName.Text;
                                SqlConnection connection = new SqlConnection("server=.......;user id=.....;password=....;database=IMADB");
                                connection.Open();
                                SqlCommand cmd = new SqlCommand("INSERT INTO tbllocationdetails (fileup) VALUES (@loclogo)", connection);
                                //cmd.Parameters.Add("@ImageNAme", SqlDbType.VarChar, 50).Value = imagename;
                                cmd.Parameters.Add("@loclogo", SqlDbType.Image).Value = imgbyte;
                                int count = cmd.ExecuteNonQuery();
                                connection.Close();
                            }

                                //fileadd = (Server.MapPath("Image\\") + txtMulShortName.Text + "." + file.Substring(file.LastIndexOf(".") + 1).ToString());
                            //fileup.SaveAs(fileadd);

                            catch (Exception ex) { }
                            locLogo.ImageUrl = "~/Image/" + txtMulName.Text + "." + file.Substring(file.LastIndexOf(".") + 1).ToString();
                        }
                        else
                        {
                            msgmulliteral.Text = "Invalid file type of Logo, select only JPG files";
                        }

                    }
                    if (ddlparent.Text != "None")
                    {
                        DataSet ds = PDataset("select locid from tbllocation where locname='" + ddlparent.Text + "'");
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            mylocation.parentID = dr["locid"].ToString();
                        }
                    }
                    else
                    {
                        mylocation.parentID = "AA0000";
                    }

                    if (chkMulActive.Checked)
                    {
                        mylocation.active = true;
                    }
                    else
                    {
                        mylocation.active = false;
                    }

                    if (chkMulLogical.Checked)
                    {
                        mylocation.logical = true;
                    }
                    else
                    {
                        mylocation.logical = false;
                    }
                    //Find Previous Entry

                    DataSet d = PDataset("select locpkid, locname from tbllocation where locid='" + txtMunicipalID.Text + "'");
                    DataTable da = d.Tables[0];
                    if (da.Rows.Count > 0)
                    {
                        //Update
                        string str = "UPDATE tbllocation SET "+
                            "locparentid ='" + mylocation.parentID.ToString() + "', " +
                            "locname ='" + txtMulName.Text + "', " +
                            "locactive='" + mylocation.active + "', " + 
                            "userstatus='" + mylocation.logical + "', " + 
                            "locupdate='" + DateTime.Now.ToString() + "', " +
                            "locshortdesc='" + txtMulShortName.Text + "', " +
                            "locdesc='" + txtMulDescription.Text + "' " +
                        "where locpkid='" + da.Rows[0]["locpkid"].ToString() + "'";
                        
                        string updetail = "update tbllocationdetails set locwebsite='" + txtMulWebSite.Text + "', locmanager='" + ddlmanager.Text + "' where locpkid='" + da.Rows[0]["locpkid"].ToString() + "'";

                        
                        SqlConnection conn = new SqlConnection(clsconnection.getConnectionString());
                        SqlCommand cmd = new SqlCommand(str, conn);
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = updetail;
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }

                    else
                    {
                        //Add New
                        mylocation.ID = txtMunicipalID.Text;
                        mylocation.Name = txtMulName.Text;
                        mylocation.Shorname = txtMulShortName.Text;
                        mylocation.description = txtMulDescription.Text;


                        mylocation.manager = ddlmanager.SelectedValue;
                        mylocation.website = txtMulWebSite.Text;

                        if (fileup.HasFile)
                        {
                            FileStream fs;
                            fs = new FileStream(fileadd, FileMode.Open, FileAccess.Read);
                            byte[] picbyte = new byte[fs.Length];
                            fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                            fs.Close();
                            mylocation.fileupload = picbyte;
                        }
                        mylocation.locInsert();
                        clearalltextboxes();
                        Addnew();
                        msgmulliteral.Text = "Data Added Successfully";
                        
                    }
                    Response.Redirect("wpaLocationDetails.aspx");
                    rightpanelbtn(false);
                    leftpanelbtn(true);
                }
            }
}
Posted
Updated 16-Feb-12 23:22pm
v2
Comments
rockpune 17-Feb-12 5:26am    
Server Error in '/' Application.
Path cannot be null.
Parameter name: path
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Path cannot be null.
Parameter name: path

Source Error:

Line 276: {
Line 277: FileStream fs;
Line 278: fs = new FileStream(fileadd, FileMode.Open, FileAccess.Read);
Line 279: byte[] picbyte = new byte[fs.Length];
Line 280: fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
Jaganathan Bantheswaran 17-Feb-12 5:36am    
fileadd is null i guess. i couldn't see any assignment to fileadd variable
rockpune 17-Feb-12 5:40am    
plz tell me what can i do am newly learnig .net plz help me
Jaganathan Bantheswaran 17-Feb-12 5:41am    
Just conform whether the fileadd is null or not by debugging the app.
rockpune 17-Feb-12 5:45am    
not null sir.....loclogo(image,not null)

1 solution

you have commented the code
C#
//fileadd = (Server.MapPath("Image\\") + txtMulShortName.Text + "." + file.Substring(file.LastIndexOf(".") + 1).ToString());
                            //fileup.SaveAs(fileadd);

why???
you save file then read file from server by
C#
FileStream fs;
   fs = new FileStream(fileadd, FileMode.Open, FileAccess.Read);

and file not found so
fs is null
you can use

fileup.PostedFile.InputStream as a Stream to convert it to byte
in place of
first you save file on server then read from file stream....
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 17-Feb-12 7:50am    
Added pre tag and formatting done

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