Insert values to database tables through AJAX (without jquery)

Posted by Sina under ASP.NET AJAX on 6/5/2013 | Points: 10 | Views : 2708 | Status : [Member] | Replies : 3
Hi,i want to insert values to database tables from dropdownlist, textbox on button click using ajax, javascript in C#. How can it be done without jquery and stored proc?

@Sina


Responses

Posted by: Imteyazkhan374 on: 6/5/2013 [Member] Starter | Points: 25

Up
0
Down
Hi, Try this

Create a table :
create table student(name varchar(30), class varchar(10))

Html Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="97px">
<asp:ListItem Selected="True">Sem-I</asp:ListItem>
<asp:ListItem>Sem-II</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</ContentTemplate>
</asp:UpdatePanel>
<p>
&nbsp;</p>

</form>
</body>
</html>


C# Code-

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

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mydatabase;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("insert student values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.ToString() + "')", con);
cmd.ExecuteNonQuery();
Label1.Text = "Inserted Successfully.";
}
}

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sina on: 6/5/2013 [Member] Starter | Points: 25

Up
0
Down
but i have a gridview and it should be updated with the entered values on the button click (without refreshing/reloading the page)

@Sina

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Imteyazkhan374 on: 6/5/2013 [Member] Starter | Points: 25

Up
0
Down
Try this:

HTML Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="97px">
<asp:ListItem Selected="True">Sem-I</asp:ListItem>
<asp:ListItem>Sem-II</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<p>
&nbsp;</p>



</form>
</body>
</html>


aspx.cs code:

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

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mydatabase;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("insert student values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.ToString() + "')", con);
int i=cmd.ExecuteNonQuery();
if (i > 0)
{
Label1.Text = "Inserted Successfully.";
string[] row = new string[] { TextBox1.Text, DropDownList1.SelectedItem.ToString() };
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Items");
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Class", Type.GetType("System.String"));
dt.Rows.Add(row);
GridView1.DataSource = ds;
GridView1.DataBind();
ds.Dispose();
dt.Dispose();
}
}
}

Sina, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response