472,950 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

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

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.
Nov 19 '05 #1
3 12859
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" <im***@tb2.net> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.

Nov 19 '05 #2
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" <pe***@nospam.rilling.net> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...
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" <im***@tb2.net> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.


Nov 19 '05 #3
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" <im***@tb2.net> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
When I execute the following (with an OleDBDataAdapter), wanting to add a row to a visual foxpro table: myrow= datasetTarget.Tables(0).NewRow 'fill all columns here like.. row(i)= myvalue '...
1
by: u022318 | last post by:
I am writing a program with MS Visual Studio .Net 2003, which reads data from a MS SQL DB. Reading the data is fine, but when I push update, so it writes the data, it get the error message invald...
6
by: John Hoffman | last post by:
Reading registry: .... RegistryKey rksub = rkey.OpenSubKey(s); String valstr = rksub.GetValueNames(); foreach (String vs in valstr) { String vstr = rksub.GetValue(vs).ToString(); OR String...
2
by: Imran Aziz | last post by:
Hello All, How can I convert System.Byte to System.String ? When I use Convert.ToString() it just returns me the string "System.Byte" Please help me out with this. Imran.
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
9
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
2
by: anj2k5 | last post by:
I uploaded music file in SQl DB in a image dataype .But when i try to reterive it,It doesn't play. It gives the following type casting error on runtime "Unable to cast object of type...
1
by: krishnaneeraja | last post by:
Hi, I wrote code(C#.net) in this way to find ASCII value of an URL.but i got some errors.plz rectify anyone. public int stringorder(string str) { int result=new int; ...
1
by: miller.brettm | last post by:
Hi, I'm working with an ActiveX serial component that fires an event when data is received. I need to continue to use this component because it contains the ability to do Xmodem1k an d XmodemCRC...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.