Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Unable to cast object of type 'System.Byte[]' to type 'System.String'.

Reply
Thread Tools

Unable to cast object of type 'System.Byte[]' to type 'System.String'.

 
 
Imran Aziz
Guest
Posts: n/a
 
      09-13-2005
Hello All,
I am getting the following error on our production server, and I dont
get the same error on the development box.

Unable to cast object of type 'System.Byte[]' to type 'System.String'.


here is the code that I used to create a table and then add columns to it
later, later I populate the rows in the table.

since its happening on the production box, so I cannot seem to debug it
corrently as to where the error is coming from.

ds.Tables.Add("tblADsUsers");

ds.Tables["tblADsUsers"].Columns.Add("ADsUser",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsDisplayName",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsFirstName",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsLastName",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsOffice",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsEmailAddress",
Type.GetType("System.String"));

Thanks a lot.



any help would be great.



Imran.


 
Reply With Quote
 
 
 
 
Peter Rilling
Guest
Posts: n/a
 
      09-13-2005
Your snippet contains not references to a byte[] so I am not sure about the
context, but bytes do not represent a string because a string takes into
account some type of encoding such as unicode or ascii.

You might want to take a look at Encoding.GetString(). If you know now the
bytes were encoded, then the Encoding (or a derived) class should allow you
convert to a string.

"Imran Aziz" <> wrote in message
news:%...
> Hello All,
> I am getting the following error on our production server, and I dont
> get the same error on the development box.
>
> Unable to cast object of type 'System.Byte[]' to type 'System.String'.
>
>
> here is the code that I used to create a table and then add columns to it
> later, later I populate the rows in the table.
>
> since its happening on the production box, so I cannot seem to debug it
> corrently as to where the error is coming from.
>
> ds.Tables.Add("tblADsUsers");
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsUser",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsDisplayName",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsFirstName",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsLastName",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsOffice",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsEmailAddress",
> Type.GetType("System.String"));
>
> Thanks a lot.
>
>
>
> any help would be great.
>
>
>
> Imran.
>
>



 
Reply With Quote
 
 
 
 
Imran Aziz
Guest
Posts: n/a
 
      09-13-2005
Well I dont know where the byte[] is being returned, since I only get it on
the live box. But I think the error is thrown when I am trying to convert
the return values from the ADS call to string which works fine on my
production box. Here is the complete function.

Thanks a lot for your help.Please help me do the right conversion.

public DataSet GetFilteredADDomainUsers_memdatabase(String strFirstName,
String strLastName)

{

DataSet ds = new DataSet();

int bUserExists = 0;

try

{

String ADPath = ConfigurationSettings.AppSettings["ADPath"].ToString();

String ADUser = ConfigurationSettings.AppSettings["ADUser"].ToString();

String ADPass = ConfigurationSettings.AppSettings["ADPass"].ToString();

DirectoryEntry entryRoot = new DirectoryEntry("LDAP://RootDSE", ADUser,
ADPass);

string domain = entryRoot.Properties["defaultNamingContext"][0].ToString();

DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + domain);

if (searchRoot != null)

{

DirectorySearcher search = new DirectorySearcher(searchRoot);

SearchResult result;

//search.Filter = GetFilterString(strFilter);

search.Filter = "(&(objectClass=Person)(&(sn=" + strLastName +
"*)(givenName=" + strFirstName + "*)))";


search.PropertiesToLoad.Add("samaccountname");

search.PropertiesToLoad.Add("sn");

search.PropertiesToLoad.Add("displayName");

search.PropertiesToLoad.Add("givenName");

search.PropertiesToLoad.Add("physicalDeliveryOffic eName");

search.PropertiesToLoad.Add("mail");

//SearchResult result;

SearchResultCollection resultCol = search.FindAll();

if (resultCol != null)

{

ds.Tables.Add("tblADsUsers");

ds.Tables["tblADsUsers"].Columns.Add("ADsUser",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsDisplayName",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsFirstName",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsLastName",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsOffice",
Type.GetType("System.String"));

ds.Tables["tblADsUsers"].Columns.Add("ADsEmailAddress",
Type.GetType("System.String"));

//SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(sqlAdapter);

//sqlAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

for (int counter = 0; counter < resultCol.Count; counter++)

{

result = resultCol[counter];

if (result.Properties.Contains("samaccountname"))

{

String tmpUser;

tmpUser = result.Properties["samaccountname"][0].ToString();

bUserExists = CheckExistingUser(tmpUser.ToLower());

if (bUserExists == 0)

{

DataRow row = ds.Tables["tblADsUsers"].NewRow();

row["ADsUser"] = tmpUser;

if (result.Properties.Contains("displayName"))

{

row["ADsDisplayName"] = (String)result.Properties["displayName"][0];

}

else

{

row["ADsDisplayName"] = " ";

}

if (result.Properties.Contains("givenName"))

{

row["ADsFirstName"] = (String)result.Properties["givenName"][0];

}

else

{

if (result.Properties.Contains("displayName"))

{

row["ADsFirstName"] = (String)result.Properties["displayName"][0];

}

else

{

row["ADsFirstName"] = " ";

}

}

if (result.Properties.Contains("sn"))

{

row["ADsLastName"] = (String)result.Properties["sn"][0];

}

else

{

row["ADsLastName"] = " ";

}

if (result.Properties.Contains("physicalDeliveryOffic eName"))

{

row["ADsOffice"] =
(String)result.Properties["physicalDeliveryOfficeName"][0];

}

else

{

row["ADsOffice"] = " ";

}

if (result.Properties.Contains("mail"))

{

row["ADsEmailAddress"] = (String)result.Properties["mail"][0];

}

else

{

row["ADsEmailAddress"] = " ";

}


ds.Tables["tblADsUsers"].Rows.Add(row);

}

else

{

nUserExist++;

}

}

}

if (ds.Tables["tblADsUsers"].Rows.Count > 0)

{

return ds;

}

else

{

return null;

}

}

}

}

catch (Exception ex)

{

strError = ex.Message;

}

return null;

}


"Peter Rilling" <> wrote in message
news:...
> Your snippet contains not references to a byte[] so I am not sure about
> the context, but bytes do not represent a string because a string takes
> into account some type of encoding such as unicode or ascii.
>
> You might want to take a look at Encoding.GetString(). If you know now
> the bytes were encoded, then the Encoding (or a derived) class should
> allow you convert to a string.
>
> "Imran Aziz" <> wrote in message
> news:%...
>> Hello All,
>> I am getting the following error on our production server, and I dont
>> get the same error on the development box.
>>
>> Unable to cast object of type 'System.Byte[]' to type 'System.String'.
>>
>>
>> here is the code that I used to create a table and then add columns to it
>> later, later I populate the rows in the table.
>>
>> since its happening on the production box, so I cannot seem to debug it
>> corrently as to where the error is coming from.
>>
>> ds.Tables.Add("tblADsUsers");
>>
>> ds.Tables["tblADsUsers"].Columns.Add("ADsUser",
>> Type.GetType("System.String"));
>>
>> ds.Tables["tblADsUsers"].Columns.Add("ADsDisplayName",
>> Type.GetType("System.String"));
>>
>> ds.Tables["tblADsUsers"].Columns.Add("ADsFirstName",
>> Type.GetType("System.String"));
>>
>> ds.Tables["tblADsUsers"].Columns.Add("ADsLastName",
>> Type.GetType("System.String"));
>>
>> ds.Tables["tblADsUsers"].Columns.Add("ADsOffice",
>> Type.GetType("System.String"));
>>
>> ds.Tables["tblADsUsers"].Columns.Add("ADsEmailAddress",
>> Type.GetType("System.String"));
>>
>> Thanks a lot.
>>
>>
>>
>> any help would be great.
>>
>>
>>
>> Imran.
>>
>>

>
>



 
Reply With Quote
 
Imran Aziz
Guest
Posts: n/a
 
      09-13-2005
I am using the LDAP provider to get list of properties for users. the code
works fine on my development machine but when moved to production I get a
conversion error of.

Unable to cast object of type 'System.Byte[]' to type 'System.String'.

Here is the code that generates error.

SearchResult result;
String tmpUser;

tmpUser = result.Properties["samaccountname"][0].ToString();

If I use Convert.ToString() to convert it I get System.byte[] as the return.

Can anyone please help me with this issue.

Imran.

"Imran Aziz" <> wrote in message
news:%...
> Hello All,
> I am getting the following error on our production server, and I dont
> get the same error on the development box.
>
> Unable to cast object of type 'System.Byte[]' to type 'System.String'.
>
>
> here is the code that I used to create a table and then add columns to it
> later, later I populate the rows in the table.
>
> since its happening on the production box, so I cannot seem to debug it
> corrently as to where the error is coming from.
>
> ds.Tables.Add("tblADsUsers");
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsUser",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsDisplayName",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsFirstName",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsLastName",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsOffice",
> Type.GetType("System.String"));
>
> ds.Tables["tblADsUsers"].Columns.Add("ADsEmailAddress",
> Type.GetType("System.String"));
>
> Thanks a lot.
>
>
>
> any help would be great.
>
>
>
> Imran.
>
>



 
Reply With Quote
 
ljenner01 ljenner01 is offline
Junior Member
Join Date: Jan 2011
Posts: 1
 
      01-19-2011
The blog on this Developers website may shed some light:

(Put this into Google):

"HellSpawn Developments" Unable to cast object of type 'System.String' to type 'System.String[]'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to cast object of type 'ASP.main_master' to type 'ASP.main_master'. craigkenisston@hotmail.com ASP .Net 2 08-15-2006 11:23 PM
Unable to cast object of type 'asp.usercontrols_....._ascx' to type 'usercontrols_.....' marshausk@hotmail.com ASP .Net 1 08-14-2006 12:41 PM
Unable to cast object of type 'System.Web.Compilation.BuildResultCompiledAssembly' to type 'System.Web.Util.ITypedWebObjectFactory'. jlecain@free.fr ASP .Net 0 08-03-2006 06:00 PM
Help: Unable to cast object of type 'System.Int32' to type 'System.String'. keithb ASP .Net 3 05-07-2006 08:06 AM
Unable to cast object of type 'ASP.' to type '' hlyall1189@gmail.com ASP .Net 0 04-11-2006 08:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57