请帮助上传和下载.....感谢您提前回复 [英] Please help on Uploading and Downloading..... Thanks for ur Response in advance

查看:37
本文介绍了请帮助上传和下载.....感谢您提前回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在上传和下载这是我的代码以下



I am currently working on uploading and downloading a this is my code below

using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class downloads : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["lateefconnectionstring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Id, Name from tblfiles";
                cmd.Connection = con;
                con.Open();
                GridView1.DataSource = cmd.ExecuteReader();
                GridView1.DataBind();
                con.Close();
            }
        }
    }
    protected void Upload(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string contentType = FileUpload1.PostedFile.ContentType;
        using (Stream fs = FileUpload1.PostedFile.InputStream)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                byte[] bytes = br.ReadBytes((Int32)fs.Length);
                string constr = ConfigurationManager.ConnectionStrings["lateefconnectionstring"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    string query = "insert into tblfiles values (@Name, @ContentType, @Data)";
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Name", filename);
                        cmd.Parameters.AddWithValue("@ContentType", contentType);
                        cmd.Parameters.AddWithValue("@Data", bytes);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
        }
        Response.Redirect(Request.Url.AbsoluteUri);
    }
    protected void DownloadFile(object sender, EventArgs e)
    {
        int id = int.Parse((sender as LinkButton).CommandArgument);
        byte[] bytes;
        string fileName, contentType;
        string constr = ConfigurationManager.ConnectionStrings["lateefconnectionstring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Name, Data, ContentType from tblfiles where Id=@Id";
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    sdr.Read();
                    bytes = (byte[])sdr["Data"];
                    contentType = sdr["ContentType"].ToString();
                    fileName = sdr["Name"].ToString();
                }
                con.Close();
            }
        }
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = contentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }
}







我正在获得以下eRROr






and i am getting the below eRROr

Server Error in '/project1' Application.
Column name or number of supplied values does not match table definition.
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.Data.SqlClient.SqlException: Column name or number of supplied values does not match table definition.

Source Error:


Line 57:                         cmd.Parameters.AddWithValue("@Data", bytes);
Line 58:                         con.Open();
Line 59:                         cmd.ExecuteNonQuery();
Line 60:                         con.Close();
Line 61:                     }


Source File: c:\Users\Globalwise\Desktop\project1\downloads.aspx.cs    Line: 59 

推荐答案

检查你的表:

Check your table:
string query = "insert into tblfiles values (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@Name", filename);
    cmd.Parameters.AddWithValue("@ContentType", contentType);
    cmd.Parameters.AddWithValue("@Data", bytes);

您提供了三个值,但未指定它们的去向 - 表格中有三列以上或少于三列。

可能,它很容易修复:始终在SQL中为要插入的列命名

You are providing three values, without specifying where they are to go - and there are more than three or less than three columns in your table.
Probably, it's simple to fix: always name the columns you are inserting into in your SQL

string query = "insert into tblfiles (UseName, Content, Data) values (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@Name", filename);
    cmd.Parameters.AddWithValue("@ContentType", contentType);
    cmd.Parameters.AddWithValue("@Data", bytes);

它只会尝试插入这些列。如果你没有告诉它在哪里放东西,它假设你想要它们按数字顺序 - 所以如果你有一个列,你不需要在开始时提供一个值(例如一个Identity字段,或一个字段)它可以为null)它会尝试将你的值放在那里。

总是指定列:这样你的代码就不会在下个月更改数据库列顺序的某个人身上落下来!

And it will only try to insert to those columns. If you don't tell it where to put things, it assumes you want them in numerical order - so if you have a column you don't need to supply a value for at the start (an Identity field for example, or a field that can be null) it will try to put your values in that.
Always specify columns: that way your code doesn't fall over in someone changes the database columns order next month!


这篇关于请帮助上传和下载.....感谢您提前回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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