Showing posts with label private. Show all posts
Showing posts with label private. Show all posts

Monday, March 26, 2012

Pass value to Web User Control

Say I have a user control like this one...
private Sections s = new Sections();

private void Page_Load(object sender, System.EventArgs e)
{
}

public Sections Sections
{
get { return s; }
set { s = value; }
}

public void f()
{
if (s.Query.Load())
while (s.MoveNext())
Response.Write(string.Format("<tr><td width='5'><img src='images/arrow.gif'></td><td><a href='showsections.aspx?s={0}'>{1}</a></td></tr>", s.SectionId, s.Title));
}
And I dragged the user control to index.aspx. Now, on page load of index.aspx, I want to pass some value to the user control's Section property. How do I do this? Or is this possible?

Thanks.If you have the User Control on the Page then you can simply use myUserControl.Sections = someValue; This is same as you would do for any other control on your page.
The thing is, even if i dragged the UserControl on my index.aspx, I can't seem to find SectionsControl (my web user control). At design mode though, I can see SectionsControl1 on index.aspx.

Am I missing something here?
Yes that is pretty much the behaviour you expect for any other control you add to a page. When you add a textbox control first time, the name of textbox will be textBox1 and second control will the name textBox2. In the same manner when you add a usercontrol, it will also follow the same behaviour. So in your code you should write this SectionsControl1.Sections = someValue;
It's a bug in VS 2003. You add a control to your page, but it fails to show up in your codebehind.

Go into the codebehind designer region, and declare your control (of the same name as on the page) with events.
Or, when you add your control to your page by dragging, save your page, THEN go into codebehind. It should show up there.
I missed this post last days. Sorry for the late reply. A bug that is, so decided to declare it myself. Thanks guys.

Wednesday, March 21, 2012

Passing a DataReader between methods and getting RETURN_VALUE

Hi all,

I have the following problem:

I have a private method that returns a SqlDataReader. For this to work I
have not to close the DB connection in the above method. I do this only to
have the possibility to iterate through the entire rows set in a while loop,
located in the calling method.

I have included a few lines of code to get the number of rows fetched from
the DB. I do this with SqlParameter("RETURN_VALUE", SqlDbType.Int) [ I am
using a stored procedure that return @dotnet.itags.org.@dotnet.itags.org.rowcount].

I have found out that I am getting the appropriate value for the stored
procedure parameter ("RETURN_VALUE") ONLY when I have explicitly close the
DB connection. Unfortunately when the control is returned to the calling
method the usual error message

Invalid attempt to Read when reader is closed

is being received as the connection is closed and there's no such DataReader
already.

Does anyone know a workaround for this?

Thanks,

Martin

--------

private SqlDataReader GetReader(string parameter, string date)

{

DateTime MyDateTime;

....//. date parsing

SqlConnection myConn = new SqlConnection(ConnectionString());
SqlCommand myCmd = new SqlCommand();

SqlDataReader myReader=null;

myCmd.CommandType = CommandType.StoredProcedure;
myCmd.Connection = myConn;
myCmd.CommandText = "GetData";
myCmd.CommandTimeout = 250;

SqlParameter Param1 = new SqlParameter();
Param1 = myCmd.Parameters.Add("@dotnet.itags.org.parameter", SqlDbType.VarChar, 12);
Param1.Direction = ParameterDirection.Input;
Param1.Value = parameter;

SqlParameter Param2 = new SqlParameter();
Param2 = myCmd.Parameters.Add("@dotnet.itags.org.date", SqlDbType.VarChar, 20);
Param2.Direction = ParameterDirection.Input;
Param2.Value = MyDateTime.Date.ToShortDateString();

SqlParameter outValue = new SqlParameter();
outValue = myCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
outValue.Direction = ParameterDirection.ReturnValue;

myConn.Open();
myReader = myCmd.ExecuteReader();

// this works only is myConn.Close() is executed but then

// we cannot return a READER to the calling method?

intRowsReturned =
Convert.ToInt32(myCmd.Parameters["RETURN_VALUE"].Value);
return myReader;
}

// the calling method

private void btnSQLGet_Click(object sender, System.EventArgs e)

{

/// ... .

// get the data in a reader

SqlDataReader myReader = GetReader(cboParameter.Text, txtDate.Text);
if (myReader==null)
return;
///....

}You can't read output parameters in a DataReader until the DataReader is
closed. Close the DataReader, and leave the Connection opened to read the
value.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Martin Raychev" <marty_gov@.hotmail.com> wrote in message
news:#RLAe#IKEHA.1892@.TK2MSFTNGP09.phx.gbl...
> Hi all,
>
> I have the following problem:
>
> I have a private method that returns a SqlDataReader. For this to work I
> have not to close the DB connection in the above method. I do this only to
> have the possibility to iterate through the entire rows set in a while
loop,
> located in the calling method.
>
> I have included a few lines of code to get the number of rows fetched from
> the DB. I do this with SqlParameter("RETURN_VALUE", SqlDbType.Int) [ I am
> using a stored procedure that return @.@.rowcount].
>
> I have found out that I am getting the appropriate value for the stored
> procedure parameter ("RETURN_VALUE") ONLY when I have explicitly close the
> DB connection. Unfortunately when the control is returned to the calling
> method the usual error message
>
> Invalid attempt to Read when reader is closed
>
> is being received as the connection is closed and there's no such
DataReader
> already.
>
> Does anyone know a workaround for this?
>
> Thanks,
> Martin
>
> --------
> private SqlDataReader GetReader(string parameter, string date)
> {
> DateTime MyDateTime;
> ....//. date parsing
>
> SqlConnection myConn = new SqlConnection(ConnectionString());
> SqlCommand myCmd = new SqlCommand();
> SqlDataReader myReader=null;
>
> myCmd.CommandType = CommandType.StoredProcedure;
> myCmd.Connection = myConn;
> myCmd.CommandText = "GetData";
> myCmd.CommandTimeout = 250;
> SqlParameter Param1 = new SqlParameter();
> Param1 = myCmd.Parameters.Add("@.parameter", SqlDbType.VarChar, 12);
> Param1.Direction = ParameterDirection.Input;
> Param1.Value = parameter;
>
> SqlParameter Param2 = new SqlParameter();
> Param2 = myCmd.Parameters.Add("@.date", SqlDbType.VarChar, 20);
> Param2.Direction = ParameterDirection.Input;
> Param2.Value = MyDateTime.Date.ToShortDateString();
>
> SqlParameter outValue = new SqlParameter();
> outValue = myCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
> outValue.Direction = ParameterDirection.ReturnValue;
> myConn.Open();
> myReader = myCmd.ExecuteReader();
>
> // this works only is myConn.Close() is executed but then
> // we cannot return a READER to the calling method?
> intRowsReturned =
> Convert.ToInt32(myCmd.Parameters["RETURN_VALUE"].Value);
> return myReader;
> }
>
> // the calling method
> private void btnSQLGet_Click(object sender, System.EventArgs e)
> {
> /// ... .
> // get the data in a reader
> SqlDataReader myReader = GetReader(cboParameter.Text, txtDate.Text);
> if (myReader==null)
> return;
> ///....
> }
1) the return value and parameters values of a stored proc are returned
after all result sets in the proc are returned.
2) a datareader is a forward only cursor, so to get to the return value, you
need to read thru all rows, and result sets
3) a close will read thru all rows and result sets for you, but is not
necessary, the following code will also work:

// process all result sets to get to parameters and reutn value

do
{
while (dr.Read())
;
} while (dr.NextResult())

// now you can access output parameters and the return value

returning a row count makes little sesnse, as you could add up the row
count yourself, as you have to read them all to get to the count return
value.

-- bruce (sqlwork.com)

"Martin Raychev" <marty_gov@.hotmail.com> wrote in message
news:#RLAe#IKEHA.1892@.TK2MSFTNGP09.phx.gbl...
> Hi all,
>
> I have the following problem:
>
> I have a private method that returns a SqlDataReader. For this to work I
> have not to close the DB connection in the above method. I do this only to
> have the possibility to iterate through the entire rows set in a while
loop,
> located in the calling method.
>
> I have included a few lines of code to get the number of rows fetched from
> the DB. I do this with SqlParameter("RETURN_VALUE", SqlDbType.Int) [ I am
> using a stored procedure that return @.@.rowcount].
>
> I have found out that I am getting the appropriate value for the stored
> procedure parameter ("RETURN_VALUE") ONLY when I have explicitly close the
> DB connection. Unfortunately when the control is returned to the calling
> method the usual error message
>
> Invalid attempt to Read when reader is closed
>
> is being received as the connection is closed and there's no such
DataReader
> already.
>
> Does anyone know a workaround for this?
>
> Thanks,
> Martin
>
> --------
> private SqlDataReader GetReader(string parameter, string date)
> {
> DateTime MyDateTime;
> ....//. date parsing
>
> SqlConnection myConn = new SqlConnection(ConnectionString());
> SqlCommand myCmd = new SqlCommand();
> SqlDataReader myReader=null;
>
> myCmd.CommandType = CommandType.StoredProcedure;
> myCmd.Connection = myConn;
> myCmd.CommandText = "GetData";
> myCmd.CommandTimeout = 250;
> SqlParameter Param1 = new SqlParameter();
> Param1 = myCmd.Parameters.Add("@.parameter", SqlDbType.VarChar, 12);
> Param1.Direction = ParameterDirection.Input;
> Param1.Value = parameter;
>
> SqlParameter Param2 = new SqlParameter();
> Param2 = myCmd.Parameters.Add("@.date", SqlDbType.VarChar, 20);
> Param2.Direction = ParameterDirection.Input;
> Param2.Value = MyDateTime.Date.ToShortDateString();
>
> SqlParameter outValue = new SqlParameter();
> outValue = myCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
> outValue.Direction = ParameterDirection.ReturnValue;
> myConn.Open();
> myReader = myCmd.ExecuteReader();
>
> // this works only is myConn.Close() is executed but then
> // we cannot return a READER to the calling method?
> intRowsReturned =
> Convert.ToInt32(myCmd.Parameters["RETURN_VALUE"].Value);
> return myReader;
> }
>
> // the calling method
> private void btnSQLGet_Click(object sender, System.EventArgs e)
> {
> /// ... .
> // get the data in a reader
> SqlDataReader myReader = GetReader(cboParameter.Text, txtDate.Text);
> if (myReader==null)
> return;
> ///....
> }

Passing a DataReader between methods and getting RETURN_VALUE

Hi all,
I have the following problem:
I have a private method that returns a SqlDataReader. For this to work I
have not to close the DB connection in the above method. I do this only to
have the possibility to iterate through the entire rows set in a while loop,
located in the calling method.
I have included a few lines of code to get the number of rows fetched from
the DB. I do this with SqlParameter("RETURN_VALUE", SqlDbType.Int) [ I a
m
using a stored procedure that return @dotnet.itags.org.@dotnet.itags.org.rowcount].
I have found out that I am getting the appropriate value for the stored
procedure parameter ("RETURN_VALUE") ONLY when I have explicitly close the
DB connection. Unfortunately when the control is returned to the calling
method the usual error message
Invalid attempt to Read when reader is closed
is being received as the connection is closed and there's no such DataReader
already.
Does anyone know a workaround for this?
Thanks,
Martin
private SqlDataReader GetReader(string parameter, string date)
{
DateTime MyDateTime;
...//. date parsing
SqlConnection myConn = new SqlConnection(ConnectionString());
SqlCommand myCmd = new SqlCommand();
SqlDataReader myReader=null;
myCmd.CommandType = CommandType.StoredProcedure;
myCmd.Connection = myConn;
myCmd.CommandText = "GetData";
myCmd.CommandTimeout = 250;
SqlParameter Param1 = new SqlParameter();
Param1 = myCmd.Parameters.Add("@dotnet.itags.org.parameter", SqlDbType.VarChar, 12);
Param1.Direction = ParameterDirection.Input;
Param1.Value = parameter;
SqlParameter Param2 = new SqlParameter();
Param2 = myCmd.Parameters.Add("@dotnet.itags.org.date", SqlDbType.VarChar, 20);
Param2.Direction = ParameterDirection.Input;
Param2.Value = MyDateTime.Date.ToShortDateString();
SqlParameter outValue = new SqlParameter();
outValue = myCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
outValue.Direction = ParameterDirection.ReturnValue;
myConn.Open();
myReader = myCmd.ExecuteReader();
// this works only is myConn.Close() is executed but then
// we cannot return a READER to the calling method?
intRowsReturned =
Convert.ToInt32(myCmd.Parameters["RETURN_VALUE"].Value);
return myReader;
}
// the calling method
private void btnSQLGet_Click(object sender, System.EventArgs e)
{
/// ... .
// get the data in a reader
SqlDataReader myReader = GetReader(cboParameter.Text, txtDate.Text);
if (myReader==null)
return;
///....
}You can't read output parameters in a DataReader until the DataReader is
closed. Close the DataReader, and leave the Connection opened to read the
value.
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Martin Raychev" <marty_gov@.hotmail.com> wrote in message
news:#RLAe#IKEHA.1892@.TK2MSFTNGP09.phx.gbl...
> Hi all,
>
> I have the following problem:
>
> I have a private method that returns a SqlDataReader. For this to work I
> have not to close the DB connection in the above method. I do this only to
> have the possibility to iterate through the entire rows set in a while
loop,
> located in the calling method.
>
> I have included a few lines of code to get the number of rows fetched from
> the DB. I do this with SqlParameter("RETURN_VALUE", SqlDbType.Int) [ I
am
> using a stored procedure that return @.@.rowcount].
>
> I have found out that I am getting the appropriate value for the stored
> procedure parameter ("RETURN_VALUE") ONLY when I have explicitly close the
> DB connection. Unfortunately when the control is returned to the calling
> method the usual error message
>
> Invalid attempt to Read when reader is closed
>
> is being received as the connection is closed and there's no such
DataReader
> already.
>
> Does anyone know a workaround for this?
>
> Thanks,
> Martin
>
> --
> private SqlDataReader GetReader(string parameter, string date)
> {
> DateTime MyDateTime;
> ....//. date parsing
>
> SqlConnection myConn = new SqlConnection(ConnectionString());
> SqlCommand myCmd = new SqlCommand();
> SqlDataReader myReader=null;
>
> myCmd.CommandType = CommandType.StoredProcedure;
> myCmd.Connection = myConn;
> myCmd.CommandText = "GetData";
> myCmd.CommandTimeout = 250;
> SqlParameter Param1 = new SqlParameter();
> Param1 = myCmd.Parameters.Add("@.parameter", SqlDbType.VarChar, 12);
> Param1.Direction = ParameterDirection.Input;
> Param1.Value = parameter;
>
> SqlParameter Param2 = new SqlParameter();
> Param2 = myCmd.Parameters.Add("@.date", SqlDbType.VarChar, 20);
> Param2.Direction = ParameterDirection.Input;
> Param2.Value = MyDateTime.Date.ToShortDateString();
>
> SqlParameter outValue = new SqlParameter();
> outValue = myCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
> outValue.Direction = ParameterDirection.ReturnValue;
> myConn.Open();
> myReader = myCmd.ExecuteReader();
>
> // this works only is myConn.Close() is executed but then
> // we cannot return a READER to the calling method?
> intRowsReturned =
> Convert.ToInt32(myCmd.Parameters["RETURN_VALUE"].Value);
> return myReader;
> }
>
> // the calling method
> private void btnSQLGet_Click(object sender, System.EventArgs e)
> {
> /// ... .
> // get the data in a reader
> SqlDataReader myReader = GetReader(cboParameter.Text, txtDate.Text);
> if (myReader==null)
> return;
> ///....
> }
>
>
1) the return value and parameters values of a stored proc are returned
after all result sets in the proc are returned.
2) a datareader is a forward only cursor, so to get to the return value, you
need to read thru all rows, and result sets
3) a close will read thru all rows and result sets for you, but is not
necessary, the following code will also work:
// process all result sets to get to parameters and reutn value
do
{
while (dr.Read())
;
} while (dr.NextResult())
// now you can access output parameters and the return value
returning a row count makes little sesnse, as you could add up the row
count yourself, as you have to read them all to get to the count return
value.
-- bruce (sqlwork.com)
"Martin Raychev" <marty_gov@.hotmail.com> wrote in message
news:#RLAe#IKEHA.1892@.TK2MSFTNGP09.phx.gbl...
> Hi all,
>
> I have the following problem:
>
> I have a private method that returns a SqlDataReader. For this to work I
> have not to close the DB connection in the above method. I do this only to
> have the possibility to iterate through the entire rows set in a while
loop,
> located in the calling method.
>
> I have included a few lines of code to get the number of rows fetched from
> the DB. I do this with SqlParameter("RETURN_VALUE", SqlDbType.Int) [ I
am
> using a stored procedure that return @.@.rowcount].
>
> I have found out that I am getting the appropriate value for the stored
> procedure parameter ("RETURN_VALUE") ONLY when I have explicitly close the
> DB connection. Unfortunately when the control is returned to the calling
> method the usual error message
>
> Invalid attempt to Read when reader is closed
>
> is being received as the connection is closed and there's no such
DataReader
> already.
>
> Does anyone know a workaround for this?
>
> Thanks,
> Martin
>
> --
> private SqlDataReader GetReader(string parameter, string date)
> {
> DateTime MyDateTime;
> ....//. date parsing
>
> SqlConnection myConn = new SqlConnection(ConnectionString());
> SqlCommand myCmd = new SqlCommand();
> SqlDataReader myReader=null;
>
> myCmd.CommandType = CommandType.StoredProcedure;
> myCmd.Connection = myConn;
> myCmd.CommandText = "GetData";
> myCmd.CommandTimeout = 250;
> SqlParameter Param1 = new SqlParameter();
> Param1 = myCmd.Parameters.Add("@.parameter", SqlDbType.VarChar, 12);
> Param1.Direction = ParameterDirection.Input;
> Param1.Value = parameter;
>
> SqlParameter Param2 = new SqlParameter();
> Param2 = myCmd.Parameters.Add("@.date", SqlDbType.VarChar, 20);
> Param2.Direction = ParameterDirection.Input;
> Param2.Value = MyDateTime.Date.ToShortDateString();
>
> SqlParameter outValue = new SqlParameter();
> outValue = myCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
> outValue.Direction = ParameterDirection.ReturnValue;
> myConn.Open();
> myReader = myCmd.ExecuteReader();
>
> // this works only is myConn.Close() is executed but then
> // we cannot return a READER to the calling method?
> intRowsReturned =
> Convert.ToInt32(myCmd.Parameters["RETURN_VALUE"].Value);
> return myReader;
> }
>
> // the calling method
> private void btnSQLGet_Click(object sender, System.EventArgs e)
> {
> /// ... .
> // get the data in a reader
> SqlDataReader myReader = GetReader(cboParameter.Text, txtDate.Text);
> if (myReader==null)
> return;
> ///....
> }
>
>