Sunday, April 26, 2015

Prevent Enter Click on Text Box using Java Script

When ever in your html if you click Enter key on a text box after typing your text the very next submit button is being hit automatically. Now not every time it causes problems but few times it creates an serious issue to the application.
So how to prevent this issue? With java script we can handle this very easily. Lets see how to do so?

Into a notepad document write down this line.

<input type='text' onkeydown="return (event.keyCode!=13);"/>

onkeydown="return (event.keyCode!=13);" 

This is the main code to prevent the Enter press within the textbox. 

Note: ASCII key of ENTER is 13.

Now save your document, run it in browser and check.

On line demo:



How to upload Database in Godaddy Server, explain with images

As a request form one of my follower I am writing this post about how to upload your Ms. SQL Server Database. Just follow my steps and in new few minutes your database will be on line in your Godaddy server.

Step 1:
First generate a script of your database with data (Schema & data).

Step 2:
Open your Godaddy server login into it and Go to Web Sites and Domain section. There is a link for Database (marked in pic) section. Click on it and you will be redirect to Database section.


Step 3:
Right now there is no database exists. So you have to create a new one. Click on the Add new database button and go to Add New Database page.

Step 4:
Here you have to enter your database name and  choose Microsoft SQL Server from Type drop down list.
Now its come to the user part. Here I don't have an exiting user so I have to create an user to access the database. So enter the user name and password for the user. You can generate the password for user also.
After all entry click on the OK button and if there is no issue you will be redirect to the previous page where you can see that you have a database with your given name and its user.  Now your database is created and you can add your tables, stored procedures, functions, views  and also insert data.

Note: make a note of the provided IP address and pin no. It will use in case of writing connection string in your web.config file.



Step 5:
To modify your database click on the webadmin link on the right side of your database. It will redirect you to a database management tool.



Step 6:
Click on the Tools section and within that on New Query button.


Step 7:
A new editor will be open for you to write sql query in it. Now copy and paste your local database script into this editor and click on the submit button.


Step 8:
After few minutes(as per your database size) your database will be on line. Now go back to your project. You have to change the connection string of you web.config file to attach this on line database.


Add the following connection string:

<add name="mlm_real_1050ConnectionString" connectionString="Data Source=<IP address>,<PIN No>; Initial Catalog=<Database name>;Network Library=DBMSSOCN;User ID=<User Name>;Password=<Password>;"      providerName="System.Data.SqlClient" />

Now upload your project and enjoy with your website.

Sunday, April 19, 2015

Import Export SQL Server database to Ms. Exel

Here in this post I will show you how to retrieve database table from Ms. Excel step by step with images. First create an excel file where all your data will be saved. 


Now saved this excel file into your drive and open Ms SQL Server management studio. Create a new database or you can do this in your existing database.


On clicking the button import a window will be opened with Data Source option. Choose Ms. Excel from here. 


Now click on the next button to choose Destination. 
Choose SQL Server and your database name from the drop down.


Now only few steps are ahead. Click on the Next button and go to Tables and Views Section. Here select the proper sheet of your excel. And change the name of table as you want.


By clicking on next few Next buttons you will get a success message with no of rows transferred.


Now cross check your database. And also your table which have been created during this process.


Now do it with yours and enjoy.

  

Get currency format using Google in ASP.NET

Many of times I have encountered by a vital question, "How to change 20000.00 to $20,000.00". This is nothing but a simple currency formatter. You can use either jquery or server side coding to show the currency in this format. Here in this post I will show you how to format the currency input from user in ASP.NET.

Format Currency using JQuery:
First start with JQuery. Google has provided a very simple way to format currency. You can find it out from here. Or you can follow this one.

To continue you have to download two js files. One is jquery.formatCurrency-1.4.0.js and second one is jquery.min.js. I have attached these js files with the code I have attached along with this post.

So lets start with sample coding. First create a new project and add a new page, named it whatever you want. Then add a new text box. Firstly we will do it with onBlur function of jquery, so we don't need any more extra button for showing our formatted currency.

Add those downloaded js files into your header section of web form. And the paste the following code into your page.

<script src="jquery.min.js"></script>
<script src="jquery.formatCurrency-1.4.0.js"></script>
<script type="text/javascript">
        $(document).ready(function () {
            $('.text').focusout(function () {
                $('.text').formatCurrency();
                $('.text').formatCurrency('.currencyLabel');
            });
        });       
</script>

Now copy the text box.

<div>
     <p>Currency Formatter</p>
     <asp:TextBox runat="server" ID="txtPrice" CssClass="text"></asp:TextBox>
     Show by Jquery: <span class="currencyLabel"></span>
</div>

Check the CssClass of text box. Its the thing by which formatCurrency() method is calling to format it to text box and also show the output value to a span.

Format Currency using C#:
I hope its clear to you how to format currency by JQuery, now lets see how to do this such using C#. Don't worry C# has an inbuilt function for this. For C# we are taking an extra button to display the output into a label.

<asp:TextBox ID="txtCurrency" runat="server"></asp:TextBox>
<asp:Button ID="btnChange" Text="Format" runat="server" OnClick="btnChange_Click"    />
<asp:Label ID="lblShow" runat="server"></asp:Label>

C# Code:

protected void btnChange_Click(object sender, EventArgs e)
{
    lblShow.Text = (Convert.ToDouble(txtCurrency.Text)).ToString("C2");
}

Make sure this method is only applicable to  data type like decimal and double. So you have to add a checking whether user input is bound to numbers.

Download the full source code here.


Friday, April 3, 2015

Jquery get method in MVC 4.0

In previous blog post I had discussed about how to post form data to back end, to controller and then to database, and here in this tutorial session I will show you how to get data asynchronously to server ie controller and from there to database. So how to do this? Honestly speaking its very simple, a few steps and its done.

So lets start by creating a new application, name it as you want. Create a new controller and a view against that controller.

We are going to implement a Login form, it may be any others also. Whatever the form is functionality is same. I choose a login form to show you choose according to your requirement. 

For login form we need two text boxes and one button. So design it quickly.

<body>
    <h2>Login</h2>

    <div id="login">
        <p>
            <input type="text" id="email" /></p>
        <p>
            <input type="password" id="password" /></p>
        <p>
            <input type="button" value="Login" id="btnLogin" /></p>
    </div>
</body>

Now add the jquery method.Before do any type of jquery operation add the jquery api. Here I am using Google api. You can also use it from different other sites(like jquery.com) or you can use it as your own.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

Now we can do coding of jquery.

On the btnLogin click we will call the get method and pass the variable to controller. Before we proceed we have to create a new controller where we can catch the data we are sending via jquery. 

So create  a new controller named Login.

Now write down these jquery code into your view page.

<script>
        function login() {
            var email = document.getElementById("email").value;
            var pass = document.getElementById("password").value;

            $.get("@Url.Action("Login")",
                      {
                          email: email,
                          password: pass
                      }, function (data) {
                          if (data == "1") {
                              window.location.href = '../admin/dashboard';
                          }
                          else {
                              document.getElementById("lblMessage").innerHTML = data;
                          }
                      }
                    );
        }
    </script>

After this add a onclick properties to btnLogin. 

Now its turn for the Login controller.

public ActionResult CheckLogin(string email, string password)
{
      clsUser objUser = new clsUser();
      bool f = objUser.CheckLogin(email, password);
      if (f)
      {
           Session["useremail"] = email;
           return Content("1");
      }
      else
      {
           return Content("Please enter valid email & password !");
      }
}

Here I have created a method CheckLogin under clsUser. In that method I am actually checking whether the user is valid or not. So lets take a look on that method.

public bool CheckLogin(string email, string pass)
{
    bool f = false;

    DataTable dt = new DataTable();
    string sql = "";
    sql = "select id from tblUser where email='"+email+"' and password = '"+pass+"'";
    dt = objServer.GetDataTable(sql);
    if (dt.Rows.Count == 1)
    {
         f = true;
    }
    else
    {
         f = false;
    }
    return f;
}

Here I am using a simple ADO.NET code. Its not the right process to so. I have just used just because to make this short. I highly recommend to use parametrize query to do such thing.

Add a new connection string in web.config  to configure your server with this. And what else? your application is done. You have created it. Now test it and enjoy.

Download the full source code here.


Popular Posts

Pageviews