object of Guid that are not converted?

  • Thread starter Thread starter Alexander Vasilevsky
  • Start date Start date
A

Alexander Vasilevsky

public static void AddUserToNewsLetter(object UserId, string
typeSend, out Exception ex)
{
ex = new Exception();
NewsLetterDataContext newsLetterDB = new
NewsLetterDataContext();
NewsLetter newsLetter = new NewsLetter();
newsLetter.UserId = (Guid)UserId;

exception on the last line of saying what was cast is not valid. Is there a
way?

http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
Gift
 
Alexander Vasilevsky said:
public static void AddUserToNewsLetter(object UserId, string
typeSend, out Exception ex)
{
ex = new Exception();
NewsLetterDataContext newsLetterDB = new
NewsLetterDataContext();
NewsLetter newsLetter = new NewsLetter();
newsLetter.UserId = (Guid)UserId;

exception on the last line of saying what was cast is not valid. Is there
a way?

Assuming that you are actually passing to the method something you believe
to be a Guid I suspect you are passing a string instead of an Guid.

I would recommend this approach:-

public static void AddUserToNewsLetter(Guid userId, string typeSend)
{
var dc = new NewsLetterDataContext();
var newsLetter = new NewsLetter();
newsLetter.UserId = userID;
...
}

public static void AddUserToNewsLetter(string userId, string typeSend)
{
AddUserToNewsLetter(new Guid(userId), typeSend);
}

public static void AddUserToNewLetter(byte[] userId, string typeSend)
{
AddUserToNewsLetter(new Guid(userId), typeSend);
}
 
Hello Alexander
You are receiving this exception because the first parameter of
AddUserToNewsLetter is not a GUID. Modify the code to look something like

public static void AddUserToNewsLetter(Guid UserId, string
typeSend, out Exception ex)

so you would call AddUserToNewsLetter(Guid.NewGuid()...

hope this helps
 
Back
Top