•
•
•
•
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
![]() |
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.
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.
•
•
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation:
Rep Power: 3
Solved Threads: 15
You can check it against the Database, if it already exists then call the same routine to generate next randomn no.
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>
"<br> <a href='<link>/'> VDAS </a> <br> Your Verification code is : <b>";
String footer = "</b><br> 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 ...
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>
"<br> <a href='<link>/'> VDAS </a> <br> Your Verification code is : <b>";
String footer = "</b><br> 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 ...
•
•
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation:
Rep Power: 3
Solved Threads: 15
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.
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.
•
•
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation:
Rep Power: 3
Solved Threads: 15
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.
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.
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.
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.
•
•
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation:
Rep Power: 3
Solved Threads: 15
Switch is faster than using "if". Also i think you are in the right direction with the login.
•
•
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation:
Rep Power: 3
Solved Threads: 15
Let me know how it goes.
Hi, I'm working on Switch right now.
I saw switch statements online.
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...
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...
C# Syntax (Toggle Plain Text)
if (!txtEmail1.Text.Equals("")) { msgMail1.From = new MailAddress(txtPCEmail.Text); if (!txtBackupEmail.Text.Equals("")) { msgMail1.CC.Add(new MailAddress(txtBackupEmail.Text)); } msgMail1.Subject = "TEST EMAIL for VR Documents (EndPack) - " + projectID; msgMail1.IsBodyHtml = true; String v_code = generateVCode(); vc = new VerCode(v_code,"1",projectID); VerCodeDAO.addVCode(vc); msgMail1.Body = body + v_code + footer; MailAddress to1 = new MailAddress(txtEmail1.Text); if (!txtWatcherEmail1.Text.Equals("")) { MailAddress cc1 = new MailAddress(txtWatcherEmail1.Text); msgMail1.CC.Add(cc1); } msgMail1.To.Add(to1); } if (!txtEmail2.Text.Equals("")) { msgMail2.From = new MailAddress(txtPCEmail.Text); if (!txtBackupEmail.Text.Equals("")) { msgMail2.CC.Add(new MailAddress(txtBackupEmail.Text)); } msgMail2.Subject = "TEST EMAIL for VR Documents (EndPack) - " + projectID; msgMail2.IsBodyHtml = true; 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); msgMail2.CC.Add(cc2); } msgMail2.To.Add(to2); msgMail2.Body = body + v_code + footer; }
Serene Joey
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
.net .net framework 3.0 access accounting software selection ajax asp business software code combo core custom data developer development dom dropdownlist erp systems evaluation evaluations fedora feed legacy linux microsoft module msdn net office project project management reader reuse selection skin software software selection software solutions sql technology evaluation theme vista weather web windows workflow xml xoap
- how can I copy asp.net project from one system and work on it in a different system (ASP.NET)
- i ned asp.net project (ASP.NET)
- I need a ASP.net project (ASP.NET)
- ??? ASP.NET vs. Outlook Calendar ??? (ASP.NET)
- ASP.NET project (ASP.NET)
- Error creating new ASP.Net project (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: Static vs Non-Static Methods
- Next Thread: Drawing circles and connecting them


Linear Mode