本文介绍了这是什么错误试图从数据库中查看图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助这个
正在尝试从数据库查看图像的错误是什么?

路径不能为空.
参数名称:path

这是我的代码....

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....

 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);
                }
            }
}

推荐答案

//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

FileStream fs;
   fs = new FileStream(fileadd, FileMode.Open, FileAccess.Read);


和文件找不到,所以
fs为空
您可以使用

fileup.PostedFile.InputStream作为将其转换为字节的Stream
代替
首先,您将文件保存在服务器上,然后从文件流中读取....


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....


这篇关于这是什么错误试图从数据库中查看图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:39