User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 456,277 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,134 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 3038 | Replies: 29 | Solved
Reply
Join Date: Nov 2007
Posts: 52
Reputation: dotNetDummi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dotNetDummi's Avatar
dotNetDummi dotNetDummi is offline Offline
Junior Poster in Training

Re: C# ASP.NET Project Idea

  #11  
Nov 13th, 2007
Hi and thanks for your reply,

I found the method of generating numbers. It's working... But how can I improve it so that the code are not repeated as v_code is my primary key in my database.

the method is as follows:

//Generate v_code
private String generateVCode()
{
char[] normalChars = "qwertyuiopasdfghjklzxcvbnm1234567890".ToCharArray();

string code = string.Empty;

for (int i = 0; i < 20; i++)
{
char n = ' ';

n = normalChars[GetRandomNumber(normalChars.Length - 1)];

if (i % 2 == 0)
code += n.ToString().ToUpper();

else
code += n;
}
return code;
}

//Get Random Number
public static int GetRandomNumber(int maxvalue)
{
System.Security.Cryptography.RandomNumberGenerator random =

System.Security.Cryptography.RandomNumberGenerator.Create();

byte[] r = new byte[1];

random.GetBytes(r);

double val = (double)r[0] / byte.MaxValue;

int i = (int)Math.Round(val * maxvalue, 0);

return i;
}


Thank you in advance.
Reply With Quote  
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation: binoj_daniel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 15
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: C# ASP.NET Project Idea

  #12  
Nov 13th, 2007
You can check it against the Database, if it already exists then call the same routine to generate next randomn no.
Reply With Quote  
Join Date: Nov 2007
Posts: 52
Reputation: dotNetDummi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dotNetDummi's Avatar
dotNetDummi dotNetDummi is offline Offline
Junior Poster in Training

Re: C# ASP.NET Project Idea

  #13  
Nov 13th, 2007
Cool, I understand already. Thanks..
Erm, Now I have an error with my email.

I have 2 recipents, both vercode and apid in the database are different.

however, both the recipents receive the same vercode. the first recipents receive the second recipent's ver_code(different from database).

String body = "<html><body>Hi, <br><br>&nbsp;&nbsp;&nbsp;
"<br>&nbsp;&nbsp;&nbsp;<a href='<link>/'> VDAS </a> <br>&nbsp;&nbsp;&nbsp;Your Verification code is : <b>";

String footer = "</b><br>&nbsp;&nbsp;&nbsp;Thank You!" +
"<br><br>Regards, <br>Team</body></html>";

VerCode vc = new VerCode();

if (!txtEmail1.Text.Equals(""))
{
String v_code = generateVCode();
vc = new VerCode(v_code,"1",projectID);
VerCodeDAO.addVCode(vc);

msgMail.Body = body + v_code + footer;

MailAddress to1 = new MailAddress(txtEmail1.Text);

if (!txtWatcherEmail1.Text.Equals(""))
{
MailAddress cc1 = new MailAddress(txtWatcherEmail1.Text);
msgMail.CC.Add(cc1);
}
msgMail.To.Add(to1);


}

if (!txtEmail2.Text.Equals(""))
{
String v_code = generateVCode();
vc = new VerCode(v_code, "2", projectID);
VerCodeDAO.addVCode(vc);

MailAddress to2 = new MailAddress(txtEmail2.Text);

if (!txtWatcherEmail2.Text.Equals(""))
{
MailAddress cc2 = new MailAddress(txtWatcherEmail2.Text);
msgMail.CC.Add(cc2);
}
msgMail.To.Add(to2);
msgMail.Body = body + v_code + footer;

}

smtp.Send(msgMail);

I tried and put smtp.Send(msgMail); into each if statement but it's worst.

I apologise for troubling you but please advice. Thanks a Million. By the way, I find that it's not good to use if statements for checking of each txtbox (null or not) because I've 5 textboxes.

Other than using switch is there any methods for me to use? I thought of using for loop. is it possible. if so, can you please give me some hints/tips on the coding ...
Reply With Quote  
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation: binoj_daniel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 15
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: C# ASP.NET Project Idea

  #14  
Nov 14th, 2007
First debug and see what values are you getting for String v_code = generateVCode(); for each each call.
Then try using diff. mail objects to send mails to diff. people. In the above example you are trying to send the same mail to diff. user.
Reply With Quote  
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation: binoj_daniel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 15
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: C# ASP.NET Project Idea

  #15  
Nov 14th, 2007
ok, I got the problem,
In the database you might be getting diff. verification code but when you send in email it is coming same, right?
And the reason is because you are using the same mail object and same v_code variable. You have to use diff. mailobject instances for diff. values.
Reply With Quote  
Join Date: Nov 2007
Posts: 52
Reputation: dotNetDummi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dotNetDummi's Avatar
dotNetDummi dotNetDummi is offline Offline
Junior Poster in Training

Re: C# ASP.NET Project Idea

  #16  
Nov 14th, 2007
Cool. I got it already. It's working perfectly. Thanks man~
So now I can use the v_code, from here it's similar with login right?

get the v_code from the textbox, check who is the user or valid v_code then show the output?
am i right?

by the way, erm, there's no other options then if-else statements/switch??

sorry.
Reply With Quote  
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation: binoj_daniel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 15
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: C# ASP.NET Project Idea

  #17  
Nov 14th, 2007
Switch is faster than using "if". Also i think you are in the right direction with the login.
Reply With Quote  
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation: binoj_daniel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 15
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: C# ASP.NET Project Idea

  #18  
Nov 14th, 2007
Let me know how it goes.
Reply With Quote  
Join Date: Nov 2007
Posts: 52
Reputation: dotNetDummi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dotNetDummi's Avatar
dotNetDummi dotNetDummi is offline Offline
Junior Poster in Training

Re: C# ASP.NET Project Idea

  #19  
Nov 14th, 2007
Thanks for your help... =)
I will keep you update... Hopefully, I don't need to trouble you so much anymore... =)
Serene Joey
Reply With Quote  
Join Date: Nov 2007
Posts: 52
Reputation: dotNetDummi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dotNetDummi's Avatar
dotNetDummi dotNetDummi is offline Offline
Junior Poster in Training

Question Re: C# ASP.NET Project Idea

  #20  
Nov 15th, 2007
Hi, I'm working on Switch right now.
I saw switch statements online.
Switch (variable)
{
   case 1: //do something
   case 2: //do something else
   break;
}

but I don't know what variable I need for my case. Er can you please help?
I'm trying to use switch instead of if-else...

  1. if (!txtEmail1.Text.Equals(""))
  2. {
  3. msgMail1.From = new MailAddress(txtPCEmail.Text);
  4. if (!txtBackupEmail.Text.Equals(""))
  5. {
  6. msgMail1.CC.Add(new MailAddress(txtBackupEmail.Text));
  7. }
  8.  
  9. msgMail1.Subject = "TEST EMAIL for VR Documents (EndPack) - " + projectID;
  10.  
  11. msgMail1.IsBodyHtml = true;
  12.  
  13. String v_code = generateVCode();
  14. vc = new VerCode(v_code,"1",projectID);
  15. VerCodeDAO.addVCode(vc);
  16.  
  17. msgMail1.Body = body + v_code + footer;
  18.  
  19. MailAddress to1 = new MailAddress(txtEmail1.Text);
  20.  
  21. if (!txtWatcherEmail1.Text.Equals(""))
  22. {
  23. MailAddress cc1 = new MailAddress(txtWatcherEmail1.Text);
  24. msgMail1.CC.Add(cc1);
  25. }
  26. msgMail1.To.Add(to1);
  27.  
  28.  
  29. }
  30.  
  31. if (!txtEmail2.Text.Equals(""))
  32. {
  33. msgMail2.From = new MailAddress(txtPCEmail.Text);
  34. if (!txtBackupEmail.Text.Equals(""))
  35. {
  36. msgMail2.CC.Add(new MailAddress(txtBackupEmail.Text));
  37. }
  38.  
  39. msgMail2.Subject = "TEST EMAIL for VR Documents (EndPack) - " + projectID;
  40.  
  41. msgMail2.IsBodyHtml = true;
  42.  
  43. String v_code = generateVCode();
  44. vc = new VerCode(v_code, "2", projectID);
  45. VerCodeDAO.addVCode(vc);
  46.  
  47. MailAddress to2 = new MailAddress(txtEmail2.Text);
  48.  
  49. if (!txtWatcherEmail2.Text.Equals(""))
  50. {
  51. MailAddress cc2 = new MailAddress(txtWatcherEmail2.Text);
  52. msgMail2.CC.Add(cc2);
  53. }
  54. msgMail2.To.Add(to2);
  55. msgMail2.Body = body + v_code + footer;
  56.  
  57. }
Serene Joey
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 6:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC