将nvarchar文件转换为数据类型int时转换faild。 [英] Conversion faild when converting the nvarchar file to data type int.

查看:86
本文介绍了将nvarchar文件转换为数据类型int时转换faild。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gridview的linkbutton从数据库下载文件,但是在将nvarchar文件转换为数据类型int 时,它给出了一个错误,指出 转换faild ..这是我的实体:



我的数据库中有3个coulmns-SaleID& SaleFilename(此coulmn存储文件及其名称),SaleName(此列以字节存储相同的文件)





我正在尝试使用gridview的linkbutton从数据库下载文件,但没有发生任何事情,它给我一个错误,也没有下载文件。这是我的实体:



我的数据库中有3个coulmns-SaleID& SaleFilename(此coulmn存储文件及其名称),SaleName(此列以字节存储相同的文件)



这是由.aspx代码:



I am trying to download a file from database using linkbutton of a gridview, but its giving me an error stating Conversion faild when converting the nvarchar file to data type int.. Here are my entities:

I have 3coulmns in my database- SaleID & SaleFilename(this coulmn stores files & their names), SaleName(This column Stores the same files in bytes)


I am trying to download a file from database using linkbutton of a gridview, but nothing is happening niether its giving me an error nor the file is bieng downloaded. Here are my entities:

I have 3coulmns in my database- SaleID & SaleFilename(this coulmn stores files & their names), SaleName(This column Stores the same files in bytes)

Here is by .aspx code:

<asp:TemplateField HeaderText="SaleName" SortExpression="SaleName">
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server"CommandName="Download" CommandArgument='<%# Bind("SaleFileName") %>' Text='<%# Bind("SaleName") %>' ></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>





这是我的代码背后文件:





Here is my Code Behind File:

protected void gridContributions_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
      // make sure fileName  contains only file name like 'test.pdf'
            string fileName = Convert.ToString(e.CommandArgument);

            // make sure filePath  contains file path like 'Uploads/Scheme/test.pdf'

             string filePath = e.CommandArgument.ToString();


            string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
            // SqlConnection con = new SqlConnection(connectionString);

            byte[] bytes;
            //string ContentType;

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select SaleFileName from Sales";
                    cmd.Parameters.AddWithValue("@SaleFileName", SqlDBType.NVarChar );
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        sdr.Read(); //Here is throwing an exception saying Conversion faild when converting the nvarchar file to data type int.
                        bytes = (byte[])sdr["SaleFileName"];

                    }
                    con.Close();
                }
            }
            Response.Clear();
            Response.Buffer = true;

            // Read the original file from disk
             FileStream myFileStream = new FileStream( filePath  ,FileMode.Open);
            long FileSize = myFileStream.Length;
            byte[] Buffer = new byte[Convert.ToInt32(FileSize)];
            myFileStream.Read(Buffer, 0, Convert.ToInt32(FileSize));
            myFileStream.Close();




           // // Tell the browse stuff about the file
            Response.AddHeader("Content-Length", bytes.ToString());
            //Response.AddHeader("Content-Disposition", "inline; filename=" & fileName.Replace(" ", "_"));
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
            Response.TransmitFile(fileName);
            Response.ContentType = "application/octet-stream";

            // Send the data to the browser
            Response.BinaryWrite(Buffer);
            Response.End();
        }
    }





如果有人能告诉我我犯了什么错误会有很大的帮助。



Please if anybody can tell me what mistake I am making that would be great help.

推荐答案

i)我只是想知道为什么要将参数传递给sql查询,而看起来你不需要传递任何参数正在处理SQL查询而不是存储过程。要么将查询保留在存储过程中,要将值作为参数进行检索,否则将参数连接到查询本身,根据需要添加where条件。



否则你做根本不需要任何参数。



ii)其次,即使你传递参数值,它的调用方式在这里看起来也错了。 />


i) First thing i just wanted to know why you are passing the parameter to the sql query whereas it does not look like you need to pass any parameter as you are dealing with a SQL query not a stored procedure. Either keep the query in a stored procedure and retrieve value as parameter you want else pass parameter concatenating to the query itself adding a where condition if required.

Or else you do not need any parameter at all.

ii) Secondly even if you are passing the parameter with value , the way it is being called is looking like wrong here.

cmd.Parameters.AddWithValue("@SaleFileName", SqlDBType.NVarChar );





因为方法名称建议它需要参数两个参数如参数名称和值。第一个正确传递参数名称,但第二个应该是您要传递的参数值而不是此处的数据类型。请检查一下。



请查看以下参考资料: -

1. 使用C#读取,插入,更新和删除简单的ADO.NET数据库。 [ ^ ]



2. http://www.aspsnippets.com/Articles/Save-and-Retrieve-Files-from-SQL-Server-Database-using-ASP。 Net.aspx



希望它对您有所帮助。



As the method name suggest it expects parameters two parameters like the parameter name and the value. The first one you are passing correctly the parameter name, but the second one should be the parameter value you want to pass not the datatype here. Please check this.

Please check with below references :-
1. Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]

2. http://www.aspsnippets.com/Articles/Save-and-Retrieve-Files-from-SQL-Server-Database-using-ASP.Net.aspx

Hope it will be of help to you.


这篇关于将nvarchar文件转换为数据类型int时转换faild。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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