Below is the method used to generate random 5 digit number.
public int getRandomID ()
{
return r.Next(10000,99999);
}
The number 10000, 99999 specifies the range.
Below is the method used to generate random 5 digit number.
public int getRandomID ()
{
return r.Next(10000,99999);
}
The number 10000, 99999 specifies the range.
1 JavaScript
1.1 How to get client date and time
1.2 How to access a control by using JavaScript
1.3 How to invoke a server-side function with JavaScript
1.4 How to retrieve server side variables using JavaScript code
1.5 How to assign a value to a hidden field using JavaScript in ASP.NET
1.6 How to register the JavaScript function at Code-Behind
1.7 How to display images with a delay of five seconds
1.8 How to get browser screen settings and apply it to page controls
1.9 How to clear the session when the user closes a window
2 Ways to pass data between pages
2.7 How to use Server.Transfer
3 File Upload
3.2 How to upload multiple files at once
3.3 Why upload fails when using an ASP.NET FileUpload control to upload large files
3.4 How to upload an image files only
3.5 How to get a File Upload control work with an UpdatePanel
4 Calendar
4.1 How to change the culture settings for a Calendar
4.2 How to select multiple non-sequential dates at Code-Behind
4.3 How to disable some dates in Calendar control
4.4 How to extend Calendar control for Server-Side validation
4.5 How to set ToolTips and links in Calendar control’s DayRender event
4.6 How to give some dates different appearances
5 List controls
5.1 How to enable ASP.NET DropDownList with OptionGroup support
5.2 How to disable an item in DropDownList
5.3 How to hold the selected value for a DropDownList
6 User control
6.1 How to add a new property to UserControl
6.2 How to access a dynamically created UserControl
6.3 How to access a control inside a UserControl
7 Dynamic controls
7.1 How to create a dynamic control
7.2 How to access a user entered value in a dynamic created TextBox control
7.3 Dynamic controls accessed by JavaScript
7.4 How to retain all added server controls dynamically after post back
7.5 Why dynamically created controls disappear after a post back
8 Style
8.1 How to use with Code-Behind
8.2 How to use with JavaScript
8.5 How to set an image as Button’s background
8.6 How to color items in ListBox
9 Print
9.1 How to print a part of a web page with CSS
9.2 How to print a part of a web page with JavaScript (1)
9.3 How to print a part of a web page with JavaScript (2)
10 Mail
10.1 What classes are needed to send e-mails in ASP.NET
10.2 How to send emails by using System.Net.Mail
People while using MasterPages in your applications client ID of controls will differ from "server" ID. That's because ASP.NET creates a new ID for controls on a page. So this is probably very familiar to you:
ctl00_cphContent_txtName You set the ID of the textbox to "txtName" and ASP.NET adds "ctl00_cphContent_". Although there were some tries to prevent ASP.NET from generating unique ID's, I think it's better to use quick and "dirty" solutions :-)
So, how to select a server control using JS/jQuery? The usual way to select an element in JavaScript is to use server tags:
document.getElementById("<%=txtName.ClientID %>"); You can use the same approach with jQuery:
$("#'<%=txtName.ClientID %>'"); JQuery enables you to avoid server tags completely. Since we can search for element just by the part of the name, we can do the next:
$("[id$='_txtName']"); This will find elements which id's ends with "_txtName". But why the dash in the front of the name? This will ensure you selected the element by its full control ID, not just a part of it.
Pretty simple, isn't it? The only issue here would be if you have controls with the same name placed in different content pages.
The LEFT and TOP attributes in the following code sets the left and top positions of a control. You can call this code from any where in your code.
For example, if I have a table or calender control and want to change the position of the control dynamically, this is the code I need to add.
Calendar1.Attributes.Item("style") = "Z-INDEX: 176; LEFT: 334px; POSITION: absolute; TOP: 176px"
And in Asp.Net 3.5 it would be
Calendar1.Attributes.CssStyle.Value = "Z-INDEX: 176; LEFT: 334px; POSITION: absolute; TOP: 176px";
Sending a Simple Mail Using GMail SMTP SERVER :
Use the namespace
using System.Web.Mail;
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.Subject = "Test Email";
string Body = "Welcome to Mukund's Blog";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new
System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"],
ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;
smtp.Send(mail);
The above code can be used to send a simple email.
Sending Mail with Attachment:
MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("From@gmail.com");
mail.Subject = "Test Email";
string Body = "Welcome to Mukund's Blog";
mail.Body = Body;
mail.Attachments.Add(new Attachment(@"F:\Articles\Email in ASP.Net 2.0\SendEmail\mail.png"));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Send(mail);
The webcongif file should contain the following code.
Add open triangle and close triangle braces for each tag.
appSettings
add key="SMTP" value="smtp.gmail.com"
add key="FROMEMAIL" value="mail@gmail.com"
add key="FROMPWD" value="password"
appSettings
The above code can be used to send a simple email with attachment.
Thread State:
A Thread can be in one the following state.
Unstarted - Thread is Created within the common language run time but not Started still.
Running - After a Thread calls Start method
WaitSleepJoin - After a Thread calls its wait or Sleep or Join method.
Suspended - Thread Responds to a Suspend method call.
Stopped - The Thread is Stopped, either normally or Aborted.
We can check the current state of a thread using the Thread’s ThreadState property.
Thread Priorty:
The Thread class’s ThreadPriority property is used to set the priority of the Thread.
A Thread may have one of the following values as its Priority:
Lowest
BelowNormal
Normal
AboveNormal
Highest.
The default property of a thread is Normal.
REF: Code project
1. //How to select an item in a DropDownList by Value
ListItem li = yourDropDownlist.Items.FindByValue(”yourValue”);
if (li != null)
yourDropDownlist.SelectedIndex = yourDropDownlist.Items.IndexOf(li);
2. //How to check if value exists in DropDownList
public static bool IsValueInDropdownList(DropDownList controlName,string strValue)
{
if (controlName.Items.FindByValue(strValue) != null)
return true;
else
return false;
}
SYNONYMS:
Problem:
At work we face situations like ,
1. where tables created in the production database are required to move to another DB as a minimal time period, Moving the table with data's is a tedious job
2. And also we use various SPs, TVF Functions in some DB say DB1. When we are working in some other DB2 and we have situation to use the same SPs and Functions in DB1 for a test process, here we need to run the whole scripts in DB2 and we have to drop it if it is no more useful which comsume time
Solution:
The feature is SYNONYMs in SQL Server 2005. SYNONYMs is new to SQL Server 2005. It is a way to give an alias to an already existing or potential new object(May be a Table,SP,Functions,Views etc). It is just a pointer or reference, so it is not considered to be an object.
Required Permissions:
In order to create a synonym, you need to have CREATE SYNONYM permissions. If you are a sysadmin or db_owner you will have these privileges or you can grant this permission to other users. Also, you create a synonym that crosses databases you will need permissions for the other database as well..
CREATING SYNONYMs:
A SYNONYM can be created within a DB or Between DBs and also Between DBs in Different Servers.
Example 1:
Here is an Example for Creating a Synonyms within a DB
Syntax:
CREATE SYNONYM [SynName] FOR [ObjectName]
USE AdventureWorks
GO
CREATE SYNONYM MySyn FOR Production.Location
GO
To check that this works you can issue a query like
SELECT * FROM MySyn
This returns the values of the table Production.Location . This any modifications to this Synonyms will reflect in Production.Location table.
Say for example
UPDATE MySyn
SET Name = 'MukundsIdeas'
WHERE LocationID = 1
Executing this query will affect the values in the
Production.Location table of AdventureWorks DB.
Example 2:
Here is an example to create the SYNONYM in one database that references an object in another database.
USE master
GO
CREATE SYNONYM dbo.MySyn FOR AdventureWorks.Production.Location
GO
This creates a Synonym that can be accessible form any DB inside the Same Server.
Note: Make note on what Schema your DB use. For eg : if ur DB uses schema dbo then u should mention it as
CREATE SYNONYM dbo.MySyn FOR AdventureWorks.dbo.Production.Location
Example 3:
USE master
GO
CREATE SYNONYM dbo.MySyn FOR [UrServerName].[DBName].[TableName or SPname or Functionname or viewname etc]
GO
To get the meta data for all synonyms use the following command
SELECT * FROM sysobjects WHERE xtype = 'SN' ORDER BY NAME
And to drop the synonym use the following command
USE AdventureWorks;
GO
DROP SYNONYM MySyn
GO
SYNONYM's can be very useful and can be created for
1. Tables
2. Views
3. Assembly Stored Procedures,
4. Table Valued Functions,
5. Aggregations
6. SQL Scalar Functions
7. SQL Stored Procedures
8. SQL Table Valued Functions
9. SQL Inline-Table-Valued Functions
10. Local and Global Temporary Tables
11. Replication-filter-procedures
12. Extended Stored Procedures
Benefits :
SYNONYMs provide a layer of abstraction over the referenced object
Allow changes to complicated (multi part) and lengthy names with a simplified alias as a same server resident object.
Provides flexibility for changing the location of objects without changing existing code.
SYNONYMs can be created in the same database to provide backward compatibility for older applications in case of drop or rename of objects.
SYNONYMs can be useful if you give the front-end query tools like spreadsheets and Access linked tables direct links in to the tables.
Limitations:
SYNONYMs are loosely bound to the referenced objects. So you can delete a SYNONYM without getting any warning that it is being referenced by any other database object.
Chaining is not allowed. It means that you can not create SYNONYM of a SYNONYM.
Obviously consumes possible object names, as you can not create a table with the same name of a synonym
The object for which the SYNONYM is being created is checked at run time. It is not checked at creation time. So this means that if you make any related error e.g. spelling error, the synonym will created, but you will get an error while accessing the object.