ASP.NET (35) SQL (25) JAVASCRIPT (24) HTML (14) STYLE SHEET (6) ASP (4) SCRIPT (1)

Search me out HERE!!

Autofill textbox with Ajax along with value

See below steps to Autofill textbox with Ajax with list of items along with value. Also value will be fetched once any of item is selected from list.

1. Add onkeyup event on textbox to autofill textbox as user start typing in textbox.

onkeyup=KeyUpEvent('txtSearch','1');


2. Add below javascript method which will be called on keyup event of textbox. This method will be used to make Ajax call and get item list with value.

function KeyUpEvent(txtBoxId, id) {
    try {
        if (txtBoxId != null) {
            $("#" + txtBoxId).autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: 'PageName.aspx/ListItemKeyValue',
                        data: "{'prefix':'" + $('#' + txtBoxId).val() + "','Parameter1':'" + id + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) { return data; },
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                   label: item.split('/')[0],
                                   val: item.split('/')[1]
                                }
                            }))
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                        }
                    });
                },

                select: function (event, ui) { alert(ui.item.val); return false; },
                minLength: 3
            });
        }
    }
    catch (e) {
       alert('Error: ' + e);
    }
}


3. Write below web method in .cs file of page to fetch item from database based on value passed from Javascript method.

[WebMethod]
public string[] ListItemKeyValue(string prefix, int Parameter1)
{
    List items;
    Data objData = new Data();
   
//Fetch data into Datatable    System.Data.DataTable dt = objData.GetListItemKeyValue(Parameter1, prefix).Tables[0];
    items = new List(dt.Rows.Count);
    for (int intI = 0; intI < items.Capacity; intI++)
    {
       items.Add(string.Format("{0}/{1}", dt.Rows[intI]["Name"].ToString(), dt.Rows[intI]["Value"].ToString()));
    }
    return items.ToArray();
}

Importing a CSV file into your database with SQL Server Management Studio

While bulk copy and other bulk import options are not available on the SQL servers, you can import a CSV formatted file into your database using your SQL Server Management Studio.

You first need to create a table in your database in which you will be importing the CSV file. After the table is created, follow the steps below.
  1. Log into your database using SQL Server Management Studio
  2. Right click on your database and select Tasks -> Import Data
  3. Click the Next > button
  4. For the Data Source, select Flat File Source. Then use the Browse button to select the CSV file. Spend some time configuring how you want the data to be imported before clicking on the Next > button
  5. For the Destination, select the correct database provider (e.g. for SQL Server 2012, you can use SQL Server Native Client 11.0).  Enter the Server name; check Use SQL Server Authentication, enter the User name, Password, and Database before clicking on the Next > button
  6. On the Select Source Tables and Views window, you can Edit Mappings before clicking on the Next > button
  7. Check Run immediately and click on the Next > button
  8. Click on the Finish button to run the package

NULLIF and ISNULL in SQL

ISNULL
Syntax: ISNULL(expression, value)
Explanation: This function will check expression value and if expression is NULL then it will replace it will given value.
Example: SELECT ISNULL(Income,0) AS Income FROM EmpDetail
Here if Income is NULL for any employee then it will return as 0.

NULLIF
Syntax: NULLIF(expression, value)
Explanation: This function will check expression value and if expression = value then it will replace it with NULL (This function is opposite of ISNULL).
Example: SELECT NULLIF(Income,0) AS Income FROM EmpDetail
Here if Income is 0 for any employee then it will return as NULL.

Disable right click on web page using JQuery

Use below JQuery code to disable right click on web page. This will work well on all browser (IE, Firefox, Google chrome).

// To prevent right click
$(document).bind('contextmenu', function (e) {
        e.preventDefault();
        return false;
});

 

Disable saving web page with CTRL-S using JQuery

Use below JQuery code to disable saving web page using "CTRL-S" keyword directly. This will work well on all browser (IE, Firefox, Google chrome).

// To prevent saving web site directly using "ctrl-s"
$(document).bind('keydown keypress', 'ctrl+s', function () {
        $('#save').click();
        return false;
    });

Take backup of Single Table in SQL Server and MySql

Using below query, you can take backup of table in SQL Server:
SELECT * INTO [NewTableName] FROM [BackupTable]

Using below query, you can take backup of table in MySql:
CREATE TABLE [NewTableName] SELECT * FROM [BackupTable]

Getting selected value from RadioButtonList in Javascript


Consider below ASP.Net RadioButtonList control:
<asp:RadioButtonList ID="rbList" runat="Server">
   <asp:ListItem Text="Kapil" Value="1" Selected="True">
   <asp:ListItem Text="Baheti" Value="2">
</asp:RadioButtonList>

You can now get selected radio button value using below javascript:
function GetRadioButtonValue()
{
  var id = <%= rbList.ClientID %>;
  var radio = document.getElementsByName(id);
  for (var i = 0; i < radio.length; i++)
     {
        if (radio[i].checked)
           alert(radio[i].value);
     }
}