Thursday, July 24, 2014

How to Show jQuery Dialog box in Asp.net or MVC

In your View HTML design

<html>
<head><title>jQuery Dialog box Demo</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script>
 $(document).ready(function () {
        $("#btnClick").click(function () {
           $("#divDialog").dialog('open');
           return false;
           });
        $("#divDialog").dialog({
           autoOpen: false,
           width: 900,
           resizable: false,
           modal: true,
           title: "View Content",
           width: "500px"
           });
      });

</script>
</head>
<body>

<div>
<input type="button" id="btnClick"/>
</div>
<div id="divDialog">

//Your Content Here
</div>
</body>
</html>

Message to New Subscribers of this Blog

Hi Guyz..

Thanks to All who were Subscribed in My Blog, But this blog are Posts will Displayed in Social Tab in your Gmail account if you are subscribed with your gmail account. Please notice it.

If your are using other mail accounts will get directly to your mail.

Note:

If you have ideas, queries or any issues on .Net, Please send mail to
dotnetcircle@gmail.com.

Please share it in your social Networks, Stay Tuned.

Thank you.
Dot-net-circle-blogspot.com


How to Send Mails in Asp.net with your gmail account

Call this Method in Your Code and Add your Gmail  account Credentials

protected void SendMail()
{
// Gmail Address from where you send the mail
var fromAddress = "Gmail@gmail.com";
 
    // any address where the email will be sending
var toAddress = YourEmail.Text.ToString();
//Password of your gmail address
 
   string fromPassword = "Password";

string subject = ""; /Your Mail Subject
string body ="";// Your Content

// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}

Wednesday, July 23, 2014

Know the Techniques in Asp.net MVC- How to Reset Session Timeout in MVC??

Requirement:

I want to Reset Session Timeout when user is still active.

1. In my scenario my session timeout is 20 min., when session time is reached before 10 seconds
i am showing a dialog to confirm the user as "Session is going to time out, Do you want to stay in ??". If user is click yes, i want to continue my session with again 20 minutes start from 21st minute, because as per requirement i am saving user session time in database table.

2.  can we set timer for session timeout.


So, Please help me anyone, how to reset session timeout??


Technique:

Write the jQuery script like:


     $(document).ready(function () {
            $("#divdialog").dialog({
                autoOpen: false,
                resizable: false,
                modal: true,
                title: "Session Timeout",
                buttons: {
                    Yes: function () {
                        $.ajax({
                            url: '/<your controller>/SessionTimeout', // Redirects to action method for every 20 minutes.
                         
                            dataType: "json",
                            type: "GET",
                            error: function () {
                                alert(" An error occurred.");
                            },
                            success: function (data) {
                                $("#divdialog").dialog('close');
                                display("stop");
                             
                            }
                        });
                    },
                    Logout: function () {
                        location.href = '/<your controller>/Logout';
                    }
                }
            });
         

        });

        function myFunction() { // Fires every 20 minutes
         
            setInterval(function () {
                $("#divdialog").dialog('open');
             
            }, 1200000);
        }

 and Add Action Method in your controller like:

      public ActionResult SessionTimeout()
        {

            Session.Timeout = Session.Timeout + 20;
         
            return Json("",JsonRequestBehavior.AllowGet);
        }



Know another technique to you?? Just mail to dotnetcircle@gmail.com      

How to bind JSON data to dropdownlist in Asp.net MVC using jQuery

jQuery Script 
-------------------

    $.ajax({
                url: "@Url.Action("GetDDLData","Employer")",
                data: {selectedValue:selectedValue},
                dataType: "json",
                type: "GET",
                error: function () {
                    alert(" An error occurred.");
                },
                success: function (data) {
                    var optionhtml1 = '<option value="' +
                     0 + '">' + "--Select State--" + '</option>';
                    $(".ddlProjectvalue").append(optionhtml1);
                 
                    $.each(data, function (i) {
                     
                        var optionhtml = '<option value="' +
                    data[i].Value + '">' +data[i].Text + '</option>';
                        $(".ddlProjectvalue").append(optionhtml);
                    });
                }
            });

Action Method
------------------

public ActionResult GetDDLData(string selectedValue)
    {
int projectid = Convert.ToInt32(selectedValue);

IEnumerable<SelectListItem> projectslist = (from proj in db.PROJECTs where proj.IS_DELETED == "N" && proj.ID != projectid select proj).AsEnumerable().Select(projt => new SelectListItem() { Text = projt.NAME, Value = projt.ID.ToString() });
var result = new SelectList(projectslist, "Value", "Text", tm.PROJ_ID);
return Json(result, JsonRequestBehavior.AllowGet);
}

How to create Session Id for every Login in Asp.net MVC?

   To create new session id like

    SessionIDManager manager = new SessionIDManager();
    string newSessionId =  manager.CreateSessionID(HttpContext.Current);

Tuesday, July 22, 2014

How to write linq “group by” and get count based on row in Asp.net MVC

QUESTION: I have two tables  timesheet and timesheet_log .
--------------

In timesheet i have three columns id,comp_id,emp_id and in timesheet_log table,
i have three columns id, timesheet_id, stat.

Now I want to group all the "STAT" column by "COMP_ID", for that i wrote query in sql

    select tlog.stat,count(*) as count  from  timesheet ts
    join
    timesheet_log  tlog on ts.id = tlog.timesheet_id
    where comp_id=10//For ex.
    group by tlog.stat

In stat column contains each rows like "Not_Entered","Not_submitted" etc..,
when i executing above query in sql server,i getting result like

result:
Stat                Count
Not_entered      10
Not_Submitted   8...
..................
-------------------

Now, I want to write the query in linq and assign variable count of each row to a varaible like:

if Not_entered count in 10 then
int emp_not_entered= //Count of Not entered.

I have tried in linq like

            var result= (from timesheet in reslandentity.TIMESHEET
                                    join tlog in reslandentity.TIMESHEET_LOG on timesheet.ID equals tlog.TIMESHEET_ID
                                    group tlog by tlog.STAT into g

                                    select   g).ToString();

But not getting what i expected, Help me anyone

MY ANSWER:
---------------------

You can access grouping key (value of STAT in your case) via `Key` property of grouping. To get count of items in each group just call `Count()` on group:

    var result = from timesheet in reslandentity.TIMESHEET
                 join tlog in reslandentity.TIMESHEET_LOG 
                      on timesheet.ID equals tlog.TIMESHEET_ID
                 group tlog by tlog.STAT into g
                 select new { Stat = g.Key, Count = g.Count() };

Keep in mind - this query will return `IQueryable` of some anonymous type with two properties - Stat and Count. Calling `ToString()` makes no sense here, because you will just get type name.