4

I currently have a connection class that handles all of my database connectivity. What I am trying to do is build a SqlParameterCollection object on one page, then pass that object to my connection class. Is it possible to do this? I am not getting any compilation errors, but I can not get the parameters to be recognized. Here is what I am trying to do:

Page 1: 

    string sql = null;
conn.strConn = connectionstring;

sql = "sqlstring";

SqlParameterCollection newcollect = null;
newcollect.Add("@Switch",1);

conn.OpenReader(sql, newcollect);
while (conn.DR.Read())
{
        read data onto page here...
}
conn.CloseReader();

Page 2 (connection class) :

public void OpenReader(string sql, SqlParameterCollection collect)
{
        Conn = new SqlConnection(strConn);
        Conn.Open();
        Command = new SqlCommand(sql,Conn);

        Command.Parameters.Add(collect);  <------This is the root of my question
        Command.CommandTimeout = 300;
        // executes sql and fills data reader with data
        DR = Command.ExecuteReader(); 
}
1
  • 1
    Also, before adding items to your collection, you need to initialize it! This doesn't work: SqlParameterCollection newcollect = null - you'll need a SqlParameterCollection newcollect = new SqlParameterCollection(); somewhere in your code, before adding any items... Commented Jun 12, 2011 at 7:35

2 Answers 2

4

Basically, in ASP.NET, to persist something from page to page, you need to use session state and put your object there - so you could try something like this:

Page 1:

List<SqlParameter> newcollect = new List<SqlParameter>();
newcollect.Add(new SqlParameter("@Switch", 1));

Session["SqlParameters"] = newcollect;

Page 2 (connection class) :

public void OpenReader(string sql)
{
    Conn = new SqlConnection(strConn);
    Conn.Open();

    Command = new SqlCommand(sql,Conn);

    List<SqlParameter> coll = null;
    if(Session["SqlParameters"] != null)
    {
       coll = (List<SqlParameter>)Session["SqlParameters"];
    }

    Command.Parameters.AddRange(coll.ToArray());
    Command.CommandTimeout = 300;
    // executes sql and fills data reader with data
    DR = Command.ExecuteReader(); 
}

This will work - if you have Session state enabled on your ASP.NET site. This will not work if your site cannot use session state memory for some reason (like if you're on a webfarm and cannot use SQL Server for session state)

Sign up to request clarification or add additional context in comments.

3 Comments

The statement "SqlParameterCollection newcollect = new SqlParameterCollection();" gives the following error: - Error 1 The type 'System.Data.SqlClient.SqlParameterCollection' has no constructors defined
@Muhammed Rauf K: OK, you need to use List<SqlParameter> instead - too bad MS chose to make this collection class not instantiable....
Yes. I used like.. List<SqlParameter> spParamList = new List<SqlParameter>();
-2

You can instantiate a new SqlParameterCollection by like that:

var P = new SqlCommand().Parameters;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.