Click here to Skip to main content
15,915,611 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Question is how to get output in json format .............

[WebMethod]
C#
public string Inserttblval(int CountryId, string UserName, int IsActive)
{
int rowsInserted;
SqlConnection conn = new SqlConnection(your connectionstring);
string sql = string.Format(@"INSERT INTO [TestDB].[dbo].[Table_1] ([UserName],[CountryId],[IsActive]) VALUES ({0},'{1}',{2})", UserName, CountryId, IsActive);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
rowsInserted = cmd.ExecuteNonQuery();
conn.Close();
cmd = null;
return string.Format("Thank you, {0} number of rows inserted!", rowsInserted);
}





aspx page



XML
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
    <script type="text/javascript">

        $(function () {
            $("[id*=Button1]").click(function () {
                var obj = {};
                obj.countryid= $.trim($("[id*=TextBox2]").val());
                obj.username= $.trim($("[id*=TextBox1]").val());
                obj.isactive= $.trim($("[id*=TextBox3]").val());
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetValues",
                    data: JSON.stringify(obj),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (r) {
                        alert(r.d);
                        document.getElementById('Label1').value = r.d;
                    }
                });
                return false;
            });
        });
    </script>
</head>



XML
<body>
<form id="form1" runat="server">
<div>

<table align="center" class="auto-style1">
<tr>
<td>UserName</td>
<td>
<asp:TextBox ID="TextBox1" runat="server">
</td>
<td> </td>
</tr>
<tr>
<td>CountryId </td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2">IsActive</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox3" runat="server">
</td>
<td class="auto-style2"></td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" />
</td>
<td> </td>
</tr>
<tr>
<td>Result:</td>
<td>
<asp:Label ID="Label1" runat="server" Text="">
</td>
<td> </td>
</tr>
</table>

</div>
</form>
</body>


cs Code


C#
using System.Web.Services;
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static string GetValues(int countryid,string username,int isactive)
    {
        WebService ws = new WebService();
        return ws.Inserttblval(countryid, username, isactive);
    }



}
Posted
Updated 21-Aug-14 23:55pm
v4
Comments
[no name] 22-Aug-14 4:31am    
It's no need to write this line> cmd = null; in your top Inserttblval method

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