Thursday 28 March 2013

ASP.NET - A simple Question

Question :


I have a StudentManagement Web application. In this application there is an admin module. Admin wants to send personlaized mail only to those students who have scored less than 30 in Physics.
For this purpose admin has a list of student ids with him.

How can the admin retieve a student email id when he enters Student Id in the To Field??

ASP.NET

SqlConnection ObjCon = new SqlConnection("Data Source=servername;Initial Catalog=TestCon;User ID=sa;Password=sa;Pooling=False");
string str = "SELECT emailid FROM Information WHERE num='" + txtnum.Text + "'";
ObjCon.Open();
SqlCommand ObjCmd = new SqlCommand(str, ObjCon);
SqlDataReader dr = ObjCmd.ExecuteReader();
if (dr.Read())
{
txtEmailId.Text = dr[0].ToString();
}
ObjCon.Close();

ASP.NET MVC


View
input type = "text" id = "EmployeeID" />
<input type = "text" id = "EmployeeEmail" />
<input type = "button" id = "GetEmail" />
<
script type="text/javascript">
     $(function(){
         $("#GetEmail").click(function(){
             var empID = $("#EmployeeID").val();
             $.ajax({
                    url: '@Url.Action("GetEmailID")',
                    type: 'post',
                    data: {EmployeeID:empID},
                    success: function (msg) {alert(msg.result); $("#EmployeeEmail").text(msg.email );},
                    error: function (msg) {}});
                    }
         });
     });
</script>

Controller:
//Declare DBContext object as db....
ActionResult Index()        

{            
return View();        
}     
   
public ActionResult GetEmailID(int EID)
{            
var emailFromdb = db.Employee.Where(x=>x.EmployeeID == EID).Select(x=> x.EmailID);

//You get data from database table Employee Which has columns EmployeeID,EmailID            

return Json(new { email = emailFromdb ,result = "success", JsonRequestBehavior.AllowGet});        

}

No comments:

Post a Comment