-->

Saturday, April 26, 2014

How to find first day of next month in ASP.NET

If you want to get first day of next month , use DateTime structure with AddMonths( ) method. using this method you can get first day of next month. Now, create a new instance of DateTime structure with some parameter like DateTime(Int32, Int32, Int32). This parameterized method Initializes a new instance of the DateTime structure to the specified year, month, and day. Here you can pass integer 2 in day field. Now, you can delete 1 day from current DateTime object using AddDays (-1) method.

Source Code 

 <form id="form1" runat="server">
    <div>  
        <asp:Button ID="Button1"
        runat="server"
        BackColor="#66CCFF"
        onclick="Button1_Click"
        Text="Click" />  
    </div>
       <asp:Label ID="Label1"
       runat="server"
       BackColor="Yellow"
       Text="Label"></asp:Label>
    </form>

Code Behind

 protected void Button1_Click(object sender, EventArgs e)
        {            
            DateTime today = DateTime.Today;            
            DateTime tempDate = today.AddMonths(1);
            DateTime tempDate2 = new DateTime(tempDate.Year, tempDate.Month, 2);
            DateTime nextmonthfirstday = tempDate2.AddDays(-1);
            Label1.Text = "Today : " + today.ToLongDateString();
            Label1.Text += "<br /><br />next month first day = ";
            Label1.Text += nextmonthfirstday.ToLongDateString();

        }

Code Generate the following output

How to find first day of next month in ASP.NET

Tuesday, April 22, 2014

How to Create DropDownList in Asp.Net MVC

Drop down list is a common and usable control in every programming area to let the user can select only one item from the list. In MVC we have to create a list in action method of controller and then pass it through any method like ViewBag and ViewModel etc.

Programmer can create required type of list as per the requirements, we will create a SelectListItem type of list because this list provides two property Text and Value by default. Create a simple list of type SelectListItem and add some items (we have added four item here) as added below:

List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Value 1", Value = "1" });
items.Add(new SelectListItem { Text = "Value 2", Value = "2" });
items.Add(new SelectListItem { Text = "Value 3", Value = "3" });
items.Add(new SelectListItem { Text = "Other", Value = "4" });

Now to pass this to View page write a single line as written below:

ViewBag.ddlItems = new SelectList(items, "Value", "Text");

Value will be the dataValue field and Text will be dataText field for this dropdownlist. In the view page create a form and of course a dropdownlist with a submit button:

@using (Html.BeginForm())
{
    <div>
        @Html.DropDownList("selectedItem", (IEnumerable<SelectListItem>)ViewBag.ddlItems)
        <input type="submit" value="Submit" id="submitBtn" />
    </div>
}

First parameters of this helper Html.DropDownList() specifies name of the HTML select element, and also the name of the view model property that is bound to the element. Second parameter shows the list of options of select list.

This will create a dropdownlist with a submit button. Run the application and open particular view in the browser. Select any value and then submit. Oops! i haven't write the code to access the selected value. So write HttpPost type of action:

public ActionResult Index(string selectedItem)
{
....
....

Here parameter selectedItem is the same name of dropdownlist we have assigned in the view page. Again run the application, select any value("Value 2") and then submit. This parameter will contain the selected value(2).

How to add Hours in existing time in ASP.NET

Introduction

Using AddHours method  you can add Hours in integer, Object will be updated with new date and time. This method is used where you want to calculate time after some time. Like Library management project. Lets take an simple example

<form id="form1" runat="server">
    <div>  
        <asp:Button ID="Button1"
        runat="server"
        BackColor="#66FFFF"
        onclick="Button1_Click"
        Text="Click" />  
    </div>
        <asp:Label ID="Label1"
        runat="server"
        BackColor="Yellow"></asp:Label>
    </form>
Code Behind
 protected void Button1_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;          
            DateTime after3Hours = now.AddHours(3);            
            DateTime after4Hours = now.AddHours(4);
            Label1.Text = "present time = " + now.ToLongTimeString();
            Label1.Text += "<br /><br />";
            Label1.Text += "after added three hours with present time <br />";
            Label1.Text += after3Hours.ToLongTimeString();
            Label1.Text += "<br /><br />";
            Label1.Text += "after added four hours with present time <br />";
            Label1.Text += after4Hours.ToLongTimeString();

        }
 Code Generate the following Output
How to add Hours in existing time in ASP.NET

Monday, April 21, 2014

Add seconds in existing time ASP.NET example

Introduction

Using AddSeconds method  you can add seconds in integer, Object will be updated with new date and time. This method is used where you want to calculate time after some seconds. Like Library management project. Lets take an simple example

<form id="form1" runat="server">
    <asp:Button ID="Button1"
    runat="server"
    BackColor="#66CCFF"
    onclick="Button1_Click" Text="Click" />
    <div>  
    <asp:Label ID="Label1"
    runat="server"
    BackColor="Yellow"></asp:Label>  
    </div>
    </form>
Code Behind
 protected void Button1_Click(object sender, EventArgs e)
 
        {        
            DateTime now = DateTime.Now;          
            DateTime after56Seconds = now.AddSeconds(56);
            Label1.Text = "present time = " + now.ToLongTimeString();
            Label1.Text += "<br /><br />";
            Label1.Text += "after added 56 seconds with present time= ";
            Label1.Text += after56Seconds.ToLongTimeString();

        }
Code Generate the following output
Add seconds in existing time ASP.NET example

Sunday, April 20, 2014

Doctor Medicine Prescription Project in Windows Forms with C#

Download this project

Project cost : 300Rs or $10
Pay me at:
PayPal id : saini1987tarun@gmail.com

Via bank transfer

ICICI bank account number is :    153801503056
Account holder name is :                 Tarun kumar saini
IFSC code is :                                   ICIC0001538

SBBJ bank detail :                             61134658849
Account holder name:                        Tarun kumar saini
IFSC code :                                        SBBJ0010398

CONTACT ME

narenkumar851@gmail.com

Doctor Medicine Prescription system covers the functionality to store the symptoms of the patient’s disease date by date and also the medicines written by the doctor. It is windows form project which may be a setup file to be installed in client’s system.


Features of the project:


  • Only authorized person can login into the system and use the software.
  • User can add/modify patients, disease and medicines which will stored in the database.
  • Through this system, all the symptoms of particular disease and their medicines may be stored in the database according to date.
  • Small project to be installed in the windows platform.

System Requirements:


  • Visual studio 2010 or higher
  • SqlServer 2008 or higher
  • DotNet Framework 4.0 or higher(pre-loaded with visual studio)

Run the Project:

It is a windows form application, so either press F5 or click on Start button. It will load the login window, which requires username and password. Username is “admin” and password is “password”, click on login button and the manage options window will be shown to you.

Project’s Functions:

Manage List: here all the functions of project can be redirected through a single click.

1. Manage list doctor medicine project

Manage Patient: where user can add/modify all the details about a patient like name, father name, age, address, contact no and also registration date.

2. Manage patient in doctor medicine project

Manage Disease: user can add/modify details about diseases like their name and type.

Manage Symptoms: there are some symptoms of particular disease which should be first stored in the system. It also have related medicine details for each symptoms, which may be prescribed to the patient.

Manage Medicine: where user can add/modify all the details about medicines like their name, type, the salt contains and components including in the medicine.

Prescribe medicine: user can assign the medicines to particular patient according to their symptoms/disease. By a single click the system will store the medicines assigned to the patient.

3. prescribe form doctor medicine project

The coding part is so simple of this project so that other programmer can easily change the functionality as per the requirements.

How to Serve Files to User in Asp.Net MVC

Let the user download a file from server is not a typical task, programmer have to write some lines of code which will execute the task. Asp.Net MVC action with returning type FileResult will complete this task in few task listed in the article.

After uploading file/files on the server, how to deliver/serve a file for user to download and save on an individual system depends on the way of storing the file on the server by programmer. Programmer can provide a simple link to an action including some lines of code to perform the task.

In the controller write below line of c# code in the action named “DownloadFile” which will access the file saved on the root of this project and then prompt user to Open/Save that file.

public FileResult DownloadFile()
{
var file = Server.MapPath("~/download file.txt");
return File(file, "Text", "download file");
}

These two line of code will sufficient to prompt the user, now in view page create an actionline which will redirect the user to this action. The second line of code will creates a System.Web.Mvc.FilePathResult object by using the file name, the content type, and the file download name.

@Html.ActionLink("Download File", "DownloadFile");

Run the project and particular controller/action then click on the link created above. This will show an opening download file window having two option to open or save that file, as shown in the below image:

How to Serve Files to User in Asp.Net MVC

Saturday, April 19, 2014

How to Validate File or File type in Java Script MVC

Programmer need to check whether the file is selected by the user or not, when uploading a file. If user will not upload any file and submit the form, the code will throw an exception with description like "file must not be null".

To overcome this issue programmer have to check the uploaded file before the form submission, and this can be performed by using Java script. Write the following code in our view where the file upload control is placed:

function validateForm() {
if ($("#file").val() == "") {
alert("Please select a file");
return false;
}
}

Here “#file” is the id of file upload control and we are checking the value of this control. If the value is empty then it will alert with the message shown and return back to the form. If this control will have file then it will submit the form. On the submit button, just call this function to execute this code with Onclick=”validateForm();”.

Now to select only some file types write another function as below:

function checkfile(sender) {
        var validExts = new Array(".jpeg", ".jpg", ".png");
        var fileExt = sender.value;
        fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
        if (validExts.indexOf(fileExt) < 0) {
            alert("File must of types \".jpeg\", \".jpg\", \".png\");
            $("#file").val('');
            return false;
        }
        else return true;
    }

This function will be called on OnChange event of the file upload control. It will show an alert message written above if the type of selected file will not be in the list declared in the function. Write CheckFile(this) in onChange event of the file upload control.

In this function we are checking only the extension of the selected file, extension may be found by any individual logic. Earlier article was about to uploading file, this java script will not submit the form if user will not select any file through the file upload control.
© Copyright 2013 Computer Programming | All Right Reserved