Monday, 8 July 2013

Connectivity with Sql Server with Entity FrameWork (Using Preview Concept with File Upload in Java Script)

Execute this query in Sql Server 2008 for creating database and table………………

Create database DbforEntityFrameWork

use DbforEntityFrameWork

create  table UserRegistration (SrNo int identity primary key,Name nvarchar(50),Gender nvarchar(50),Qulification nvarchar(50),EmailId nvarchar(50) unique,Password nvarchar(50),MobileNo nvarchar(50),CityName nvarchar(100),UserImage  nvarchar(max),Address nvarchar(max))

First we  take Web Appllication which name is ConnectivityWithEntityFrameWork and  Add New folder which  name Data for edmx file  ………………………..


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyRegistration.aspx.cs" Inherits="ConnectivitywithEntityFramework.MyRegistraiont" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>My Regitration</title>

<!--code  for  Preview Image in File Upload  in Java Script....... -- >
<script type="text/javascript" language="javascript">

function PreviewImage() {
OfReader = new FileReader();
OfReader.readAsDataURL(document.getElementById("uploadImage").files[0]);

OfReader.onload = function (oFREvent) {
document.getElementById("uploadPreview").src = oFREvent.target.result;
};
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr><td colspan="3">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</td></tr>
<tr><td>SrNo</td><td><asp:TextBox ID="txtsrno" runat="server"></asp:TextBox></td><td></td></tr>
<tr><td>Name</td><td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td><td></td></tr>
<tr><td>Gender</td><td>
<asp:RadioButtonList ID="rbgender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Selected="True">Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td><td></td></tr>
<tr><td>Qulification</td><td>
<asp:CheckBoxList ID="cblQulification" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>High School</asp:ListItem>
<asp:ListItem>Intermidiate</asp:ListItem>
<asp:ListItem>Graduation</asp:ListItem>
<asp:ListItem>P. G.</asp:ListItem>
</asp:CheckBoxList>
</td><td></td></tr>
<tr><td>Email Id</td><td>
<asp:TextBox ID="txtemailid" runat="server"
ontextchanged="txtemailid_TextChanged" AutoPostBack="True"></asp:TextBox>
<div id="checkusername" runat="server"
style="float:bottom; margin-left:0px" visible="false">
<asp:Image ID="imgstatus" runat="server"
style="height: 17px; width: 17px; padding: 2px;" />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</div>
</td><td></td></tr>
<tr><td>Password</td><td><asp:TextBox ID="txtpassword" runat="server" TextMode="Password" ></asp:TextBox></td><td></td></tr>
<tr><td>Confirm Password</td><td><asp:TextBox ID="txtcpassword" runat="server"  TextMode="Password"  ></asp:TextBox></td><td></td></tr>
<tr><td>Mobile No</td><td><asp:TextBox ID="txtmobileno" runat="server"></asp:TextBox></td><td>
&nbsp;</td></tr>
<tr><td>Your City</td>

<td>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListBox ID="lbcity" runat="server" Width="100px"></asp:ListBox>
<asp:LinkButton ID="lbremove" runat="server" onclick="lbremove_Click">Remove</asp:LinkButton>
<asp:DropDownList ID="ddlcity" runat="server"
> 
<asp:ListItem Selected="True">--Select--</asp:ListItem>
<asp:ListItem>Varanasi</asp:ListItem>
<asp:ListItem>Noida</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Ghaziabad</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="lbadd" runat="server" onclick="lbadd_Click">Add</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel></td><td>



</td></tr>
<tr><td>User Image</td><td>
<asp:Image id="uploadPreview"  runat="server" style="width: 50px; height:50px;" /> &nbsp;
<asp:FileUpload id="uploadImage" runat="server"   type="file" name="myPhoto" onchange="PreviewImage();" />

</td><td>
</td></tr>



<tr><td>Address</td><td>
<asp:TextBox ID="txtaddress" runat="server"  TextMode="MultiLine" ></asp:TextBox>
</td><td></td></tr>
<tr><td></td><td>
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save"
style="height: 26px" />
</td><td></td></tr>
<tr><td></td><td>
<asp:Label ID="lblmessage" runat="server" Visible="False"></asp:Label>
</td><td></td></tr>

</table>

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


Add Ado.Net Entity Model step by step…..

                                       







using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//
using ConnectivitywithEntityFramework.Data;
using System.Drawing;
using System.IO;

namespace ConnectivitywithEntityFramework
{
public partial class MyRegistraiont : System.Web.UI.Page
{
// create instance of Entity which name is DbforEntityFrameWorkEntities..........
DbforEntityFrameWorkEntities dbentity = new DbforEntityFrameWorkEntities();

string qul, city;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSave_Click(object sender, EventArgs e)
{
try
{
foreach (ListItem li in cblQulification.Items)
{
if (li.Selected)
{
qul = qul + li.Value.ToString() + ",";
}
}
qul = qul.Remove(qul.Length - 1);

foreach (var kane in lbcity.Items)
{
city += kane + ",";
}
city = city.Remove(city.Length - 1);

//  code  for  only  upload png,jpg,jpeg format  for  image………………
string exe = Path.GetExtension(uploadImage.FileName).ToLower();
string filename = uploadImage.FileName;
if (exe == ".png" || exe == ".jpg" || exe == ".jpeg")
{
if (uploadImage.FileBytes.Length <= 10855)
{
//  code for  generate unique  name with extension
filename = Guid.NewGuid().ToString() + Path.GetExtension(filename);

//// UserRegistration ur = new UserRegistration();  or

// create instance of table which name UserRegistration  ........
ConnectivitywithEntityFramework.Data.UserRegistration ur = new Data.UserRegistration();
ur.Name = txtname.Text;
ur.Gender = rbgender.SelectedValue;
ur.Qulification = qul;
ur.EmailId = txtemailid.Text;
ur.Password = txtcpassword.Text;
ur.MobileNo = txtmobileno.Text;
ur.CityName = city;
ur.UserImage =filename;
uploadImage.SaveAs(Server.MapPath("~/Image/") + filename);
ur.Address = txtaddress.Text;
dbentity.UserRegistrations.AddObject(ur);
dbentity.SaveChanges();
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Registration  Successfully";
}
else
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "Please upload image less than 10.6 kb";
}
}
else
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "Please select png,jpg,jpeg file for image upload";
}

}
catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = ex.Message;
}
}
// code for remove item from  List Box Control ......
protected void lbremove_Click(object sender, EventArgs e)
{
lbcity.Items.Remove(lbcity.SelectedValue);
}
// code for add item in  List Box Control from drop down selected value  ......
protected void lbadd_Click(object sender, EventArgs e)
{
foreach (ListItem its in lbcity.Items)
{
if (its.Text == ddlcity.SelectedItem.Text)
{
lbcity.Focus();
lbcity.SelectedValue = its.Text;
return;
}

}
lbcity.Items.Add(ddlcity.SelectedItem.Text);
}
// code for  check availability  with emailid
protected void txtemailid_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtemailid.Text))
{
var res = (from obj in dbentity.UserRegistrations where obj.EmailId==txtemailid.Text select obj).FirstOrDefault();
if (res != null)
{
checkusername.Visible = true;
imgstatus.ImageUrl = "/NotAvailable.jpg";
lblStatus.Text = "UserName Already Taken";


}
else
{
checkusername.Visible = true;
imgstatus.ImageUrl = "/Icon_Available.giF";
lblStatus.Text = "UserName Available";
}
}
else
{
checkusername.Visible = false;
}
}
}

1 comments: