图像路径未保存在数据库中,并且图像目录也为空 [英] image path not saved in database and image directory is also empty

查看:81
本文介绍了图像路径未保存在数据库中,并且图像目录也为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以在数据库中插入数据.一切正常..但是ProductImage字段在数据库中保持为空,并且其中的image文件夹也没有上传图像..

i have a code to insert data in the database . all is working fine.. but ProductImage field remain empty in database and image folder in the is also not uploading image..

        try
        {
            string path = Server.MapPath(".") + "\\images\\" + FileUpload1.FileName;
            FileUpload1.SaveAs(path);
            SqlConnection con = new SqlConnection();
            con.ConnectionString = connectionString;
con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd = con.CreateCommand();
            
            cmd.CommandText="insert into ESK_Products(CategoryID,ProductName,ProductImage,UnitCost,Description)select CategoryID,' + " + txtproname.Text + "','" + FileUpload1.FileName + "'," + txtproprice.Text + ",'" + txtprodesc.Text + "' from ESK_Categories where CategoryName='" + DropDownList1.Text + "'";
            cmd.ExecuteNonQuery();
            lblresult.Text = "inserted";
            con.Close();
           // Response.Redirect(Request.Url.AbsoluteUri);
        }
        catch (Exception ex)
        {
            lblresult.Text = "not inserted" + ex;
        }



问题是什么?



whats is the problem??

推荐答案

在这种情况下,FileUpload1.FileName是一个空字符串.从提供的代码中,我们不能提供更多帮助.

从您过去几天发布的问题来看,如果您开始自己学习如何查找和解决这些错误,那么我认为这对您有好处,不是我们会介意帮助您,而是必须等待,这很烦人不断寻找答案.
为了帮助您调试应用程序,我相信本文可以为您提供帮助.
Visual Studio 2010中的精通调试-入门指南 [
Well in that case FileUpload1.FileName is an empty string. From the code provided we can''t help out much more than that.

From the questions you''ve posted the past few days I think it would be good for you if you started to learn how to find and resolve those bugs yourself, not that we mind helping you out, but it must be annoying having to wait for an answer constantly.
To help you debug your application I believe this article can help you.
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]


首先检查fileupload类实际上具有使用HasFile属性的文件.之后,您可以确保路径变量包含正确的目录,然后再保存文件.

另一件事,您是否不应该将文件在服务器上的位置实际存储到数据库中.因此,应该在插入语句中使用path而不是插入 FileUpload1.FileName.

而且,不要将字符串连接到sql语句,而是使用
SqlParameter [^ ]
First check that the fileupload class actually has file using HasFile property. After that you can ensure that the path variable contains the correct directory before you save the file.

Another thing, shouldn''t you actually store the location of the file at server into the database. So instead of inserting FileUpload1.FileName should you use path in the insert statement.

And also, instead of concatenating strings to an sql statement, use SqlParameter[^]


这篇关于图像路径未保存在数据库中,并且图像目录也为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆