Hi micah,
Please refer below sample.
HTML
<div>
Name:
<asp:TextBox ID="txtName" runat="server" />
Country:
<asp:TextBox ID="txtCountry" runat="server" />
<asp:Button ID="Insert" Text="Insert" runat="server" OnClick="Insert_Click" />
<asp:DataList ID="dlCustomers" runat="server" RepeatLayout="Table">
<ItemTemplate>
<table class="table">
<tr>
<td>
<%# Eval("CustomerId") %></b>
<%# Eval("Name") %>,
<%# Eval("Country")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
<script type="text/javascript">
var soundObject = null;
function PlaySound() {
if (soundObject != null) {
document.body.removeChild(soundObject);
soundObject.removed = true;
soundObject = null;
}
soundObject = document.createElement("embed");
soundObject.setAttribute("src", "sounds/sound.wav");
soundObject.setAttribute("hidden", true);
soundObject.setAttribute("autostart", true);
document.body.appendChild(soundObject);
}
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindDataList();
}
}
private void BindDataList()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId,Name, Country FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.dlCustomers.DataSource = dt;
this.dlCustomers.DataBind();
}
}
}
}
protected void Insert_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers(Name, Country) VALUES(@Name, @Country)", con))
{
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ClientScript.RegisterStartupScript(this.GetType(), "UpdateTime", "PlaySound()", true);
}
}
this.BindDataList();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindDataList()
End If
End Sub
Private Sub BindDataList()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId,Name, Country FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.dlCustomers.DataSource = dt
Me.dlCustomers.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub Insert_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Customers(Name, Country) VALUES(@Name, @Country)", con)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
ClientScript.RegisterStartupScript(Me.[GetType](), "UpdateTime", "PlaySound()", True)
End Using
End Using
Me.BindDataList()
End Sub