Hi micah,
I have created sample code which full-fill your requirement.So please refer the below code.
HTML
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<asp:TextBox ID="txtcountry" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</td>
</tr>
</table>
<br />
<div>
<asp:DataList ID="dlGetresult" runat="server" RepeatColumns="3" RepeatDirection="Horizontal">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
CustomerId:<asp:Label ID="lblCustomerId" Text='<%#Eval("CustomerId")%>' runat="server" />
</td>
</tr>
<tr>
<td>
Name:<asp:Label ID="lblName" Text='<%#Eval("Name")%>' runat="server" />
</td>
</tr>
<tr>
<td>
Country:<asp:Label ID="lblCountry" Text='<%#Eval("Country")%>' runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
<div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=btnSubmit]').click(function () {
var obj = {};
obj.name = $('[id*=txtName]').val();
obj.country = $('[id*=txtcountry]').val();
$.ajax({
type: "POST",
url: "Default.aspx/SaveRecord",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
},
error: function (response) {
}
});
});
});
</script>
</div>
</form>
C#
private string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
this.GetData();
}
private void GetData()
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("Select * from Customers", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dlGetresult.DataSource = dt;
dlGetresult.DataBind();
}
[System.Web.Services.WebMethod]
public static void SaveRecord(string name, string country)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("Insert into Customers Values('" + name + "','" + country + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Vb.Net
Private constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(sender As Object, e As EventArgs)
Me.GetData()
End Sub
Private Sub GetData()
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand("Select * from Customers", con)
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
dlGetresult.DataSource = dt
dlGetresult.DataBind()
End Sub
<System.Web.Services.WebMethod> _
Public Shared Sub SaveRecord(name As String, country As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand((Convert.ToString((Convert.ToString("Insert into Customers Values('") & name) + "','") & country) + "')", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Screenshot