allow only characters in textbox using javascript in asp.net


<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
<script type=”text/javascript”>
function AllowAlphabet(e)
{
isIE = document.all ? 1 : 0
keyEntry = !isIE ? e.which : event.keyCode;
if (((keyEntry >= ’65’) && (keyEntry <= ’90’)) || ((keyEntry >= ’97’) && (keyEntry <= ‘122’)) || (keyEntry == ’46’) || (keyEntry == ’32’) || keyEntry == ’45’)
return true;
else
{
alert(‘Please Enter Only Character values.’);
return false;
}
}
</script>

</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:TextBox ID=”TextBox1″ runat=”server” onkeypress=”return AllowAlphabet(event)” ></asp:TextBox>
</div>
</form>
</body>
</html>

Advertisements

How to call a stored procedure using entity framework(edm)


First u need to create some database including tables and related stored procedures after completing this task we need to add that data base to our front end as entity framework for that we do some procedure. If u want that procedure please refer

https://chikkanti.wordpress.com/2012/02/08/how-to-add-entity-data-model/

When adding .edmx file we need to check tables and stored procedures as shown below

calling stored procedure using edm

after completion of .edmx file we need to add the available stored procedures in our .edmx to functions

for that first open modal browser and then open EntityContainer

then right clickon function imports and choose add function import as shown in below figure

We can get one window to select storedprocedure as follows

In that window we have different fields

1. function import name: this is the function name we will use in our front end to call stored procedure.

2. storedprocedurename: this is the storedprocedure name in our database and .edmx file we need to call

3. returns a collection of this will tell which type of output is coming from storedprocedure(we need to select this).

If we want to call that imported function

First create object for edm like

SRMModel.SRMBEntities ent = new SRMModel.SRMBEntities();

And call function like

ent.ForgotPWD();//if it don’t have any parameters to pass

if it have any input parameters call as follows

ent.ForgotPWD(UName, Qsn, Ans);

how to prevent user selecting past date from calender


<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head id=”Head1″ runat=”server”>
<title>Calendar Extender</title>
<script type=”text/javascript”>
function checkDate(sender, args) {

if (sender._selectedDate < new Date()) {
alert(“You cannot select a day earlier than today!”);
sender._selectedDate = new Date();
// set the date back to the current date
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
//if u want to assign null value to textbox uncomment following stmt
// document.getElementById(“TextBox1”).value = “”;
}
}
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” />
<div>

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:calendarextender ID=”CalendarExtender1″
runat=”server” OnClientDateSelectionChanged=”checkDate”
TargetControlID=”TextBox1″ />

</div>
</form>
</body>
</html>

how to add textboxes to webform in runtime(dynamically)


design the webform as follows:

  1. <html xmlns="http://www.w3.org/1999/xhtml"&gt;
  2. <head id="Head1" runat="server">
  3. <title></title>
  4. </head>
  5. <body>
  6. <form id="form1" runat="server">
  7. <div>
  8.  
  9. <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
  10. style="font-weight: 700" Text="Click to add textbox" />
  11.  
  12. </div>
  13. <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
  14. </form>
  15. </body>
  16. </html>

write codebehind as follows:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.     }
  13.     int count;
  14.     protected void Button1_Click(object sender, EventArgs e)
  15.     {
  16.         if (ViewState["tbno"] != null)
  17.         {
  18.             count = (int)ViewState["tbno"];
  19.             for (int i = 0; i < count; i++)
  20.             {
  21.                 TextBox tb = new TextBox();
  22.                 tb.ID = "tb" + ViewState["tbno"];
  23.                 PlaceHolder1.Controls.Add(tb);
  24.             }
  25.             ViewState["tbno"] = Convert.ToInt16(ViewState["tbno"]) + 1;
  26.         }
  27.         else
  28.         {
  29.             TextBox tb0 = new TextBox();
  30.             PlaceHolder1.Controls.Add(tb0);
  31.             ViewState["tbno"] = 2;
  32.         }
  33.     }
  34. }

displaying windows forms in panel in windows forms(c#)


after designing the form as above add what ever the forms you want to display in panel

then write the following code under button click event

  1. private void button1_Click(object sender, EventArgs e)
  2.     {
  3.         panel1.Controls.Clear();
  4.         Form2 f = new Form2();
  5.         f.TopLevel = false;
  6.         panel1.Controls.Add(f);
  7.         f.Show();
  8.     }

validations for windows forms


first desigh windows form as follows

write the following code in Register buttonclick event

 

  1. private void button1_Click(object sender, EventArgs e)
  2.     {
  3.         if (isvalid())
  4.         {
  5.             errorProvider1.Clear();
  6.             SqlConnection con = new SqlConnection(strcon);
  7.             SqlCommand cmd = new SqlCommand("sp_registration", con);
  8.             cmd.CommandType = CommandType.StoredProcedure;
  9.             cmd.Parameters.Add("@username", textBox1.Text);
  10.             cmd.Parameters.Add("@password", textBox2.Text);
  11.             cmd.Parameters.Add("@firstname", textBox3.Text);
  12.             cmd.Parameters.Add("@lastname", textBox4.Text);
  13.             cmd.Parameters.Add("@email", textBox5.Text);
  14.             cmd.Parameters.Add("@mobile", textBox6.Text);
  15.             con.Open();
  16.             int i = cmd.ExecuteNonQuery();
  17.             if (i > 0)
  18.             {
  19.                 label7.Text = "successfully Registered…";
  20.             }
  21.             else
  22.                 label7.Text = "Failed to register or details already exist…";
  23.             con.Close();
  24.         }
  25.     }
  26.     private bool isvalid()
  27.     {
  28.         bool error = true;
  29.         if (textBox1.Text.Trim() == string.Empty)
  30.         {
  31.             errorProvider1.SetError(textBox1, "Enter UserName");
  32.             error = false;
  33.         }
  34.         else if (textBox2.Text.Trim() == string.Empty)
  35.         {
  36.             errorProvider1.Clear();
  37.             errorProvider1.SetError(textBox2, "Enter Password");
  38.             error = false;
  39.         }
  40.         else if (textBox3.Text.Trim() == string.Empty)
  41.         {
  42.             errorProvider1.Clear();
  43.             errorProvider1.SetError(textBox3, "Enter FirstName");
  44.             error = false;
  45.         }
  46.         else if (textBox4.Text.Trim() == string.Empty)
  47.         {
  48.             errorProvider1.Clear();
  49.             errorProvider1.SetError(textBox4, "Enter LastName");
  50.             error = false;
  51.         }
  52.         else if (isvalidemail())
  53.         {
  54.             errorProvider1.Clear();
  55.             errorProvider1.SetError(textBox5, "Enter Valid Email");
  56.             error = false;
  57.         }
  58.         else if (isvalidmobile())
  59.         {
  60.             errorProvider1.Clear();
  61.             errorProvider1.SetError(textBox6, "Enter Valid Mobile Number");
  62.             error = false;
  63.         }
  64.         return error;
  65.  
  66.     }
  67.     private bool isvalidemail()
  68.     {
  69.         bool emailr = true;
  70.         if (textBox5.Text.Trim() != string.Empty)
  71.         {
  72.             if (textBox5.Text.Trim().Contains("@") && textBox5.Text.Trim().Contains("."))
  73.                 emailr = false;
  74.         }
  75.         return emailr;
  76.     }
  77.     private bool isvalidmobile()
  78.     {
  79.         bool mobile = false;
  80.         if (textBox6.Text.Trim() == string.Empty)
  81.             mobile = true;
  82.         else if (textBox6.Text.Trim().Length < 10)
  83.             mobile = true;
  84.         else if (textBox6.Text.Trim().Length > 10)
  85.             mobile = true;
  86.         else
  87.         {
  88.             try
  89.             {
  90.                 int.Parse(textBox6.Text);
  91.                 mobile = true;
  92.             }
  93.             catch
  94.             {
  95.                 mobile = false;
  96.                 return mobile;
  97.             }
  98.         }
  99.         return mobile;
  100.     }

How to retrieve data from database using EntityFrameWork


Create database tables as follows:

First add ADO.NET entity data modal to our project

Display data using grid view:

Form Design:

Add new form and place a GridView in form.

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</div>
</form>
</body>
</html>

Code Behind:

Write following code in pageload event:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetData();
    }
}
public void GetData()
{
    //first create object to the EDMEntities as follows…
    sampledatabaseModel.sampledatabaseEntities obj = new sampledatabaseModel.sampledatabaseEntities();
    //retrive data using edm…
    var x = (from n in obj.emps select n);
    //bind data to gridview
    GridView1.DataSource = x;
    GridView1.DataBind();
}

======================================================

How to get data from database based on some parameter using EntityFrameWork

Form Design:

1.Keep one gridview  to display data

2.One textbox to enter parameter

3.One button to send value

4.One literal to display error message

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<br />
Enter EmpNo:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>

Code behind:

In form load write above code only

Then write following code in button click event

if (TextBox1.Text.Trim() != string.Empty)
{
int i = int.Parse(TextBox1.Text);
var x = (from n in obj.emps
where n.eno == i
select n);
if (x != null)
{
GridView1.DataSource = x;
GridView1.DataBind();
}
else
{
Literal1.Text = "Enter Proper Empid from above details";
GetData();
}
}
else
Literal1.Text = "textbox should not be empty";