Wednesday, March 21, 2012
Passing a javascript array to a handler .ashx
displayed according to parameters passed in the querystring. The handerl is
called via some clientside javascript.
I want to send an array of unknown length to the handler. I am not sure
whether the querystring is the best place to do this. What's the best way to
send an array held in the client side javascript to the handler?
Thanks in advance,
JJyou can not post an image request, so the url is the only option other than
a
prelimary ajax call. the url is restricted to about 2k. how you encode the
array depends on the array type. the most common is a array.join('|'), with
a
matching split on the server side.
-- bruce (sqlwork.com)
"JJ" wrote:
> I am using a handler (processImage.ashx) to display an image. The image is
> displayed according to parameters passed in the querystring. The handerl i
s
> called via some clientside javascript.
> I want to send an array of unknown length to the handler. I am not sure
> whether the querystring is the best place to do this. What's the best way
to
> send an array held in the client side javascript to the handler?
> Thanks in advance,
> JJ
>
>
How would I do the prelimary ajax call?
Thanks,
JJ
"bruce barker (sqlwork.com)"
<brucebarkersqlworkcom@.discussions.microsoft.com> wrote in message
news:C8495134-FF6B-4D8B-82C1-FE5F393D6454@.microsoft.com...
> you can not post an image request, so the url is the only option other
> than a
> prelimary ajax call. the url is restricted to about 2k. how you encode the
> array depends on the array type. the most common is a array.join('|'),
> with a
> matching split on the server side.
> -- bruce (sqlwork.com)
>
> "JJ" wrote:
>
Hi JJ,
I think Bruce means you should pass the image urls(or any array parameters)
either through url querystring or through AJAX xmlhttp request. Directly
manipulate binary content of image is not quite supported in client script
and also not effecient to pass such large content frequently.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--
>From: "JJ" <abc@.xyz.com>
>Subject: Re: Passing a javascript array to a handler .ashx
>Date: Tue, 20 Nov 2007 10:55:18 -0000
>How would I do the prelimary ajax call?
>Thanks,
>JJ
>"bruce barker (sqlwork.com)"
><brucebarkersqlworkcom@.discussions.microsoft.com> wrote in message
>news:C8495134-FF6B-4D8B-82C1-FE5F393D6454@.microsoft.com...
the
handerl
way
>
>
Passing a javascript array to a handler .ashx
displayed according to parameters passed in the querystring. The handerl is
called via some clientside javascript.
I want to send an array of unknown length to the handler. I am not sure
whether the querystring is the best place to do this. What's the best way to
send an array held in the client side javascript to the handler?
Thanks in advance,
JJyou can not post an image request, so the url is the only option other than a
prelimary ajax call. the url is restricted to about 2k. how you encode the
array depends on the array type. the most common is a array.join('|'), with a
matching split on the server side.
-- bruce (sqlwork.com)
"JJ" wrote:
Quote:
Originally Posted by
I am using a handler (processImage.ashx) to display an image. The image is
displayed according to parameters passed in the querystring. The handerl is
called via some clientside javascript.
>
I want to send an array of unknown length to the handler. I am not sure
whether the querystring is the best place to do this. What's the best way to
send an array held in the client side javascript to the handler?
>
Thanks in advance,
JJ
>
>
>
How would I do the prelimary ajax call?
Thanks,
JJ
"bruce barker (sqlwork.com)"
<brucebarkersqlworkcom@.discussions.microsoft.comwro te in message
news:C8495134-FF6B-4D8B-82C1-FE5F393D6454@.microsoft.com...
Quote:
Originally Posted by
you can not post an image request, so the url is the only option other
than a
prelimary ajax call. the url is restricted to about 2k. how you encode the
array depends on the array type. the most common is a array.join('|'),
with a
matching split on the server side.
>
-- bruce (sqlwork.com)
>
>
"JJ" wrote:
>
Quote:
Originally Posted by
>I am using a handler (processImage.ashx) to display an image. The image
>is
>displayed according to parameters passed in the querystring. The handerl
>is
>called via some clientside javascript.
>>
>I want to send an array of unknown length to the handler. I am not sure
>whether the querystring is the best place to do this. What's the best way
>to
>send an array held in the client side javascript to the handler?
>>
>Thanks in advance,
>JJ
>>
>>
>>
Hi JJ,
I think Bruce means you should pass the image urls(or any array parameters)
either through url querystring or through AJAX xmlhttp request. Directly
manipulate binary content of image is not quite supported in client script
and also not effecient to pass such large content frequently.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
-------
Quote:
Originally Posted by
>From: "JJ" <abc@.xyz.com>
>Subject: Re: Passing a javascript array to a handler .ashx
>Date: Tue, 20 Nov 2007 10:55:18 -0000
Quote:
Originally Posted by
>How would I do the prelimary ajax call?
>
>Thanks,
>JJ
>"bruce barker (sqlwork.com)"
><brucebarkersqlworkcom@.discussions.microsoft.comwro te in message
>news:C8495134-FF6B-4D8B-82C1-FE5F393D6454@.microsoft.com...
Quote:
Originally Posted by
>you can not post an image request, so the url is the only option other
>than a
>prelimary ajax call. the url is restricted to about 2k. how you encode
the
Quote:
Originally Posted by
Quote:
Originally Posted by
>array depends on the array type. the most common is a array.join('|'),
>with a
>matching split on the server side.
>>
>-- bruce (sqlwork.com)
>>
>>
>"JJ" wrote:
>>
Quote:
Originally Posted by
>>I am using a handler (processImage.ashx) to display an image. The image
>>is
>>displayed according to parameters passed in the querystring. The
handerl
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
>>is
>>called via some clientside javascript.
>>>
>>I want to send an array of unknown length to the handler. I am not sure
>>whether the querystring is the best place to do this. What's the best
way
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
>>to
>>send an array held in the client side javascript to the handler?
>>>
>>Thanks in advance,
>>JJ
>>>
>>>
>>>
>
>
>
passing a string array?
"agb" <agbeltekin@.mynet.com> wrote in message
news:%23%23o4VPUbDHA.4020@.tk2msftngp13.phx.gbl...
> How can I send a string array to another aspx page?
Hello
There are several methods to do this, the best method depends on your
application.
1 - Put your array in a session variable. This method can be used with any
type of data
2 - Using cookies: concatenate the strings in one big string (use any
separator ';' for example ) then send it as a cookie to the brower, in the
other aspx page, get the cookie value, split the string to get the array.
3 - Same as method 2 but use a query string instead of cookies, for exampe
mypage.aspx?mystrings=str1;str2;str3;str4.
4 - Put strings in hidden form fields, and submit to your other page.
Best Regards
Sherif
"agb" <agbeltekin@.mynet.com> wrote in message
news:##o4VPUbDHA.4020@.tk2msftngp13.phx.gbl...
> How can I send a string array to another aspx page?
Thanks a lot
"Sherif ElMetainy" <elmeteny@.wayout.net> wrote in message
news:uHTBHFVbDHA.2264@.TK2MSFTNGP11.phx.gbl...
> Hello
> There are several methods to do this, the best method depends on your
> application.
> 1 - Put your array in a session variable. This method can be used with any
> type of data
> 2 - Using cookies: concatenate the strings in one big string (use any
> separator ';' for example ) then send it as a cookie to the brower, in the
> other aspx page, get the cookie value, split the string to get the array.
> 3 - Same as method 2 but use a query string instead of cookies, for exampe
> mypage.aspx?mystrings=str1;str2;str3;str4.
> 4 - Put strings in hidden form fields, and submit to your other page.
> Best Regards
> Sherif
> "agb" <agbeltekin@.mynet.com> wrote in message
> news:##o4VPUbDHA.4020@.tk2msftngp13.phx.gbl...
> > How can I send a string array to another aspx page?
Passing a string[] into a class
hi i am trying to pass a string array( all of the values at the same time) into a class
I am using this code trying to pass into this class
string[] itemTitle;string[] itemNames; OdbcConnection userCheckCon = sqlconnect.Connect(); OdbcCommand userCheck =new OdbcCommand("select Username from tbl_usr where Username = '" + uName +"'", userCheckCon); OdbcDataReader userRead = userCheck.ExecuteReader();int count = 0;while (userRead.Read()) {if (userRead.HasRows && variablePass) { itemTitle[count] ="userName"; itemNames[count] = uName; }else { itemTitle[count] ="userName"; itemNames[count] = ("<span class='greenHigh'>" + uName +" is available </span>"); } count++; } userRead.Close(); userCheckCon.Close();//END XML xmlGen userC =new xmlGen(); userC.xmlStart(writer); userC.addNode(writer,itemTitle[],itemNames[]); userC.xmlEnd(writer);The problem is on the userC.addNode line and this is the class i am trying to pass into it
class
public XmlTextWriter addNode(XmlTextWriter writer,string[] itemTitle,string[] itemName)
{
for(int items = 0; items < itemTitle.Length; items++)
{
writer.WriteElementString(itemTitle[items], itemName[items]);
}
return writer;
}
Thanks
Dan
Two problems.
1. Your string[] itemTitle needs a = new string[MaxRows] where maxrows is an int. If you dont know the size until later in the code you can always use a List<string> instead.
2 (and more importantly), take out the []'s from userC.addNode(writer,itemTitle[],itemNames[]); Should be userC.addNode(writer,itemTitle,itemNames);
hi
using arry of strings here is not recomended ,
because we dont know the number of elements ,
you must useStringCollection class ,
this is a dynamic array,
you can add and remove from it as you want ,
so change you variables types as follows :
StringCollection itemTitle=new StringCollection ();
StringCollection itemNames=new StringCollection ();
now to add items to these colletions use this :
itemTitle.add("your string") ....
and to iterate inside these collections use this :
for( int i=0; collection.count-1; i++)
{ yoou code here }
regards,
This code is also vulnerable to SQL injection.
Hi thanks will give this all a go tonight
and how is it vunerable to SQL injection
OdbcCommand userCheck =new OdbcCommand("select Username from tbl_usr where Username = '" + uName +"'", userCheckCon);if uName is like the query string or textbox text value all I need to enter to return the all rows isanything' OR 'x'='x
Or If I want to get more malicious I can do a ' delete from Users --
Look into stored procedures if you want to solve this problem.
Friday, March 16, 2012
Passing an array as a session variable
I have 2 asp.net forms. In the first form I create a one dimensional array and copy that array into a session object. In the second form I want to read the elements of that array. Everything I've tried results in either a casting error, a late binding error or nothing at all. The following code shows what Iwant to do.
Form one: Dim aArray(2) as stringaArray(0) = "zero"aArray(1) = "one"Session("PassArray") = aArrayForm two: Dim aTest(2) as stringaTest(0) = CType(Session("PassArray(0)"), String)aTest(1) = CType(Session("PassArray(1)"), String)When I run form two in the editor, I can Watch Session("PassArray") and see:Session("PassArray") {System.Array} object-- (0) "zero" string-- (1) "one" stringSo I know it's there and available, but how do I read the extents.Could really use a simple code example to explain what I'm doing wrong.
Hi,
Use this on the second form:
Dim aTest() As String = CType(Session("PassArray"), String())
That did it. Thank you.
Passing an Array as a Property
I would like to use the following in a property:
SqlParameter [] param = new SqlParameter[3];
param[0] = new SqlParameter("@dotnet.itags.org.attend", SqlDbType.NVarChar, 3);
param[0].Value = this.TextBox1.Text;
.
.
.
the textboxes are also in another class (webform), so i'm not sure how can i use them as well.
If anybody has any suggestions, i'd really appreciate it.The syntax to create a property in a class is as follows:
1) create a private variable in the class to hold and return the value
2) create the Get & Set accessors for the private variable described in 1
Example:
This will create a "Customer Name" property to a class:
private string _customerName;public string CustomerName
{
get
{
return _customerName;
}
set
{
_customerName=value;
}
}
Now with the syntax out of the way.. your specific question .. try this.. I am doing this from memory but it should work:
private SqlParameter[] _parameters;public SqlParameter[] Parameters
{
get
{
return _parameters;
}
set
{
_parameters = value;
}
}
Now.. to use the property, you could do this:
MyObject obj = new MyObject();
obj.Parameters = new SqlParameter[2];
obj.Parameters[0] = new SqlParameter("@.par1","some value");
obj.Parameters[1] = new SqlParameter("@.par2","some value");
Hope this helps,
Passing an array as a Session varibale
I have a problem passing an array as a session variable. here is my code.
page1.aspx
dim _title,_desc as String
dim i,_id,total As Integer
dim prs as project() ' project is a custom class
prs= D.getProjectsByHost(country) ' an array of projects.
total=prs.Length
For i = 0 To total- 1
_id =prs(i).prID
_title =prs(i).prTitle
_desc =prs(i).prDescription
''' here is what I need''
dim prArray() as string
prArray(_id)=_desc
session("prData")=prArray
next
I get an error for doing this: prArray(_id)=_desc
obect not set to an instance of object.........
in the next page"page2.aspx" I want to retreive the _desc for each project.
something like this:
dim dim prArray() as string
dim id = 45 ' for example
prArray=session("prData")
response.write(prArray(id))
thanks for any help.I could be wrong but dont you need to specify the size of the array?
Well, the reason you are getting an error is because you have not "initialized" an any elements for your array:
dim prArray() as stringYou have no useable elements in the array (technically, you have nothing more than some allocated memory space; you have put nothing IN it yet). A better way to declare your array (if I follow your sample code correctly) would be:
prArray(_id)=_desc
dim prArray(total) as stringThat should give you enough available elements in your array; that being said though, the above doesn't look like it is really the best way to pull off what you want.
A better way to arrange the data would probably be to make use of a HashTable object. HashTables allow you to create key/value pairs, and allow you ways of retrieving the values by searching with the key. Research on this object would reap great rewards, I'm sure...
Lastly, it's not necesarily a good idea to store objects in Session variables, if those objects are very large. This is because EACH USER will create and store THEIR OWN versions of the object, and if you have a high-traffic site, that could lead to serious performance issues. A (potentially) more performance-conscious method of storing this data in Session would be to store it as raw strings, and parse it as needed.
If you still feel you need to store the actual object in Session, the proper way to get it back out would be to use the CType() to cast it. Object in Session are of type Object; if you are retrieving a HashTable, you need to cast it BACK to a HashTable
'put the object in SessionHTH...
Sesssion("myHashTable") = myHashTableObject
...
'get the object out of Session
Dim myNewHashTableObject = CType(Session("myHashTable"), HashTable)
Chris
Thanks Chris, you idea of using hashtables is great. It solved my problem.
Passing an array from a function
I have a function that autenticates a user. Generally I just pass back true if the user is in the database, but now I want to pass back the userID and if the user is in the database. My guess is to do this with and array, but I keep having troubles passing it back. Code below.
Error I get is
Compiler Error Message:CS0029: Cannot implicitly convert type 'bool' to 'string[]'
Source Error:
Line 10: String strAuthenticate;Line 11:Line 12: userInfo = Authenticate(username.Text, password.Text);Line 13: strAuthenticate = userInfo[0];Line 14: if(strAuthenticate == "0"){
<code>
void login(Object s, EventArgs e){
String[] userInfo = new String[2];
String strAuthenticate;
userInfo = Authenticate(username.Text, password.Text);
strAuthenticate = userInfo[0];
if(strAuthenticate == "0"){
//creating a cookie to store user info
FormsAuthenticationTicket objTicket;
HttpCookie objCookie;
objTicket = new FormsAuthenticationTicket(1, username.Text, DateTime.Now, DateTime.Now.AddMinutes(240), false,"");
objCookie = new HttpCookie(".ASPXAUTH");
objCookie.Value = FormsAuthentication.Encrypt(objTicket);
Response.Cookies.Add(objCookie);
Response.Redirect("logedIn.aspx");
//FormsAuthentication.RedirectFromLoginPage(username.Text, false);
}
else{
lblLogin.Text = "INCORRECT USER NAME AND/OR PASSWORD";
}
}
bool Authenticate(string strUsername, string strPassword){
OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["backupDataB"]);
OleDbCommand objCmd;
OleDbDataReader objDR;
bool userFound;
String userID;
String[] userInfo = new String[2];
objCmd = new OleDbCommand("SELECT * FROM users WHEREemail=@dotnet.itags.org.strUsername ANDPassword=@dotnet.itags.org.strPassword", objConn);
//get userID from this query and store it in a ticket
objCmd.Parameters.Add("@dotnet.itags.org.strUsername", strUsername);
objCmd.Parameters.Add("@dotnet.itags.org.strPassword", strPassword);
objConn.Open();
objDR = objCmd.ExecuteReader();
userFound = objDR.Read();
userID = objDR["userID"];
objDR.Close();
objConn.Close();
userInfo[0] = userFound;
userInfo[1] = userID;
return (String)userInfo;
}
</code>
You'll need to also change the definition of your function to return a string array:
bool Authenticate(string strUsername, string strPassword){
Marcie
I have changed what I think I need to change, but now I get this error and I'm not really sure why.
CS0165: Use of unassigned local variable 'userID'
void login(Object s, EventArgs e){
String[] userInfo = new String[2];
String strAuthenticate;
userInfo = Authenticate(username.Text, password.Text);
strAuthenticate = userInfo[0];
if(strAuthenticate == "0"){
//creating a cookie to store user info
FormsAuthenticationTicket objTicket;
HttpCookie objCookie;
objTicket = new FormsAuthenticationTicket(1, username.Text, DateTime.Now, DateTime.Now.AddMinutes(240), false, userInfo[1]);
objCookie = new HttpCookie(".ASPXAUTH");
objCookie.Value = FormsAuthentication.Encrypt(objTicket);
Response.Cookies.Add(objCookie);
Response.Redirect("logedIn.aspx");
}
else{
lblLogin.Text = "INCORRECT USER NAME AND/OR PASSWORD";
}
}
String[] Authenticate(string strUsername, string strPassword){
OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["backupDataB"]);
OleDbCommand objCmd;
OleDbDataReader objDR;
String userFound = "no";
String userID;
String[] userInfo = new String[2];
objCmd = new OleDbCommand("SELECT * FROM users WHEREemail=@.strUsername ANDPassword=@.strPassword", objConn);
//get userID from this query and store it in a ticket
objCmd.Parameters.Add("@.strUsername", strUsername);
objCmd.Parameters.Add("@.strPassword", strPassword);
objConn.Open();
objDR = objCmd.ExecuteReader();
//"Read" reads each line that is returned from the datareader it starts one above the first record
//if the data is there it will return true if not it will return false. Here it is only examining to see
//if the datareader can read one line. If it can the record does exsit if it can't it means the reader
//returned nothing.
while(objDR.Read()){
userFound = "yes";
userID = objDR.GetString(0);
}
objDR.Close();
objConn.Close();
userInfo[0] = userFound;
userInfo[1] = userID;
return userInfo;
}
Your method signature specifies bool
bool Authenticate(string strUsername, string strPassword){
your return specifies a string
return (String)userInfo;
and userinfo is actually a string array
You need to make all these the same Type
That could happen if your query doesn't return any results (user is not found). You're only initializing userID in the while--read loop:
while(objDR.Read()){
userFound = "yes";
userID = objDR.GetString(0);
}
Marcie
Yes it was because I needed to intialize userID,
Thanks
void login(Object s, EventArgs e){
int[] userInfo = new int[2];
int strAuthenticate;
userInfo = Authenticate(username.Text, password.Text);
strAuthenticate = userInfo[0];
if(strAuthenticate == 1){
//creating a cookie to store user info
FormsAuthenticationTicket objTicket;
HttpCookie objCookie;
objTicket = new FormsAuthenticationTicket(1, username.Text, DateTime.Now, DateTime.Now.AddMinutes(240), false, userInfo[1].ToString());
objCookie = new HttpCookie(".ASPXAUTH");
objCookie.Value = FormsAuthentication.Encrypt(objTicket);
Response.Cookies.Add(objCookie);
Response.Redirect("logedIn.aspx");
}
else{
lblLogin.Text = "INCORRECT USER NAME AND/OR PASSWORD";
}
}
int[] Authenticate(string strUsername, string strPassword){
OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["backupDataB"]);
OleDbCommand objCmd;
OleDbDataReader objDR;
int userFound = 0;
int userID = 0;
int[] userInfo = new int[2];
objCmd = new OleDbCommand("SELECT * FROM users WHEREemail=@.strUsername ANDPassword=@.strPassword", objConn);
//get userID from this query and store it in a ticket
objCmd.Parameters.Add("@.strUsername", strUsername);
objCmd.Parameters.Add("@.strPassword", strPassword);
objConn.Open();
objDR = objCmd.ExecuteReader();
//"Read" reads each line that is returned from the datareader it starts one above the first record
//if the data is there it will return true if not it will return false. Here it is only examining to see
//if the datareader can read one line. If it can the record does exsit if it can't it means the reader
//returned nothing.
while(objDR.Read()){
userFound = 1;
userID = objDR.GetInt32(0);
}
objDR.Close();
objConn.Close();
userInfo[0] = userFound;
userInfo[1] = userID;
return userInfo;
}
Passing an array to a function
What is the syntax to pass an array to a function ?
CheersVB? C#?
Here is VB
Dim myArray(10) as string
Dim returnVal as integer
returnVal = myFunction(myArray)
...
Private Function myFunction(passedArray() as string) as Integer
return passedArray.Length
End Function