RSS

Category Archives: ASP.NET

Regular expression in javascript

In Javascript,

function Validate()

{

            var txt = document.getElementById(“<%=txt.ClientID %>”);

            var regEx = /^[0-9]{1,6}?$/; // For not more than 6 numbers

            if (regEx.test(txt.value))

                alert(“Correct.”);

            else

           {

                alert(“Only 6 numbers.”);

                txt.focus();

            }

}

In CodeBehind,

<asp:TextBox ID=”txt” runat=”server” onblur=”Validate()”>

 
Leave a comment

Posted by on August 23, 2012 in ASP.NET

 

AJAX AutoCompleteExtender without WebService

Consider an example of search by name,

In database create a table that consist of names.

In design page,

<div>
Name to search
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:ToolkitScriptManager ID=”ToolkitScriptManager1″ runat=”server”>
</asp:ToolkitScriptManager>
<asp:AutoCompleteExtender ID=”AutoCompleteExtender1″ TargetControlID=”TextBox1″ runat=”server”
UseContextKey=”True” ServiceMethod=”GetCountries” MinimumPrefixLength=”1″
CompletionSetCount=”1″ CompletionInterval=”50″>
</asp:AutoCompleteExtender>
</div>

In code behind,

protected void Page_Load(object sender, EventArgs e)
{
}

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCountries(string prefixText)
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings[“dbconnection”].ToString());
con.Open();
SqlCommand cmd = new SqlCommand(“select name from tblName where name like @Name+’%'”, con);
cmd.Parameters.AddWithValue(“@Name”, prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> CountryNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CountryNames.Add(dt.Rows[i][0].ToString());
}
return CountryNames;
}

 
Leave a comment

Posted by on August 8, 2012 in ASP.NET

 

Procedure with output parameter in ASP.NET

My Procedure is follows,

create proc outparam(@a int,@b int,@c int out)

as

set @c=@a+@b;

In Code behind,

SqlCommand cmd = new SqlCommand(“outparam”, con);

con.Open();

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(“@a”, SqlDbType.Int).Value = TextBox1.Text;

cmd.Parameters.Add(“@b”, SqlDbType.Int).Value = TextBox2.Text;

cmd.Parameters.Add(“@c”,SqlDbType.Int);

cmd.Parameters[“@c”].Direction = ParameterDirection.Output;

cmd.ExecuteNonQuery();

TextBox3.Text = cmd.Parameters[“@c”].Value.ToString();

con.Close();

Good luck……………..

 
Leave a comment

Posted by on June 12, 2012 in ASP.NET, SQL

 

Send email using ASP.NET

ASP.NET allows us to send email from the programming.

using System.Net.Mail;

The above namespace having the feature to send email. Include that namespace to coding.

public void SendEmail()

{

 SmtpClient smtp = new SmtpClient();

 MailMessage mail = new MailMessage();

 mail.To.Add(“ToMailID@mail.com”);

 mail.From = new MailAddress(“FromMailID@mail.com”);

 mail.Subject = “Subject Here”;

 mail.Body = “Message Here”;

 smtp.Host = “smtp.mail.com”;

 smtp.Credentials = new System.Net.NetworkCredential(“FromId@mail.com”,”Password”);

 smtp.EnableSsl = true;

 smtp.Send(mail);

}

By the above way we can send email..

Good Luck……………

 
Leave a comment

Posted by on May 29, 2012 in ASP.NET, C#.NET

 

Stored procedure in ASP.NET

First of all you create one procedure in sql.

Then by following code we’ll use the stored procedure in ASP.NET. The main advantage of using stored procedure in ASP.NET is that it is fast and we hide the SQL queries in programming.

SqlConnection con;

SqlCommand cmd = new SqlCommand();

con = new SqlConnection(“server=””;uid=sa;pwd=””;database=product”);

cmd = new SqlCommand(“Procedurename”, con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(“@name”, SqlDbType.VarChar).Value = TextBox1.Text; //1st parameter to proc

cmd.Parameters.Add(“@passwd”, SqlDbType.VarChar).Value =TextBox2.Text; //2nd parameter to proc

cmd.Parameters.Add(“@email”, SqlDbType.VarChar).Value = TextBox4.Text; //3rd parameter to proc

con.Open();

cmd.ExecuteNonQuery();

con.Close();

Best of luck………..

 
Leave a comment

Posted by on May 23, 2012 in ASP.NET, SQL

 

Read word by word from txt file in c#

The following is the c# code for reading the text file word by word.

        string a;
        string[] words = { “” };
        StreamReader Reader = new StreamReader(path);
        string[] s = File.ReadAllLines(path);
        while ((a = Reader.ReadLine()) != null)
        {
            words = a.Split(‘ ‘);
            foreach (string word in words)
                          Console.WriteLine(word);
        }

By this code, you can find the exact count of words, find exact certain length word(i.e. 5 letter word) and much more.

Good luck…………………………………..

 
Leave a comment

Posted by on May 19, 2012 in ASP.NET, C#.NET