Click here to Skip to main content
16,021,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently working on uploading and downloading a this is my code below

C#
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();
    }
}



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 
Posted
Comments
[no name] 2-Aug-14 11:30am    
Column name or number of supplied values does not match table definition.
One of your column names is wrong, or you have something missing or extra.

1 solution

Check your table:
C#
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);
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
C#
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);
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!
 
Share this answer
 

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