<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data" %>
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html><head>
<title>TechRepublic.com - Test</title>
</head><body>
<script language="C#" runat="server">
private SqlConnectionconn= null;
private SqlCommandcmd =null;
private String connString;
private intrvalue;
public void SubmitChanges() {
connString = "data source=localhost;uid=test;
pwd=test;initial catalog=Northwind";
try {
conn = new SqlConnection(connString);
cmd = new SqlCommand("sp_UpdateCustomerPhone", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.NVarChar, 5);
cmd.Parameters["@id"].Value = lblID.Text;
cmd.Parameters.Add("@phone", SqlDbType.NVarChar, 24);
cmd.Parameters["@phone"].Value = txtPhone.Text;
cmd.Parameters.Add("@retvalue",System.Data.SqlDbType.Int);
cmd.Parameters["@retvalue"].Direction= ParameterDirection.Output;
conn.Open();
cmd.ExecuteNonQuery();
rvalue = (int)cmd.Parameters["@retvalue"].Value;
if (rvalue == -1) {
lblMessage.Text = "Database error duringupdate.";
lblMessage.Visible = True;
} else {
lblMessage.Text = "Data has been updated.";
lblMessage.Visible = True;
}
conn.Close();
} catch (SqlException ex) {
lblMessage.Text = "Error accessing database:" + ex.ToString();
} catch (Exception ex) {
lblMessage.Text = "Exception: " + ex.ToString();
} finally {
if (conn.State == ConnectionState.Open){
conn.Close();
}
conn.Dispose();
} }
private void btnUpdatePhone_Click(object sender,
System.EventArgs e) {
SubmitChanges();
}
</script>
<form id="frmTestUpdate"method="post" runat="server">
<asp:Label ID="lblMessage"Visible="False"
runat="server"></asp:Label><br /><br />
<asp:Label ID="lblPhone"runat="server">New Number:</asp:Label>
<asp:TextBox ID="txtPhone"runat="server" /><br/><br />
<asp:Button ID="btnUpdatePhone"Text="Update"
OnCommand="btnUpdatePhone_Click"
runat="server" />
<asp:Label ID="lblID"Visible="False"
runat="server">ALFKI</asp:Label>
</form></body></html> |