Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Saturday, March 31, 2012

Session URL Formatting

Hello,

I have a problem with ASP.Net:

I have an ASP.Net application on a web farm, using SQL Server Session
State Management, running on Windows 2000 (IIS 5)
With session management on, a URL like the following is created:
https://mywebapp.com/(ts2t2uivrj3vx...cvg)/start.aspx
The problem I am having is that at some point within the web application
I have to pass this link to an outside server, which in turn,
will do some work and then post back to my application. However, the
outside application is URL Encoding my URL, which in turn,
causes the URL to look like:
mywebapp.com/(suyvga3bcnzwrljiaxdfud30)/vbvnet.aspx
This cannot be handled by IIS, and induces a "page cannot be displayed
error"; which in effect, causes the application to terminate.

Is there any way that we can trap this error and redirect to the appropriate
location so that the web application does terminate, or is there a way
we can prevent the brackets within this application.

Hope I explained this correctly.

Appreciate any help.

Regards,

Malcolm KlotzOne approach - not clean - you can catch the IIS error code 404 and then
handle the issue on a redirect page. Sorry, I can't think of anything
cleaner at the moment.

--
Regards,
Alvin Bruney - ASP.NET MVP

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @. www.lulu.com/owc, Amazon.com etc
"Malcolm Klotz" <nonesuch23@.online.nospam> wrote in message
news:OvlaLxATFHA.2812@.TK2MSFTNGP09.phx.gbl...
> Hello,
> I have a problem with ASP.Net:
> I have an ASP.Net application on a web farm, using SQL Server Session
> State Management, running on Windows 2000 (IIS 5)
> With session management on, a URL like the following is created:
> https://mywebapp.com/(ts2t2uivrj3vx...cvg)/start.aspx
> The problem I am having is that at some point within the web
> application
> I have to pass this link to an outside server, which in turn,
> will do some work and then post back to my application. However, the
> outside application is URL Encoding my URL, which in turn,
> causes the URL to look like:
> mywebapp.com/(suyvga3bcnzwrljiaxdfud30)/vbvnet.aspx
> This cannot be handled by IIS, and induces a "page cannot be displayed
> error"; which in effect, causes the application to terminate.
> Is there any way that we can trap this error and redirect to the
> appropriate
> location so that the web application does terminate, or is there a way
> we can prevent the brackets within this application.
> Hope I explained this correctly.
> Appreciate any help.
> Regards,
> Malcolm Klotz
Thanks for Alvin's inputs,

Hi Malcolm,

Since you are using the cookieless Session(embed sessionid in the url
string) in your web app, is there any particular requirement in your
scenario that we must use cookieless session? Is it possbile that we use
cookieenabled session instread?

Also, as for passing the url to a outside server, I think we have the
following means:

1. If we are forced to use cookieless session and with the sessionid
embeded in the url, I suggest that we filter the sessionid when pass the
url out to the remote server. For example:

when giving the following url

https://mywebapp.com/(ts2t2uivrj3vx...cvg)/start.aspx

we can use substring concating or regex to get the url without embeded
sessionid like:

https://mywebapp.com/start.aspx

this can also work for the remote requster, the only difference is that the
request with non-sessionid url will be unable to retrieve the data in the
SessionState associated with the id. Do you think this means possbile?

2. If we also want to let the remote server be able to retrieve the
sessiondata, I suggest that we still pass the urlstring without embeded
sessionid , but also pass the SessionId string as separate data to that
remote server together with the url string. How do you think of this?

Please feel free to post here if you have any other questions or ideas.
Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Thank you, you have given me a place to start.

"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> wrote in message
news:eSJz3kGTFHA.2476@.TK2MSFTNGXA01.phx.gbl...
> Thanks for Alvin's inputs,
> Hi Malcolm,
> Since you are using the cookieless Session(embed sessionid in the url
> string) in your web app, is there any particular requirement in your
> scenario that we must use cookieless session? Is it possbile that we use
> cookieenabled session instread?
> Also, as for passing the url to a outside server, I think we have the
> following means:
> 1. If we are forced to use cookieless session and with the sessionid
> embeded in the url, I suggest that we filter the sessionid when pass the
> url out to the remote server. For example:
> when giving the following url
> https://mywebapp.com/(ts2t2uivrj3vx...cvg)/start.aspx
> we can use substring concating or regex to get the url without embeded
> sessionid like:
> https://mywebapp.com/start.aspx
> this can also work for the remote requster, the only difference is that
the
> request with non-sessionid url will be unable to retrieve the data in the
> SessionState associated with the id. Do you think this means possbile?
> 2. If we also want to let the remote server be able to retrieve the
> sessiondata, I suggest that we still pass the urlstring without embeded
> sessionid , but also pass the SessionId string as separate data to that
> remote server together with the url string. How do you think of this?
> Please feel free to post here if you have any other questions or ideas.
> Thanks,
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
You're welcome Malcolm,

Good luck! :-)

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Thursday, March 29, 2012

Session URL Formatting

Hello,
I have a problem with ASP.Net:
I have an ASP.Net application on a web farm, using SQL Server Session
State Management, running on Windows 2000 (IIS 5)
With session management on, a URL like the following is created:
https://mywebapp.com/(ts2t2uivrj3vxg452ah33cvg)/start.aspx
The problem I am having is that at some point within the web application
I have to pass this link to an outside server, which in turn,
will do some work and then post back to my application. However, the
outside application is URL Encoding my URL, which in turn,
causes the URL to look like:
mywebapp.com/(suyvga3bcnzwrljiaxdfud30)/vbvnet.aspx
This cannot be handled by IIS, and induces a "page cannot be displayed
error"; which in effect, causes the application to terminate.
Is there any way that we can trap this error and redirect to the appropriate
location so that the web application does terminate, or is there a way
we can prevent the brackets within this application.
Hope I explained this correctly.
Appreciate any help.
Regards,
Malcolm KlotzOne approach - not clean - you can catch the IIS error code 404 and then
handle the issue on a redirect page. Sorry, I can't think of anything
cleaner at the moment.
Regards,
Alvin Bruney - ASP.NET MVP
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @. www.lulu.com/owc, Amazon.com etc
"Malcolm Klotz" <nonesuch23@.online.nospam> wrote in message
news:OvlaLxATFHA.2812@.TK2MSFTNGP09.phx.gbl...
> Hello,
> I have a problem with ASP.Net:
> I have an ASP.Net application on a web farm, using SQL Server Session
> State Management, running on Windows 2000 (IIS 5)
> With session management on, a URL like the following is created:
> https://mywebapp.com/(ts2t2uivrj3vxg452ah33cvg)/start.aspx
> The problem I am having is that at some point within the web
> application
> I have to pass this link to an outside server, which in turn,
> will do some work and then post back to my application. However, the
> outside application is URL Encoding my URL, which in turn,
> causes the URL to look like:
> mywebapp.com/(suyvga3bcnzwrljiaxdfud30)/vbvnet.aspx
> This cannot be handled by IIS, and induces a "page cannot be displayed
> error"; which in effect, causes the application to terminate.
> Is there any way that we can trap this error and redirect to the
> appropriate
> location so that the web application does terminate, or is there a way
> we can prevent the brackets within this application.
> Hope I explained this correctly.
> Appreciate any help.
> Regards,
> Malcolm Klotz
>
Thanks for Alvin's inputs,
Hi Malcolm,
Since you are using the cookieless Session(embed sessionid in the url
string) in your web app, is there any particular requirement in your
scenario that we must use cookieless session? Is it possbile that we use
cookieenabled session instread?
Also, as for passing the url to a outside server, I think we have the
following means:
1. If we are forced to use cookieless session and with the sessionid
embeded in the url, I suggest that we filter the sessionid when pass the
url out to the remote server. For example:
when giving the following url
https://mywebapp.com/(ts2t2uivrj3vxg452ah33cvg)/start.aspx
we can use substring concating or regex to get the url without embeded
sessionid like:
https://mywebapp.com/start.aspx
this can also work for the remote requster, the only difference is that the
request with non-sessionid url will be unable to retrieve the data in the
SessionState associated with the id. Do you think this means possbile?
2. If we also want to let the remote server be able to retrieve the
sessiondata, I suggest that we still pass the urlstring without embeded
sessionid , but also pass the SessionId string as separate data to that
remote server together with the url string. How do you think of this?
Please feel free to post here if you have any other questions or ideas.
Thanks,
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Thank you, you have given me a place to start.
"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> wrote in message
news:eSJz3kGTFHA.2476@.TK2MSFTNGXA01.phx.gbl...
> Thanks for Alvin's inputs,
> Hi Malcolm,
> Since you are using the cookieless Session(embed sessionid in the url
> string) in your web app, is there any particular requirement in your
> scenario that we must use cookieless session? Is it possbile that we use
> cookieenabled session instread?
> Also, as for passing the url to a outside server, I think we have the
> following means:
> 1. If we are forced to use cookieless session and with the sessionid
> embeded in the url, I suggest that we filter the sessionid when pass the
> url out to the remote server. For example:
> when giving the following url
> https://mywebapp.com/(ts2t2uivrj3vxg452ah33cvg)/start.aspx
> we can use substring concating or regex to get the url without embeded
> sessionid like:
> https://mywebapp.com/start.aspx
> this can also work for the remote requster, the only difference is that
the
> request with non-sessionid url will be unable to retrieve the data in the
> SessionState associated with the id. Do you think this means possbile?
> 2. If we also want to let the remote server be able to retrieve the
> sessiondata, I suggest that we still pass the urlstring without embeded
> sessionid , but also pass the SessionId string as separate data to that
> remote server together with the url string. How do you think of this?
> Please feel free to post here if you have any other questions or ideas.
> Thanks,
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
You're welcome Malcolm,
Good luck! :-)
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Saturday, March 24, 2012

Session Variable question

Hi everyone,

I am trying to extract data out of a SQL Server database (data looks like this:http://svrxblcyadnet02/crimp/CRIMP_Report_Frame.asp?) and plug it into a session variable. Below is the code I've come up with so far. My question is this: How do you get the data from the Dataset to the session variable? Thanks

privatevoid GetData()

{

string ConnectionString = "server=MyServer;uid=Sphynx;pwd=letmein;database=MyDatabase";

string CommandText = "select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM = 'CRIMP_strName'";

SqlConnection myConnection =new SqlConnection(ConnectionString);

SqlDataAdapter myCommand =new SqlDataAdapter(CommandText, myConnection);

DataSet ds =new DataSet();

myCommand.Fill(ds);

Session["varString"] = "??"; //? What goes here?

myConnection.Close();

}

Ds.Tables(0).Rows(0)("ordnum")

Session can hold any object. You can store your dataset this way!

Session["myData"] = ds;

When you try to access the data, you need to do this

if (Session["myData"] != null)
DataSet ds = (DataSet) Session["myData"];

If you want store the values of the dataset, you use

ds.Tables[0].Rows[0][columnno]

HTH


Well almost any object. Depends if the Session is InProc or stored in the state server or SQL Server. In which case the the object would have to be serializable. It is also worth mentioning that DataSets can be stored in Session while DataRows can't.


batman++:


Depends if the Session is InProc or stored in the state server or SQL Server. In which case the the object would have to be serializable.

Thanks batman! I missed that :)


I get the following error then I run it:

Object reference not set to an instance of an object.

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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 41: Line 42:Line 43: Session["varString"] = ds.Tables["CRIMP_Common_Lookup"].Rows[0][2].ToString();Line 44: myConnection.Close();Line 45:


The error indicates that there is nothing in the 3rd column of the first row of the table CRIMP_Common_Lookup. Without the code used to populate the DataSet, all I can tell you is that either the table doesn't exist OR there are no rows in that table OR the value in the column is null. If you are using vs2005 start debugging, set a break point on a line just before the error and use the DataSet visualizer to check the contents. Otherwise the you can just add an if clause to check for null before adding it to Session.

Sorry, I see your SQL now.

select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM = 'CRIMP_strName'";

Make sure that LOOKUP_DS is the 3rd column in the table. You can also use Rows[0]["LOOKUP_DS"].ToString().

Also you might want to set the table name in the Fill() method.

myCommand.Fill(ds, "CRIMP_common_lookup");

C# is case sensative even though sql usually is not.


Thanks guys,

I used batman++ last reply and it fixed the problem.

Fix ==> Also you might want to set the table name in the Fill() method.

myCommand.Fill(ds, "CRIMP_common_lookup");


Are you sure, that this statement is working fine?

select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM = 'CRIMP_strName'";

I guess CRIMP_strName is a variable, but you have included it in the string. It should be like this..

select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM ='" + CRIMP_strName + "'";

If you are going to store only one table in the dataset, then its not madatory to give the table name!. C# doesnt look for SQL case sensitiveness!

Thanks


Thanks e_screw,

Although my SQL definition works: (

select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM = 'CRIMP_strName'";

), yours lookes more appropriate: (select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM ='" + CRIMP_strName + "'";)

On another note, how would I write the code to use a DataReader instead of a DataSet? How would you rewrite the following code to reflect the DataReader?


private void GetData()

{

string ConnectionString = "server=MyServer;uid=Sphynx;pwd=letmein;database=MyDatabase";

string CommandText = "select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM = '" + CRIMP_strName + "'";

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);

DataSet ds = new DataSet();

myCommand.Fill(ds, "CRIMP_common_lookup");

Session["varString"] = ds.Tables["CRIMP_Common_Lookup"].Rows[0][2].ToString();
myConnection.Close();

}

Thanks!


This will be the code to use SqlDataReader instead of DataSet. When you set the command text to the adapter, by default it will be SelectCommand. So we call the ExecuteReader on the selectCommand of the sqlDataAdapter. If you want to close the connection, you need to use the CommandBehavior.CloseConnection in ExecuteReader method.

1private void GetData() {2string ConnectionString ="server=MyServer;uid=Sphynx;pwd=letmein;database=MyDatabase";3string CommandText ="select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM = '" + CRIMP_strName +"'";45 SqlConnection myConnection =new SqlConnection(ConnectionString);6 SqlDataAdapter myCommand =new SqlDataAdapter(CommandText, myConnection);7 SqlDataReader myReader = myCommand.SelectCommand.ExecuteReader();8while (myReader.Read()) {9 Session["varString"] = myReader["CRIMP_Common_Lookup"].ToString();10 }11// myConnection.Close(); or CommandBehavior.CloseConnection12}13
HTH

How do you specify which row and column to read the data from?

e_screw,

what did you do when you copy and paste code with line number? I haven't figure out how to include the line number.

If you can give me a hint how to do that I will really appreciate!

Sue

e_screw:

This will be the code to use SqlDataReader instead of DataSet. When you set the command text to the adapter, by default it will be SelectCommand. So we call the ExecuteReader on the selectCommand of the sqlDataAdapter. If you want to close the connection, you need to use the CommandBehavior.CloseConnection in ExecuteReader method.

1private void GetData() {2string ConnectionString ="server=MyServer;uid=Sphynx;pwd=letmein;database=MyDatabase";3string CommandText ="select LOOKUP_DS from CRIMP_COMMON_LOOKUP where LOOKUP_NM = '" + CRIMP_strName +"'";45 SqlConnection myConnection =new SqlConnection(ConnectionString);6 SqlDataAdapter myCommand =new SqlDataAdapter(CommandText, myConnection);7 SqlDataReader myReader = myCommand.SelectCommand.ExecuteReader();8while (myReader.Read()) {9 Session["varString"] = myReader["CRIMP_Common_Lookup"].ToString();10 }11// myConnection.Close(); or CommandBehavior.CloseConnection12}13
HTH


When you are using SqlDataReader, we use the .Read() method, to read the rows returned.myReader.Read() reads all the rows in the reader. In a particular row, you can directly access the column by its name or ordinal.myReader["fieldName"] or myReader[0] gets you the required column.

while(myReader.Read()) {
string name = myReader["name"].ToString();
}

This will read the fieldname "name" for each row returned.

Thanks