P
Phil Sweeney
Hello -
I am trying to get started with ASP.NET and c# by creating a simple
database "CRUD" program (CRUD: CREATE REPORT UPDATE DELETE) using a
simple one table contacts database.
I have come a long way, and have a form that seems to be working well
for these functions except for one piece:
When I use the "onclick" for an asp button, which then runs the code
to insert to the database and refresh a datagrid with the new data,
the insert statement (and all the code in the "onclick" piece?) runs
TWICE...???
I am new to ASP and event based VS controls so please forgive me if
there is a simple solution to this problem - but I have searched many
listings and tried many edits to the code and cannot find a solution.
Thanks in advance for any help.
Cheers -
Phil
***
Code snippets below:
****************
WebForm1.aspx
****************
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" Trace="false"
AutoEventWireup="false" Inherits="econtact.WebForm1" %>
*
*
*
<div>
<asp:Label id="lname_label" style="LEFT: 272px; POSITION: absolute;
TOP: 256px"
runat="server">Last Name</asp:Label>
<asp:TextBox id="add_lname_txt" style="Z-INDEX: 104; LEFT: 352px;
POSITION: absolute; TOP: 256px" runat="server"></asp:TextBox>
<asp:TextBox id="add_fname_txt" style="Z-INDEX: 103; LEFT: 96px;
POSITION: absolute; TOP: 256px" runat="server"></asp:TextBox>
<asp:Label id="fname_label" style="LEFT: 16px; POSITION: absolute;
TOP: 256px"
runat="server">First Name</asp:Label>
<asp:Button id="btnAdd" style="Z-INDEX: 105; LEFT: 536px; POSITION:
absolute; TOP: 256px" runat="server"Text="Add" OnClick="btnAdd_Click">
</asp:Button>
</div>
*
*
*
****************
WebForm1.aspx.cs
****************
*
*
*
public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (! IsPostBack)
{
BindGrid();
} //endif
}
private void Page_Init(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
*
*
*
public void btnAdd_Click(object source, EventArgs e)
{
string fname = add_fname_txt.Text;
string lname = add_lname_txt.Text;
string dbconnstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\\pjs\\VS\\webdb.mdb";
string dbsqlstr = "INSERT INTO contacts (first_name, last_name,
user_id) VALUES ('" + fname + "', '" + lname + "', 1)";
try
{
System.Data.OleDb.OleDbConnection conn
= new System.Data.OleDb.OleDbConnection(dbconnstr);
conn.Open();
System.Data.OleDb.OleDbCommand cmd = new
System.Data.OleDb.OleDbCommand();
cmd.CommandText = dbsqlstr;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
cmd.ExecuteReader();
add_fname_txt.Text = null;
add_lname_txt.Text = null;
DataGrid1.EditItemIndex = -1;
cmd.Dispose();
conn.Close();
BindGrid();
}
catch(OleDbException ex)
{
Console.WriteLine("Error: {0}", ex.Errors[0].Message);
throw ex;
}
}
*
*
*
***
End Code Snippets
I am trying to get started with ASP.NET and c# by creating a simple
database "CRUD" program (CRUD: CREATE REPORT UPDATE DELETE) using a
simple one table contacts database.
I have come a long way, and have a form that seems to be working well
for these functions except for one piece:
When I use the "onclick" for an asp button, which then runs the code
to insert to the database and refresh a datagrid with the new data,
the insert statement (and all the code in the "onclick" piece?) runs
TWICE...???
I am new to ASP and event based VS controls so please forgive me if
there is a simple solution to this problem - but I have searched many
listings and tried many edits to the code and cannot find a solution.
Thanks in advance for any help.
Cheers -
Phil
***
Code snippets below:
****************
WebForm1.aspx
****************
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" Trace="false"
AutoEventWireup="false" Inherits="econtact.WebForm1" %>
*
*
*
<div>
<asp:Label id="lname_label" style="LEFT: 272px; POSITION: absolute;
TOP: 256px"
runat="server">Last Name</asp:Label>
<asp:TextBox id="add_lname_txt" style="Z-INDEX: 104; LEFT: 352px;
POSITION: absolute; TOP: 256px" runat="server"></asp:TextBox>
<asp:TextBox id="add_fname_txt" style="Z-INDEX: 103; LEFT: 96px;
POSITION: absolute; TOP: 256px" runat="server"></asp:TextBox>
<asp:Label id="fname_label" style="LEFT: 16px; POSITION: absolute;
TOP: 256px"
runat="server">First Name</asp:Label>
<asp:Button id="btnAdd" style="Z-INDEX: 105; LEFT: 536px; POSITION:
absolute; TOP: 256px" runat="server"Text="Add" OnClick="btnAdd_Click">
</asp:Button>
</div>
*
*
*
****************
WebForm1.aspx.cs
****************
*
*
*
public WebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (! IsPostBack)
{
BindGrid();
} //endif
}
private void Page_Init(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
*
*
*
public void btnAdd_Click(object source, EventArgs e)
{
string fname = add_fname_txt.Text;
string lname = add_lname_txt.Text;
string dbconnstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\\pjs\\VS\\webdb.mdb";
string dbsqlstr = "INSERT INTO contacts (first_name, last_name,
user_id) VALUES ('" + fname + "', '" + lname + "', 1)";
try
{
System.Data.OleDb.OleDbConnection conn
= new System.Data.OleDb.OleDbConnection(dbconnstr);
conn.Open();
System.Data.OleDb.OleDbCommand cmd = new
System.Data.OleDb.OleDbCommand();
cmd.CommandText = dbsqlstr;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
cmd.ExecuteReader();
add_fname_txt.Text = null;
add_lname_txt.Text = null;
DataGrid1.EditItemIndex = -1;
cmd.Dispose();
conn.Close();
BindGrid();
}
catch(OleDbException ex)
{
Console.WriteLine("Error: {0}", ex.Errors[0].Message);
throw ex;
}
}
*
*
*
***
End Code Snippets