C#, ASP.NET web programmers. | Ford Explorer - Ford Ranger Forums - Serious Explorations

  • Register Today It's free! Once registered you can join the Elite Explorers to eliminate ads from the website.

C#, ASP.NET web programmers.

Campo

Elitus Explorus
Joined
September 7, 2001
Messages
715
Reaction score
2
City, State
White City, KS
Year, Model & Trim Level
99 XLT 4x4
All,

I'm working on a school project with asp.NET and MS Access.

I've got this to work, but I can't figure out how to get the price variable in the add record form to ignore any charecters exept numbers. It's the btnAddPart_Click method.

I figured out how to do the same thing to update records (dgParts_update method), but it doesn't work for insert..

Any help would be greatly appreciated.
TIA

Here's the code

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Parts Management</title>
<script runat="server">
string sqlConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\wwwroot\\Parts\\data1\\PartsAreUs.mdb";
protected void Page_Load(Object Src, EventArgs e)
{
if (!IsPostBack)
{
DoConnect();
}
}

private void DoConnect()
{

OleDbConnection PartConn = new OleDbConnection(sqlConn);
OleDbCommand selCMD = new OleDbCommand("Select * from Parts", PartConn);
OleDbDataAdapter sda = new OleDbDataAdapter();
sda.SelectCommand = selCMD;
DataSet ds = new DataSet();
sda.Fill(ds, "Parts");
dgParts.DataSource=ds.Tables["Parts"].DefaultView;
dgParts.DataBind();

}

public void dgParts_Edit(Object sender, DataGridCommandEventArgs e)
{

dgParts.EditItemIndex = e.Item.ItemIndex;
DoConnect();

}

public void dgParts_Cancel(Object sender, DataGridCommandEventArgs e)
{

dgParts.EditItemIndex = -1;
DoConnect();
}

public void dgParts_Update(Object sender, DataGridCommandEventArgs e)
{

string sPartID, sPartName, sPartDescription, sPartPrice;
sPartID = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
sPartName = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
sPartDescription = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
sPartPrice = ((TextBox)e.Item.Cells[4].Controls[0]).Text;

string sSqlCmd = "UPDATE Parts SET Name = ?, Description = ?, Price = ? WHERE PartID = ?";

OleDbConnection updCon = new OleDbConnection(sqlConn);
OleDbCommand updCmd = new OleDbCommand(sSqlCmd, updCon);

string sPrice = sPartPrice.Remove(0, 1);

updCmd.Parameters.Add(new OleDbParameter("@PartName", OleDbType.VarWChar, 50));
updCmd.Parameters["@PartName"].Value = sPartName;
updCmd.Parameters.Add(new OleDbParameter("@PartDescription", OleDbType.VarWChar, 100));
updCmd.Parameters["@PartDescription"].Value = sPartDescription;
updCmd.Parameters.Add(new OleDbParameter("@PartPrice", OleDbType.Currency));
updCmd.Parameters["@PartPrice"].Value = sPrice;
updCmd.Parameters.Add(new OleDbParameter("@PartID", OleDbType.Integer));
updCmd.Parameters["@PartID"].Value = sPartID;

updCon.Open();
updCmd.ExecuteNonQuery();
updCon.Close();

//lblMsg.Text = "You chose to update Item # " + dgParts.EditItemIndex;

dgParts.EditItemIndex = -1;
DoConnect();

}

public void dgParts_Delete(Object sender, DataGridCommandEventArgs e)
{

string delSQL = "DELETE FROM Parts WHERE PartID = ?";

OleDbConnection delCon = new OleDbConnection(sqlConn);
OleDbCommand delCmd = new OleDbCommand(delSQL, delCon);

delCmd.Parameters.Add(new OleDbParameter("@PartID", OleDbType.Integer));
delCmd.Parameters["@PartID"].Value = dgParts.DataKeys[(int)e.Item.ItemIndex];

delCon.Open();
delCmd.ExecuteNonQuery();
delCon.Close();


dgParts.EditItemIndex = e.Item.ItemIndex;
DoConnect();

}


public void btnAddPart_Click(object Src, EventArgs e)
{
string inSQL = "Insert INTO Parts (Name, Description, Price) VALUES (?, ?, ?)";

OleDbConnection insCon = new OleDbConnection(sqlConn);
OleDbCommand insCmd = new OleDbCommand(inSQL, insCon);


insCmd.Parameters.Add(new OleDbParameter("@PartName", OleDbType.VarWChar, 50));
insCmd.Parameters["@PartName"].Value = txtPartName.Text;
insCmd.Parameters.Add(new OleDbParameter("@PartDescription", OleDbType.VarWChar, 100));
insCmd.Parameters["@PartDescription"].Value = txtPartDescription.Text;
insCmd.Parameters.Add(new OleDbParameter("@PartPrice", OleDbType.Currency));
insCmd.Parameters["@PartPrice"].Value = txtPartPrice.Text;

insCon.Open();
insCmd.ExecuteNonQuery();
insCon.Close();

DoConnect();
txtPartName.Text="";
txtPartDescription.Text="";
txtPartPrice.Text="";
pnlAddPart.Visible = false;
btnAddRecord.Visible = true;


}

public void btnAddRecord_Click(Object Src, EventArgs E)
{
btnAddRecord.Visible = false;
pnlAddPart.Visible = true;
}

public void btnCancelAdd_Click(object Src, EventArgs e)
{

txtPartName.Text="";
txtPartDescription.Text="";
txtPartPrice.Text="";
btnAddRecord.Visible = true;
pnlAddPart.Visible = false;

}

protected void ConfirmDelete(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
WebControl button = (WebControl) e.Item.Cells[5].Controls[0];
button.Attributes.Add("onclick", "return confirm (\"Are you sure you want to permanently delete this part ?\");");
}
}

protected void dgParts_Sort(Object Src, DataGridSortCommandEventArgs e)
{
BindGrid(e.SortExpression);
}

public void BindGrid(string sortfield)
{
OleDbConnection srtCon = new OleDbConnection(sqlConn);
OleDbDataAdapter srtDA = new OleDbDataAdapter("Select * from Parts", srtCon);
DataSet srtDS = new DataSet();
srtDA.Fill(srtDS);
DataView view = srtDS.Tables[0].DefaultView;
view.Sort = sortfield + " ASC";
if (sortfield == "Price")
{
view.Sort = sortfield + " DESC";
}
dgParts.DataSource = view;
dgParts.DataBind();
}


</script>

</head>

<body>

<h2>Parts Administration</h2>

<hr>

<form runat="server">
<asp:Button ID="btnAddRecord" runat="server" Text="Add Record" OnClick="btnAddRecord_Click" Visible="true" />
<asp:panel ID="pnlAddPart" runat="server" Visible="false">
<table width="100%" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><div align="center">Part name</div></td>
<td><asp:textbox id="txtPartName" runat="server" /><asp:RequiredFieldValidator ControlToValidate="txtPartName" Display="Dynamic" ErrorMessage="Part Name is Required" ID="rtvPartName" runat="server" Text="*" /></td>
</tr>
<tr>
<td><div align="center">Part Description</div></td>
<td><asp:textbox id="txtPartDescription" runat="server" /></td>
</tr>
<tr>
<td><div align="center">Part Price</div></td>
<td><asp:textbox id="txtPartPrice" runat="server" /><asp:RequiredFieldValidator ControlToValidate="txtPartPrice" Display="Dynamic" ErrorMessage="Part Price is Required" ID="rfvPartPrice" runat="server" Text="*" /></td>
</tr>
<tr>
<td>
<div align="center"><asp:Button ID="btnAddPart" Text="Insert Part" runat="server" OnClick = btnAddPart_Click />
</td>
<td>
<div align="center"><asp:Button ID="btnCancelAdd" Text="Cancel" runat="server" CausesValidation="false" OnClick = btnCancelAdd_Click />
</td>
</tr>
</table>
<asp:ValidationSummary DisplayMode="BulletList" HeaderText="The Following Errors Occured:" ID="vsSummary" runat="server" />
</asp:panel>
<br>
<asp:DataGrid
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
DataKeyField="PartID"
HorizontalAlign="Center"
id="dgParts"
runat="server"
Showheader="true"
Width="85%"
allowSorting="true"
OnUpdateCommand="dgParts_Update"
OnEditCommand="dgParts_Edit"
OnCancelCommand="dgParts_Cancel"
OnDeleteCommand="dgParts_Delete"
OnItemCreated = "ConfirmDelete"
OnSortCommand = "dgParts_Sort"
GridLine="Both">
<HeaderStyle
HorizontalAlign="Center"
BackColor="#E8EBFD"
ForeColor="#3D3DB6"
Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true"
Font-Size="smaller" />
<ItemStyle
BackColor="#F2F2F2"
Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Size="smaller" />
<AlternatingItemStyle
BackColor="#E5E5E5"
Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Size="smaller" ForeColor="#0000FF" />
<FooterStyle
HorizontalAlign="center"
BackColor="#E8EBFD"
ForeColor="#3D3DB6"
Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true"
Font-Size="smaller" />
<Columns>
<asp:editcommandcolumn ButtonType="pushbutton"
runat="server"
CancelText="Cancel"
EditText="Edit"
UpdateText="Update"
HeaderText="Edit">
</asp:editcommandcolumn>
<asp:boundcolumn
DataField="PartID"
HeaderText="PartID"
SortExpression="PartID"
runat="server"
ReadOnly="false">
</asp:boundcolumn>
<asp:boundcolumn
DataField="Name"
HeaderText="Name"
SortExpression="Name"
runat="server">
</asp:boundcolumn>
<asp:boundcolumn
DataField="Description"
HeaderText="Description"
runat="server">
</asp:boundcolumn>
<asp:boundcolumn
DataField="Price"
HeaderText="Price"
SortExpression="Price"
runat="server"
DataFormatString="{0:c}">
</asp:boundcolumn>
<asp:ButtonColumn ButtonType="pushbutton"
CommandName="Delete"
Text="Delete"
runat="server">
</asp:ButtonColumn>
</Columns>
</asp:datagrid>
</form>

</body>
</html>
 



Join the Elite Explorers $20 each year.
Elite Explorer members see no advertisements, no banner ads, no double underlined links.
Add an avatar, upload photo attachments, and more!
.



Rick

Pumpkin Pilot
Staff member
Admin
Elite Explorer
Joined
February 8, 1999
Messages
34,010
Reaction score
2,225
City, State
Wayoutin, Aridzona
Year, Model & Trim Level
'93 XL Pumpkin Edition
Callsign
AB7FH
Can't help you, but I'll bump this for you ;)
 






Jefe

Explorer Addict
Joined
January 7, 2001
Messages
4,856
Reaction score
9
City, State
Clarksville, TN
Year, Model & Trim Level
'00 XLT 4x4
Rick said:
Can't help you, but I'll bump this for you ;)
Thanks for the bump Rick. I was going to take a look at this when I had more time (ie not at work :p ).

I'll play w/ it if you can send the MS DB -> [email protected]

Since I don't want to do your homework for you I'll start with some hints.
Getting formatted text through a SQL query can be trick sometimes, especially going into Access. Might be something as simple as formatting it slightly different (like hacking the $ off), or bringing it in as a different type of variable (rather than a string).
 






Campo

Elitus Explorus
Joined
September 7, 2001
Messages
715
Reaction score
2
City, State
White City, KS
Year, Model & Trim Level
99 XLT 4x4
Thank's for not letting it die off guy's.

The required book for the class cover's mostly PHP and a little ASP.NET, but I was able to hunt and guess how to do it since it's kinda like java

Just incase anyone want's to know, I added this to the btnAddPart_Click method right before the insert parameters..

string sPartPrice;
string sPrice = txtPartPrice.Text;

int at = sPrice.IndexOf("$");
if (at == 0)
{
sPartPrice = sPrice.Remove(at, 1);
}
else
{
sPartPrice = txtPartPrice.Text;
}

Again, thank's, Do you still want the DB Jefe?
 












Top