Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

Thursday, March 29, 2012

Pass Parameter to DataList Datasource!

Hi all,

I am trying pass a value to the DataSource method in a DataList like below.
DataSource = <%# GetData(DataBinder.Eval(Container.DataItem, "ID"))%>"

I get an error with this code, and am wondering if someone can show me the
correct syntax.

I have asked this question before, but unfortunately i don't remember the
correct syntax.

I know i can acheive the desired result using code behind 'ItemDataBound',
but am prefering to do it in the aspx page itself..

All help appreciated.

<asp:DataList ID="dlTest"
DataKeyField = "ID"
DataSource = <%# GetData(DataBinder.Eval(Container.DataItem,
"ID"))%>">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem,
"Name")%></ItemTemplate>
</asp:DataList>

Cheers,
AdamDataSource should be set to an on object containing data items. What is it
in your case?

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"AJ" <AJ@.discussions.microsoft.comwrote in message
news:3C74BA0B-76DB-42B0-A32B-E2EF219A44BC@.microsoft.com...

Quote:

Originally Posted by

Hi all,
>
I am trying pass a value to the DataSource method in a DataList like
below.
DataSource = <%# GetData(DataBinder.Eval(Container.DataItem, "ID"))%>"
>
I get an error with this code, and am wondering if someone can show me the
correct syntax.
>
I have asked this question before, but unfortunately i don't remember the
correct syntax.
>
I know i can acheive the desired result using code behind 'ItemDataBound',
but am prefering to do it in the aspx page itself..
>
All help appreciated.
>
<asp:DataList ID="dlTest"
DataKeyField = "ID"
DataSource = <%# GetData(DataBinder.Eval(Container.DataItem,
"ID"))%>">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem,
"Name")%></ItemTemplate>
</asp:DataList>
>
Cheers,
Adam

Monday, March 26, 2012

Pass values

Hi,
How to pass values from one to another page using post method? And how to retrieve them at another page?

ASP.NET uses by default POST method, so what is your question?

Regards


You can use a querystring through the URL, the session object, application object, Server.Transfer().

Depending on what you need, those are a few of the ways to maintain state off the top of my head.
In old asp 3.0. I'd use something like <form action="page2.asp"...>
Then In page two I'd use request to retrieve values.
How to achieve this with asp.net?

"In old asp 3.0. I'd use something like <form action="page2.asp"...>
Then In page two I'd use request to retrieve values."
In .Net you don't post do a different page, by default it post's back to itself. All web controls that have viewstate enabled will have their values stored during the postback and be accessable when the page post backs to itself.
Perhaps if you give us a specific example of what you're trying to accomplish it'd be a bit easier to help you specifically where you need it?
Lets say I want to have simple search funcionality. Search string whentyped in any page, should be passed to results.aspx page. There shouldexact searchs occur and results be displayed.
So...?

In c# code you should do something like this:

Response.Redirect("Results.aspx?searchString="+searchBox.Text+); //put a parameter in the redirect page

Note that the "searchString" is the name of the parameter you areadding to the redirect, you can call it whatever you want. ThesearchBox.Text is the TextBox of search.

Now in the page you've redirected, you will extract the parameter

string textSearch = Response.QueryString["searchString"];

Now that I'm here, how did you develop your search engine?Do you checkevery table in your database and return every values that match thesearch?If you have news, studies in your database you show for examplenews and then the news related, and then the studies and then therelated studies for example?

Thanks, hope I did help!

Hi,

If you have html page with search form, for example, you can add <form action="Result.aspx" type="post"> in it then you can get all post variables using Request.Form[variableName]. In case you use form as aspx page you do not need to transfer to other page - you can and should process all events (buttons clicks etc) right in this page. It's common for asp.net and it's recommend to use this technique.

But if you need to transfer post data to other aspx page you can use Server.Transfer() method in, for instance, button click event. Then you can get all post variables including controls state from previous aspx page using Request.Form.

In asp.net 2 you can also use cross page postback (read more about it therehttp://msdn2.microsoft.com/en-us/library/ms178139(vs.80).aspx).


I'm have the same problem. How can I get the Query String in VB?

do server.transfer.

Let me give you an example.

-- default page --<form runat="server"> ... <asp:textbox id="text1" runat="server" text="value1here"/> <asp:textbox id="text2" runat="server" text="value2here"/> ... <asp:button id="btn1" runat="server" text="button1"/> <asp:button id="btn2" runat="server" text="button2"/> ...</form>-- this is the btn1 onclick behavior you want to go page1.aspx right? --Protected Sub btn1_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles btn1.Click server.transfer("page1.aspx", true)end sub-- this is the way to show text1 at page1.aspx --Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load label1.text = request.form("text1")end sub

that's it. you can pass the values within http request, because when you click buttons, the values gets saved to http request via postback. alll you are doing is passing values in http request in next page. form submission is done for you automatically.

improvise and you'll succeed.

Hope this helps and let me know what happens.

Jae.


Great but what if iis in a datalist? I can't get the value out.

you can declare a hidden field:

 <asp:DataList ID="datalist1" runat="Server"> <ItemTemplate> <asp:HiddenField ID="hidden" Value='<%#eval("valuetopass")%>'> </ItemTemplate> </asp:DataList>

user server.transfer afterwards then retrieve value by:

request.form("hidden")
Hope this helps.
Jae.

If you are using .NET 2.0, you can set the postBackURL on the form. This will allow your page to directly post to your new page.

Nick


Do you know how to do this in VB.net?

Sorry, I got it

Request.QueryString(

"Variable")

Thanks for the help.Big Smile

Saturday, March 24, 2012

Pass variable to another page

I have a calendar form that sends the inputted date back to a text box .
Using the GET method, I would like to pass the date as a variable to another
asp page. How do I do this?
Thanks.Hi Marti,
Not sure if I understand your question. But in ASP.Net 2.0 you have to
ability to do cross-page positing. So you can just do a postback with
value of that textbox to your other page.
More info:
http://www.dotnetjohn.com/articles.aspx?articleid=176
http://msdn2.microsoft.com/en-us/library/ms178139.aspx
Ward Bekker
"Asp.Net Discussions for the Professional Developer"
http://www.dotnettaxi.com
"Free .Net 2.0 C# to/from VB.Net Code Converter"
http://www.dotnettaxi.com/Tools/Converter.aspx
Marti wrote:
> I have a calendar form that sends the inputted date back to a text box .
> Using the GET method, I would like to pass the date as a variable to anoth
er
> asp page. How do I do this?
> Thanks.
>
Ward,
Thanks - That's exactly what I was looking for. I was using a different
method to pass values. I have tried using the postback method but to no
avail. Below is the source page I am working with, can you tell me what I
should have on my target page?
Thanks.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Cross Page PostBack</title>
</head>
<body>
<form name="form1" id="form1" runat="server">
<h3>Cross Page PostBack</h3>
Enter Search Term:
<asp:TextBox ID="SearchTerm" runat="server" />
<asp:Button ID="Button2" runat="server" Text="Search"
PostBackUrl="posttarget.aspx" />
<br />
</form>
</body>
</html>
"Ward Bekker" <ward@.NOequanimitySPAM.nl> wrote in message
news:4405fc87$0$2027$9a622dc7@.news.kpnplanet.nl...
Hi Marti,
Not sure if I understand your question. But in ASP.Net 2.0 you have to
ability to do cross-page positing. So you can just do a postback with
value of that textbox to your other page.
More info:
http://www.dotnetjohn.com/articles.aspx?articleid=176
http://msdn2.microsoft.com/en-us/library/ms178139.aspx
Ward Bekker
"Asp.Net Discussions for the Professional Developer"
http://www.dotnettaxi.com
"Free .Net 2.0 C# to/from VB.Net Code Converter"
http://www.dotnettaxi.com/Tools/Converter.aspx
Marti wrote:
> I have a calendar form that sends the inputted date back to a text box .
> Using the GET method, I would like to pass the date as a variable to
another
> asp page. How do I do this?
> Thanks.
>
Ok, I think I know out the code isn't working. Don't have 2.0 installed.
I'll install the 2.0 framework and give the crosspage post back a try..
"Martin" <bmquiroz@.email.com> wrote in message
news:%23ItZRAYPGHA.3460@.TK2MSFTNGP15.phx.gbl...
Ward,
Thanks - That's exactly what I was looking for. I was using a different
method to pass values. I have tried using the postback method but to no
avail. Below is the source page I am working with, can you tell me what I
should have on my target page?
Thanks.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Cross Page PostBack</title>
</head>
<body>
<form name="form1" id="form1" runat="server">
<h3>Cross Page PostBack</h3>
Enter Search Term:
<asp:TextBox ID="SearchTerm" runat="server" />
<asp:Button ID="Button2" runat="server" Text="Search"
PostBackUrl="posttarget.aspx" />
<br />
</form>
</body>
</html>
"Ward Bekker" <ward@.NOequanimitySPAM.nl> wrote in message
news:4405fc87$0$2027$9a622dc7@.news.kpnplanet.nl...
Hi Marti,
Not sure if I understand your question. But in ASP.Net 2.0 you have to
ability to do cross-page positing. So you can just do a postback with
value of that textbox to your other page.
More info:
http://www.dotnetjohn.com/articles.aspx?articleid=176
http://msdn2.microsoft.com/en-us/library/ms178139.aspx
Ward Bekker
"Asp.Net Discussions for the Professional Developer"
http://www.dotnettaxi.com
"Free .Net 2.0 C# to/from VB.Net Code Converter"
http://www.dotnettaxi.com/Tools/Converter.aspx
Marti wrote:
> I have a calendar form that sends the inputted date back to a text box .
> Using the GET method, I would like to pass the date as a variable to
another
> asp page. How do I do this?
> Thanks.
>

Pass variable to another page

I have a calendar form that sends the inputted date back to a text box .
Using the GET method, I would like to pass the date as a variable to another
asp page. How do I do this?

Thanks.Hi Marti,

Not sure if I understand your question. But in ASP.Net 2.0 you have to
ability to do cross-page positing. So you can just do a postback with
value of that textbox to your other page.

More info:
http://www.dotnetjohn.com/articles.aspx?articleid=176
http://msdn2.microsoft.com/en-us/library/ms178139.aspx

--
Ward Bekker
"Asp.Net Discussions for the Professional Developer"
http://www.dotnettaxi.com

"Free .Net 2.0 C# to/from VB.Net Code Converter"
http://www.dotnettaxi.com/Tools/Converter.aspx

Marti wrote:
> I have a calendar form that sends the inputted date back to a text box .
> Using the GET method, I would like to pass the date as a variable to another
> asp page. How do I do this?
> Thanks.
Ward,

Thanks - That's exactly what I was looking for. I was using a different
method to pass values. I have tried using the postback method but to no
avail. Below is the source page I am working with, can you tell me what I
should have on my target page?

Thanks.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Cross Page PostBack</title>
</head
<body>
<form name="form1" id="form1" runat="server">
<h3>Cross Page PostBack</h3>
Enter Search Term:
<asp:TextBox ID="SearchTerm" runat="server" />
<asp:Button ID="Button2" runat="server" Text="Search"
PostBackUrl="posttarget.aspx" />
<br />
</form>
</body>
</html
"Ward Bekker" <ward@.NOequanimitySPAM.nl> wrote in message
news:4405fc87$0$2027$9a622dc7@.news.kpnplanet.nl...
Hi Marti,

Not sure if I understand your question. But in ASP.Net 2.0 you have to
ability to do cross-page positing. So you can just do a postback with
value of that textbox to your other page.

More info:
http://www.dotnetjohn.com/articles.aspx?articleid=176
http://msdn2.microsoft.com/en-us/library/ms178139.aspx

--
Ward Bekker
"Asp.Net Discussions for the Professional Developer"
http://www.dotnettaxi.com

"Free .Net 2.0 C# to/from VB.Net Code Converter"
http://www.dotnettaxi.com/Tools/Converter.aspx

Marti wrote:
> I have a calendar form that sends the inputted date back to a text box .
> Using the GET method, I would like to pass the date as a variable to
another
> asp page. How do I do this?
> Thanks.
Ok, I think I know out the code isn't working. Don't have 2.0 installed.
I'll install the 2.0 framework and give the crosspage post back a try..

"Martin" <bmquiroz@.email.com> wrote in message
news:%23ItZRAYPGHA.3460@.TK2MSFTNGP15.phx.gbl...
Ward,

Thanks - That's exactly what I was looking for. I was using a different
method to pass values. I have tried using the postback method but to no
avail. Below is the source page I am working with, can you tell me what I
should have on my target page?

Thanks.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Cross Page PostBack</title>
</head
<body>
<form name="form1" id="form1" runat="server">
<h3>Cross Page PostBack</h3>
Enter Search Term:
<asp:TextBox ID="SearchTerm" runat="server" />
<asp:Button ID="Button2" runat="server" Text="Search"
PostBackUrl="posttarget.aspx" />
<br />
</form>
</body>
</html
"Ward Bekker" <ward@.NOequanimitySPAM.nl> wrote in message
news:4405fc87$0$2027$9a622dc7@.news.kpnplanet.nl...
Hi Marti,

Not sure if I understand your question. But in ASP.Net 2.0 you have to
ability to do cross-page positing. So you can just do a postback with
value of that textbox to your other page.

More info:
http://www.dotnetjohn.com/articles.aspx?articleid=176
http://msdn2.microsoft.com/en-us/library/ms178139.aspx

--
Ward Bekker
"Asp.Net Discussions for the Professional Developer"
http://www.dotnettaxi.com

"Free .Net 2.0 C# to/from VB.Net Code Converter"
http://www.dotnettaxi.com/Tools/Converter.aspx

Marti wrote:
> I have a calendar form that sends the inputted date back to a text box .
> Using the GET method, I would like to pass the date as a variable to
another
> asp page. How do I do this?
> Thanks.

Pass Variables

What is the best method of passing variables from one page to another?I don't know that there really is a 'best' way - - it's just whichever way you are most comfortable with..

generally, I still use querystrings, though, with ASP.Net, I generally now use only one page to do processing, so I don't need to do cross-page programming as much
What do you feel is the best way to pass arrays from page to page?

Trey

Pass Web FileObject to a class

All
I have a file HTML object, I want to pass this object to a class so I can wr
ite a common method for uploading files.
Idea is to avoid coding file saving in the forms submit button, but call a m
ethod that can process the file that just got submitted.
Is this possible at all? Any idea on how to handle this issue?
TIA"kaushas" <kaushas@.discussions.microsoft.com> wrote in message
news:95A6B7BA-F2D9-4B2B-B60F-4BAA7EF1A4ED@.microsoft.com...
> All
> I have a file HTML object, I want to pass this object to a class so I can
write a common method for uploading files.
> Idea is to avoid coding file saving in the forms submit button, but call a
method that can process the file that just got submitted.
> Is this possible at all? Any idea on how to handle this issue?
Do you mean that you want to pass the "file HTML object" to a method of a
class? Just pass it. What problem are you having when you try to do this?
Can you post some code that reproduces the problem?
--
John Saunders
johnwsaundersiii at hotmail
Sorry
I should have been more specific. I was looking for the object type that I n
eeded to define in the method signature... and then I answered my own quest
ion -> it has to be the HtmlInputFile type that needs to be passed to the me
thod... duh
thanks
"John Saunders" wrote:

> "kaushas" <kaushas@.discussions.microsoft.com> wrote in message
> news:95A6B7BA-F2D9-4B2B-B60F-4BAA7EF1A4ED@.microsoft.com...
> write a common method for uploading files.
> method that can process the file that just got submitted.
> Do you mean that you want to pass the "file HTML object" to a method of a
> class? Just pass it. What problem are you having when you try to do this?
> Can you post some code that reproduces the problem?
> --
> John Saunders
> johnwsaundersiii at hotmail
>
>

Pass Web FileObject to a class

All
I have a file HTML object, I want to pass this object to a class so I can write a common method for uploading files.
Idea is to avoid coding file saving in the forms submit button, but call a method that can process the file that just got submitted.

Is this possible at all? Any idea on how to handle this issue?

TIA"kaushas" <kaushas@.discussions.microsoft.com> wrote in message
news:95A6B7BA-F2D9-4B2B-B60F-4BAA7EF1A4ED@.microsoft.com...
> All
> I have a file HTML object, I want to pass this object to a class so I can
write a common method for uploading files.
> Idea is to avoid coding file saving in the forms submit button, but call a
method that can process the file that just got submitted.
> Is this possible at all? Any idea on how to handle this issue?

Do you mean that you want to pass the "file HTML object" to a method of a
class? Just pass it. What problem are you having when you try to do this?
Can you post some code that reproduces the problem?
--
John Saunders
johnwsaundersiii at hotmail
Sorry
I should have been more specific. I was looking for the object type that I needed to define in the method signature... and then I answered my own question -> it has to be the HtmlInputFile type that needs to be passed to the method... duh

thanks
"John Saunders" wrote:

> "kaushas" <kaushas@.discussions.microsoft.com> wrote in message
> news:95A6B7BA-F2D9-4B2B-B60F-4BAA7EF1A4ED@.microsoft.com...
> > All
> > I have a file HTML object, I want to pass this object to a class so I can
> write a common method for uploading files.
> > Idea is to avoid coding file saving in the forms submit button, but call a
> method that can process the file that just got submitted.
> > Is this possible at all? Any idea on how to handle this issue?
> Do you mean that you want to pass the "file HTML object" to a method of a
> class? Just pass it. What problem are you having when you try to do this?
> Can you post some code that reproduces the problem?
> --
> John Saunders
> johnwsaundersiii at hotmail
>

Passing 2 Parameters from Datalist Control?

I have master/detail pages and pass a parameter from master page (datalist control) to detail page using session method.

Session.Add(

"prm_type", DataList1.SelectedValue);
Response.Redirect("detail.aspx");

The code, I had placed in the datalist "selectedindexchanged" method, above works well. In addition to this, I placed a textbox control in the datalist control to pass a second parameter to detail page. But i don't know how to reach that textbox value and pass the parameter to detail page within datalist selectedindexchanged method like above. I tried something like below, but i couldn't figure out.

Session.Add("prm_quantity", ? this must be my textbox control value);

How can i pass a second parameter like this?

Try with this:

TextBox tbText = (TextBox)DataList1.FindControl("TextBox1");
Session.Add("prm_quantity", tbText.Text);


I got an error message on this line:

Session.Add(

"prm_quantity", tbText.Text);

"NullReferenceException was unhandled by user code"
"Object reference not set to an instance of an object"


Hi,

It may be cache issue. Please look at this KB:

http://www.kbalertz.com/Feedback_831382.aspx

Good luck!

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;
> ///....
> }
>
>

Passing a value from a class method to a function in start page and back to class


Hi,
I have an a one dimensional generated in one of the function of a class which I developed. I need to know how to perform the following:
1) Pass the value to the start page from the class method (this part is done).
2) Pass the value back again to another method of the same class from the start page.

Sounds like you'll need some kind of event to fire from the start page like a button click or a javascript event like a onmouseover or some other trigger to initiate the pass. What are you trying to do exactly?

Friday, March 16, 2012

Passing a variable to another page

How can I navigate to another page via a button, and pass a variable to this
next page...
How can I do this via that method that I see in other sites that the url
goes like this:
http://www.google.com.br/search?hl=pt-BR
[]s...Look up the concept of querystrings
Regards
John Timney
ASP.NET MVP
Microsoft Regional Director
"Ricardo" <r_luceac@.hotmail.com> wrote in message
news:%23IPFidjDFHA.2632@.TK2MSFTNGP12.phx.gbl...
> How can I navigate to another page via a button, and pass a variable to
this
> next page...
> How can I do this via that method that I see in other sites that the url
> goes like this:
> http://www.google.com.br/search?hl=pt-BR
>
> []s...
>
Here's a nice, simple way to pass values from one page to another:
(VB.NET code)
'Add data to the context object before transferring
Context.Items("myParameter") = x
Server.Transfer("WebForm2.aspx")
Then, in WebForm2.aspx:
'Grab data from the context property
Dim x as Integer = CType(Context.Items("myParameter"),Integer)
Of course there are a number of ways to pass values from one page to
another, such as using the querystring, cookies, session,
context, saving to a temporary table in the database between each page, etc.
You'll have to decide which technique is best for your application.
Here are several good articles on the subject to help you decide.
http://msdn.microsoft.com/msdnmag/i...te/default.aspx
http://www.aspalliance.com/kenc/passval.aspx
http://www.dotnetbips.com/displayarticle.aspx?id=79
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Ricardo" <r_luceac@.hotmail.com> wrote in message
news:%23IPFidjDFHA.2632@.TK2MSFTNGP12.phx.gbl...
> How can I navigate to another page via a button, and pass a variable to
> this
> next page...
> How can I do this via that method that I see in other sites that the url
> goes like this:
> http://www.google.com.br/search?hl=pt-BR
>
> []s...
>

Passing a variable to another page

How can I navigate to another page via a button, and pass a variable to this
next page...

How can I do this via that method that I see in other sites that the url
goes like this:

http://www.google.com.br/search?hl=pt-BR

[]s...Look up the concept of querystrings

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Ricardo" <r_luceac@.hotmail.com> wrote in message
news:%23IPFidjDFHA.2632@.TK2MSFTNGP12.phx.gbl...
> How can I navigate to another page via a button, and pass a variable to
this
> next page...
> How can I do this via that method that I see in other sites that the url
> goes like this:
> http://www.google.com.br/search?hl=pt-BR
>
> []s...
Here's a nice, simple way to pass values from one page to another:
(VB.NET code)

'Add data to the context object before transferring
Context.Items("myParameter") = x
Server.Transfer("WebForm2.aspx")

Then, in WebForm2.aspx:

'Grab data from the context property
Dim x as Integer = CType(Context.Items("myParameter"),Integer)

Of course there are a number of ways to pass values from one page to
another, such as using the querystring, cookies, session,
context, saving to a temporary table in the database between each page, etc.
You'll have to decide which technique is best for your application.
Here are several good articles on the subject to help you decide.
http://msdn.microsoft.com/msdnmag/i...te/default.aspx

http://www.aspalliance.com/kenc/passval.aspx

http://www.dotnetbips.com/displayarticle.aspx?id=79

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"Ricardo" <r_luceac@.hotmail.com> wrote in message
news:%23IPFidjDFHA.2632@.TK2MSFTNGP12.phx.gbl...
> How can I navigate to another page via a button, and pass a variable to
> this
> next page...
> How can I do this via that method that I see in other sites that the url
> goes like this:
> http://www.google.com.br/search?hl=pt-BR
>
> []s...