Click here to Skip to main content
15,913,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friendes,

Could anybody can help me how to inser data into SQL Database from Jquery using Icallbackeventhandler, please find below my codes wich are not working correctly, please revert with corrected code.

Client Side Code

C#
function CallService() {
    // Creating variables to hold data from textboxes
    var EmpNo = $('#content_TextBox1').val();
    var EmpName = $('#content_TextBox2').val();
    var Designation = $('#content_DropDownList1').val();
    var Department = $('#content_DropDownList2').val();
    var Salary = $('#content_TextBox3').val();
    var Gender = $('#content_RadioButtonList1 input:checked').val();
    var Preference = $('#content_CheckBoxList1 input:checked').val();
//    var data= JSON.string

    $.ajax({
        type: "POST",
        url: "New.aspx/Insertdata",
        data: "{ 'EmpNo': '" + EmpNo + "','EmpName': '" + EmpName + "', 'Designation': '" + Designation + "','Department':'" + Department + "','Salary': '" + Salary + "', 'Gender': '" + Gender + "','Preference':'" + Preference + "'}",
        contentType: "application/json",
        async: false,
        success: function (data) {
            $('#message').text(data.d);
        }
    });
}



Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

public partial class New : System.Web.UI.Page,ICallbackEventHandler
{
private string returnData;
int rowsInserted = 0;

string conn = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RegisterCallBack();
}
}

#region ICallbackEventHandler Members

public string GetCallbackResult()
{
return returnData;
}

public void RaiseCallbackEvent(string eventArgument)
{
string[] str = eventArgument.Split('|');
string EmpNo=str[0];
string EmpName=str[1];
string Designation =str[2];
string Department =str[3];
decimal salary = Convert.ToDecimal(str[4]);
string Gender=str[5];
string Preference = str[6];

returnData= eventArgument+"|"+cmd.ToString();
//Insertdata(str[0], str[1],str[2],str[3],str[4],str[5],str[6]);
}

#endregion
private void RegisterCallBack()
{

string scallBack = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveFromServer", "context");
string sclientFunction = "function GetCallBack(arg, context){ " + scallBack + "; }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "GetCallBack", sclientFunction, true);
}

private void Insertdata(string EmpNo,string EmpName,string Designation, string Department,decimal Salary,string Gender, string Preference )
{
SqlConnection con = new SqlConnection(conn);
cmd = new SqlCommand("EmpInsertUpdate", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parm1 = new SqlParameter("@EmpNo", SqlDbType.VarChar);
SqlParameter parm2 = new SqlParameter("@EmpName", SqlDbType.VarChar);
SqlParameter parm3 = new SqlParameter("@Designation", SqlDbType.VarChar);
SqlParameter parm4 = new SqlParameter("@Department", SqlDbType.VarChar);
SqlParameter parm5 = new SqlParameter("@Salary", SqlDbType.Decimal);
SqlParameter parm6 = new SqlParameter("@Gender", SqlDbType.VarChar);
SqlParameter parm7 = new SqlParameter("@Preference", SqlDbType.VarChar);

parm1.Value = TextBox1.Text;
parm2.Value = TextBox2.Text;
parm3.Value = DropDownList1.SelectedItem.Text;
parm4.Value = DropDownList2.SelectedItem.Text;
parm5.Value = Convert.ToDecimal(TextBox3.Text);
parm6.Value = RadioButtonList1.SelectedItem.Text;
parm7.Value = CheckBoxList1.SelectedItem.Text;

cmd.Parameters.Add(parm1);
cmd.Parameters.Add(parm2);
cmd.Parameters.Add(parm3);
cmd.Parameters.Add(parm4);
cmd.Parameters.Add(parm5);
cmd.Parameters.Add(parm6);
cmd.Parameters.Add(parm7);

cmd.Connection = con;
con.Open();
rowsInserted = cmd.ExecuteNonQuery();
con.Close();
cmd = null;
}


}

Source Code

XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Mysite.master" AutoEventWireup="true" CodeFile="New.aspx.cs" Inherits="New" %>

<asp:Content ID="Content1" ContentPlaceHolderID="content" Runat="Server">
    <script src="Images/JqueryScript/jquery-1.6.2.js" type="text/javascript"></script>
    <script type="text/javascript" src="JScript.js"></script>
    <div class="divList">Welcome to New Page </div>
<div style="padding-bottom:30px;">
    <asp:HyperLink ID="HyperLink1" runat="server" Font-Bold="true" NavigateUrl="~/Home.aspx">
    Back To Home</asp:HyperLink>
    </div>


 <div style="padding-bottom:25px;">

<div class="lable1">
<asp:Label ID="Label1" runat="server" Text="EmpNo:"></asp:Label>
</div>

<div class="TextBox">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
<div class=" Validation1">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="TextBox1" ValidationGroup="Save" ErrorMessage="Must Enter Emp Number"></asp:RequiredFieldValidator>
<span><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ValidationGroup="Save" ValidationExpression="^[A-Z a-z 0-9]+$" ErrorMessage="Only Alphanumeric values are allowed">
</asp:RegularExpressionValidator></span>
</div>
</div>


 <div style="padding-bottom:25px;">
<div class="lable1">
<asp:Label ID="Label4" runat="server" Text="EmpName:"></asp:Label></div>
<div class="TextBox"><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
<div class=" Validation1">
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="TextBox2" ValidationGroup="Save"  ErrorMessage="Must Enter Emp Name"></asp:RequiredFieldValidator>
<span><asp:RegularExpressionValidator ID="RegExpValidator" runat="server" ControlToValidate="TextBox2" ValidationGroup="Save" ValidationExpression="^[A-Z a-z]+$" ErrorMessage="Only Charecters and white space are allowed">
</asp:RegularExpressionValidator></span>
</div>

</div>


<div style="padding-bottom:25px;">
<div class="lable1">
<asp:Label ID="Label2" runat="server" Text="Designation:"></asp:Label>
</div>
<div class="DDl"><asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True">Select Designation</asp:ListItem>
<asp:ListItem Text="Manager" Value="Manager"></asp:ListItem>
<asp:ListItem Text="Asst Manager" Value="Asst Manager"></asp:ListItem>
<asp:ListItem Text="Sales Rep" Value="Sales Rep"></asp:ListItem>
 </asp:DropDownList></div>
  <div class="Validation1">
  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="Save" ControlToValidate="DropDownList1" ErrorMessage="Select Designation" InitialValue="Select Designation">
 </asp:RequiredFieldValidator>
 </div>

</div>


<div style="padding-bottom:25px;">
<div class="lable1">
<asp:Label ID="Label3" runat="server" Text="Department:"></asp:Label>
</div>
<div class="DDl"><asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Selected="True">Select Department</asp:ListItem>
<asp:ListItem Text="Accounts" Value="Accounts"></asp:ListItem>
<asp:ListItem Text="Sales" Value="Sales"></asp:ListItem>
 </asp:DropDownList>
 </div>

 <div class="Validation1">
  <asp:RequiredFieldValidator ID="ReqFieValidator1" runat="server" ValidationGroup="Save" ControlToValidate="DropDownList2" ErrorMessage="Select Department" InitialValue="Select Department">
 </asp:RequiredFieldValidator>
 </div>
</div>


<div style="padding-bottom:60px;">
    <div class="lable1">
    <asp:Label ID="Label5" runat="server" Text="Salary:"></asp:Label>
</div>
<div class="TextBox">
<asp:TextBox ID="TextBox3" runat="server" ValidationGroup="Save"></asp:TextBox>
</div>
<div class="Validation1">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ValidationGroup="Save" ControlToValidate="TextBox3" ErrorMessage=" Enter salary with numeric values"></asp:RequiredFieldValidator>
<span><asp:RangeValidator ID="RangeValidator2" runat="server" Type="Double" ValidationGroup="Save" ControlToValidate="TextBox3" MinimumValue="10000.00" MaximumValue="60000.00" ErrorMessage =" Must be in the range between 20000.00 to 50000.00"></asp:RangeValidator></span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ValidationGroup="Update" ControlToValidate="TextBox3" ErrorMessage=" Update salary with numeric values only"></asp:RequiredFieldValidator>
<span><asp:RangeValidator ID="RangeValidator1" runat="server" Type="Double" ValidationGroup="Update" ControlToValidate="TextBox3" MinimumValue="10000.00" MaximumValue="60000.00" ErrorMessage =" Must be in the range between 20000.00 to 50000.00"></asp:RangeValidator></span>
</div>
</div>



<div style="padding-bottom:50px;">
 <div class="lable1">
 <asp:Label ID="Label7" runat="server" Text="Gender:"></asp:Label>
</div>
<div class="radio">
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</div>

<div class="Validation">
  <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"  ValidationGroup="Save" ControlToValidate="RadioButtonList1" ErrorMessage="Select Gender">
 </asp:RequiredFieldValidator>
</div>
</div>


<div style="padding-bottom:70px;">
<div class="lable1">
 <asp:Label ID="Label8" runat="server" Text="Preference:"></asp:Label>
</div>
<div class="Check">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Coffee</asp:ListItem>
<asp:ListItem>Tea</asp:ListItem>
<asp:ListItem>Cool Drink</asp:ListItem>
</asp:CheckBoxList>
</div>

<div class="Validation">
<asp:CustomValidator ID="CustomValidator1" ClientValidationFunction="checkIfSelected" runat="server" ValidationGroup="Save" ErrorMessage="Select an option"></asp:CustomValidator>
</div>
</div>


<div style="padding-bottom:22px;">
<div class="button">
<%--<asp:Button ID="Button1" runat="server" Text="Save" Width="100px" Font-Bold="true" ValidationGroup="Save"/>--%>
<asp:Button ID="Button1" runat="server" Text="Save" OnClientClick="javascript:CallService();return false;" Width="100px" Font-Bold="true" ValidationGroup="Save"/>
</div>

<div class="button">

<asp:Button ID="Button2" runat="server" Text="New"  Width="100px" Font-Bold="true" onclick="Button2_Click"/>
</div>

<div class="button">

<asp:Button ID="Button3" runat="server" Text="Update" ValidationGroup="Update"  Width="100px" Font-Bold="true"/>

</div>

<div class="button">

<asp:Button ID="Button4" runat="server" Text="Delete" Width="100px" Font-Bold="true"/>
</div>
<%--<div class="button">

<asp:Button ID="Button5" runat="server" Text="Reset" OnClick="Button5_Click" Width="100px" Font-Bold="true"/>
</div>--%>

<div style="padding-bottom:30px; padding-left:20px;"   ></div>
<div class="label5">
 <asp:Label ID="Label6" runat="server" Enabled="false"></asp:Label>
</div>
</div>
</asp:Content>



Thanks&Regards
Sreeram Dasari
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900