Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 19th, 2011, 10:24 AM
Registered User
 
Join Date: Jan 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Trying to work with Imar's File Manager

What I've been trying to do is to add another field to the database table of files so I can tie specific files to a RequestID. Everything is going to be centered around a querystring of RequestID.

Synopsis:
RequestID is going to be used as a field to tie my request form to the uploaded files. When one of my users clicks to view a request, the address is something like this:

"/ViewRequest.aspx?RequestID=1"

What I want to do in the gridview is to return any files with that request id which is integer. When I upload a file, I also will be using the querystring.

DB Schema:

Code:
Id (UniqueIdentifier)
FileURL (nvarchar(255)),null)
FileData (varbinary(max),null)
OriginalName (nvarchar(50) not null)
ContentType (nvarchar(50) not null)
DateCreated (datetime,not null)
ReqFormId (numeric(18,0), null) * This is the new field
Taking his existing project, exactly how would someone add the new field to the SPs and code behind?

The link to his overview of this is http://imar.spaanjaars.com/414/stori...with-aspnet-20

I've been working on this but I'm at the point of complete frustration. It shouldn't be this difficult to add a field and then use it to query on.
 
Old January 19th, 2011, 02:59 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
It shouldn't be this difficult to add a field and then use it to query on.
It shouldn't be, and isn't...

What have you tried so far to make it work?

Using the RequestId for the GridView means you only filter the list. Requests for single files remains the same. Is that your intention?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 19th, 2011, 05:50 PM
Registered User
 
Join Date: Jan 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Imar, that is exactly what I'm intending to do.
 
Old January 19th, 2011, 06:11 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What have you tried so far to make it work?

Here's a run-down of what you need to do (from memory; don't have the source code here):

1. Modify (or create an overload of ) GetList in the FileInfo class so it accepts an int for the RequestId.

2. Pass the ID to the stored procedure. Take a look at GetItem to see how that method passes the ID to the database.

3. Alter the sproc that gets the list (sprocFileInfoSelectList) so it accepts an ID. If you want to maintain the current GetList functionality, make the parameter optional by setting it to null.

4. Alter the WHERE clause in the sproc so it takes the RequestId into account. Use @RequestId IS NULL if you want to maintain the current GetList functionality to return all Files when there's no RequestId. E.g.

... WHERE RequestId = @RequestId OR @RequestId IS NULL

5. Modify the ObjectDataSource by adding a SelectParameters element. Inside that element, add a QueryStringParameter to look at the Request ID query string parameter. (http://www.aspnet101.com/2007/11/usi...ringparameter/)

6. Enjoy....

If you can't make it work, please be specific about what works and what doesn't. I am not going to write the complete solution for you, so you need to clearly indicate what you've already done and what you need help with.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 20th, 2011, 07:33 AM
Registered User
 
Join Date: Jan 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Ok, when I pass my querystring RequestID, the browser kicks back this error:
Code:
ObjectDataSource 'odsFiles' could not find a non-generic method 'GetList' that has parameters: RequestID.
My changes - Added RequestID to variables

FileInfo.cs:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// The FileInfo class contains summary information about uploaded files.
/// </summary>
[DataObject()]
public class FileInfo
{
  #region Private Variables

  private Guid id;
  private string contentType;
  private string originalName;
  private DateTime dateCreated;
  private Int32 ReqFormId;

  #endregion

  #region Public Properties

  public Int32 RequestID
  {
      get
      {
          return ReqFormId;
      }
  }

  /// <summary>
  /// Gets the unique id of the uploaded file.
  /// </summary>
  public Guid Id
  {
    get
    {
      return id;
    }
  }

  /// <summary>
  /// Gets the date and time the file was uploaded.
  /// </summary>
  public DateTime DateCreated
  {
    get
    {
      return dateCreated;
    }
  }

  /// <summary>
  /// Gets the content type of the file.
  /// </summary>
  public string ContentType
  {
    get
    {
      return contentType;
    }
  }

  /// <summary>
  /// Gets the original name of the file.
  /// </summary>
  public string OriginalName
  {
    get
    {
      return originalName;
    }
  }
  #endregion

  #region Constructor(s)

  /// <summary>
  /// Initializes a new instance of the <see cref="FileInfo"/> class with the data from the SqlDataReader.
  /// </summary>
  public FileInfo(SqlDataReader myReader)
  {
    ReqFormId = myReader.GetInt32(myReader.GetOrdinal("ReqFormId"));
    id = myReader.GetGuid(myReader.GetOrdinal("Id"));
    dateCreated = myReader.GetDateTime(myReader.GetOrdinal("DateCreated"));
    originalName = myReader.GetString(myReader.GetOrdinal("OriginalName"));
    contentType = myReader.GetString(myReader.GetOrdinal("ContentType"));
  }

  #endregion

  #region Public Methods

  /// <summary>
  /// Gets a list with <see cref="FileInfo"/> objects.
  /// </summary>
  /// <returns>A list with <see cref="FileInfo"/> objects when the database contains any files, or null otherwise.</returns>
  [DataObjectMethod(DataObjectMethodType.Select, true)]
  public static List<FileInfo> GetList()
  {
    List<FileInfo> myList = null;

    using (SqlConnection mySqlConnection = new SqlConnection(AppConfiguration.ConnectionString))
    {
      SqlCommand myCommand = new SqlCommand("sp_PH_FileInfoSelectList", mySqlConnection);
      myCommand.CommandType = CommandType.StoredProcedure;

      mySqlConnection.Open();

      using (SqlDataReader myReader = myCommand.ExecuteReader())
      {
        if (myReader.HasRows)
        {
          myList = new List<FileInfo>();
          while (myReader.Read())
          {
            myList.Add(new FileInfo(myReader));
          }
        }
      }

      mySqlConnection.Close();
    }
    return myList;
  }

  #endregion
}
default.aspx
Code:
      <asp:ObjectDataSource ID="odsFiles" runat="server" SelectMethod="GetList" TypeName="FileInfo">
      <SelectParameters>
      	<asp:QueryStringParameter Name="RequestID" QueryStringField="RequestID" />
      </SelectParameters>
      </asp:ObjectDataSource>
default.aspx.cs -- No changes
Code:
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    switch (e.Row.RowType)
    {
      case DataControlRowType.DataRow:
        FileInfo myFileInfo = (FileInfo)e.Row.DataItem;
        switch (myFileInfo.ContentType.ToLower())
        {
          case "image/pjpeg":         // .jpg files
          case "image/gif":           // .gif files
          case "application/msword":  // .doc files
          case "text/plain":          // .txt files 
            // Do nothing. When the row contains a viewable type, 
            // we want the View link to be enabled.
            break;
          default:
            // Find the View link and disable it.
            HyperLink myLink = (HyperLink)e.Row.FindControl("lnkView");
            myLink.Enabled = false;
            break;
        }
        break;
    }
  }
 
Old January 20th, 2011, 08:09 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You missed step 1 and 2 (and maybe 3 and 4 as well) where you need to create an overload of GetList that accepts an id:

public static List<FileInfo> GetList(int requestId)
{
// Send requestId to the database using a parameter.
}

The ObjectDataSource now expects a GetList method that accepts an int requestId, which you haven't created yet, hence you get this error.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 20th, 2011, 09:02 AM
Registered User
 
Join Date: Jan 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Ok, I can see where that could cause a problem. I've gone ahead and made some mods as recommended. Now i have a problem where the page never finishes loading so I'm not exactly sure what's wrong. So, back to the code again :) Let me know if I missed something.

FileInfo.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// The FileInfo class contains summary information about uploaded files.
/// </summary>
[DataObject()]
public class FileInfo
{
  #region Private Variables

  private Guid id;
  private string contentType;
  private string originalName;
  private DateTime dateCreated;
  private Int32 requestID;

  #endregion

  #region Public Properties
  /// <summary>
  /// Gets the RequestID for files that match request ID#
  /// </summary>
  public Int32 RequestID
  {
      get
      {
          return requestID;
      }
  }

  /// <summary>
  /// Gets the unique id of the uploaded file.
  /// </summary>
  public Guid Id
  {
    get
    {
      return id;
    }
  }

  /// <summary>
  /// Gets the date and time the file was uploaded.
  /// </summary>
  public DateTime DateCreated
  {
    get
    {
      return dateCreated;
    }
  }

  /// <summary>
  /// Gets the content type of the file.
  /// </summary>
  public string ContentType
  {
    get
    {
      return contentType;
    }
  }

  /// <summary>
  /// Gets the original name of the file.
  /// </summary>
  public string OriginalName
  {
    get
    {
      return originalName;
    }
  }
  #endregion

  #region Constructor(s)

  /// <summary>
  /// Initializes a new instance of the <see cref="FileInfo"/> class with the data from the SqlDataReader.
  /// </summary>
  public FileInfo(SqlDataReader myReader)
  {
    id = myReader.GetGuid(myReader.GetOrdinal("Id"));
    dateCreated = myReader.GetDateTime(myReader.GetOrdinal("DateCreated"));
    originalName = myReader.GetString(myReader.GetOrdinal("OriginalName"));
    contentType = myReader.GetString(myReader.GetOrdinal("ContentType"));
    requestID = myReader.GetInt32(myReader.GetOrdinal("RequestID"));
  }

  #endregion

  #region Public Methods

  /// <summary>
  /// Gets a list with <see cref="FileInfo"/> objects.
  /// </summary>
  /// <returns>A list with <see cref="FileInfo"/> objects when the database contains any files, or null otherwise.</returns>
  [DataObjectMethod(DataObjectMethodType.Select, true)]
  public static List<FileInfo> GetList(int requestID)
  {
    List<FileInfo> myList = null;

    using (SqlConnection mySqlConnection = new SqlConnection(AppConfiguration.ConnectionString))
    {
      SqlCommand myCommand = new SqlCommand("sp_PH_FileInfoSelectList", mySqlConnection);
      myCommand.CommandType = CommandType.StoredProcedure;

      SqlParameter RequestID = new SqlParameter("@requestID", SqlDbType.Int);
      RequestID.Value = Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString["RequestID"]);
      
      mySqlConnection.Open();

      using (SqlDataReader myReader = myCommand.ExecuteReader())
      {
        if (myReader.HasRows)
        {
          myList = new List<FileInfo>();
          while (myReader.Read())
          {
            myList.Add(new FileInfo(myReader));
          }
        }
      }

      mySqlConnection.Close();
    }
    return myList;
  }

  #endregion
}
sp_PH_FileInfoSelectList

Code:
USE [FILESDB]
GO
/****** Object:  StoredProcedure [dbo].[sp_PH_FileInfoSelectList]    Script Date: 01/20/2011 08:00:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_PH_FileInfoSelectList]

@requestID int

AS

SELECT
	Id,
	OriginalName,
	ContentType,
	DateCreated,
	RequestID
FROM
	PH_Files
WHERE
	RequestID = @requestID OR @requestID IS NULL
ORDER BY
	DateCreated DESC

Last edited by bbcompent1; January 20th, 2011 at 09:41 AM.. Reason: Old code posted by accident
 
Old January 20th, 2011, 09:53 AM
Registered User
 
Join Date: Jan 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I have indeed specified the QueryString as the value and the debugger shows that the value of requestID is 1 which is the querystring value. However, I'm getting this error:

Code:
Procedure or function 'sp_PH_FileInfoSelectList' expects parameter '@requestID', which was not supplied.
 
Old January 20th, 2011, 10:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Procedure or function 'sp_PH_FileInfoSelectList' expects parameter '@requestID', which was not supplied.
Which makes perfect sense to me, as you're not passing the parameter to the stored procedure. As I hinted earlier, take a look at the implementation of GetFile to see how to pass the ID. You'll see something like this:

SqlParameter prmId = new SqlParameter("@id", SqlDbType.UniqueIdentifier);
prmId.Value = fileId;
myCommand.Parameters.Add(prmId);

Rather than a UniqueIdentifier, you need to make the type an integer.

I also don't understand this:

Code:
 
RequestID.Value = Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString["RequestID"]);
Why would you get it from the HttpContext directly? You just created a parameter requestID that will hold the requested ID. The ObjectDataSource will take it from the query string and send it your GetList method...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 20th, 2011, 10:06 AM
Registered User
 
Join Date: Jan 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Arghh, one missing piece.

Code:
using (SqlConnection mySqlConnection = new SqlConnection(AppConfiguration.ConnectionString))
    {
      SqlCommand myCommand = new SqlCommand("sp_PH_FileInfoSelectList", mySqlConnection);
      myCommand.CommandType = CommandType.StoredProcedure;

      SqlParameter RequestID = new SqlParameter("@requestID", SqlDbType.Int);
      RequestID.Value = Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString["RequestID"]);

      myCommand.Parameters.Add(RequestID); // This was what I missed...
      
      mySqlConnection.Open();

      using (SqlDataReader myReader = myCommand.ExecuteReader())





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 6, Ajax File Manager, error in upload.php file kenj BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 4 June 4th, 2010 08:46 PM
File Manager Namespace error rob BOOK: ASP.NET Website Programming Problem-Design-Solution 0 January 29th, 2004 07:35 AM
File manager guoqi BOOK: ASP.NET Website Programming Problem-Design-Solution 1 October 13th, 2003 10:22 AM
Securing File Manager pkemman BOOK: ASP.NET Website Programming Problem-Design-Solution 4 August 31st, 2003 04:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.