|
Subject:
|
ASP.Net, Access DB, and a webform?
|
|
Posted By:
|
TheNinthPlayer
|
Post Date:
|
5/13/2008 9:55:14 PM
|
I recently purchased the book and I've read a majority of it, but I have a project that I still feel overwhelmed with and I'm hoping to get some helpful tips from the forums.
I have an Inventory DB of PCs in an Access DB. I want to create a web form that allows people to add/modify PCs listed in that database.
The layout of the form is: Asset Tag: [ ] Serial Num: [ ] User: [ ]
When a visitor enters an Asset Tag in the first field I want the website to check the DB for an existing entry. If it exist it should then postback all the missing fields so they can be modified and submitted. If it doesn't exist the remaining fields will be left empty and a new entry will be entered into the DB upon submission of the form.
So far I have created the form (does nothing, just contains the asp fields) and I have setup a connect to the Access Database using the DB Connector. And I'm not so much worried about submitting the entry as I think I can handle that part.
I am having trouble with the Asset Tag check/retrieval. How do I go about this?
|
|
Reply By:
|
Imar
|
Reply Date:
|
5/14/2008 1:44:40 AM
|
<table class= style1 > br / <tr> br / <td> br / Asset Tag br / </td> br / <td> br / <asp:TextBox ID= txtAsset runat= server AutoPostBack= True br / OnTextChanged= txtAsset_TextChanged ></asp:TextBox> br / </td> br / </tr> br / <tr> br / <td> br / Serial br / </td> br / <td> br / <asp:TextBox ID= txtSerial runat= server ></asp:TextBox> br / </td> br / </tr> br / <tr> br / <td> br / br / </td> br / <td> br / <asp:Button ID= btnSave runat= server OnClick= btnSave_Click Text= Save /> br / </td> br / </tr> br / <tr> br / <td colspan= 2 > br / <asp:Label ID= Label1 runat= server Text= Label ></asp:Label> br / </td> br / </tr> br / </table> br / b [Default.aspx.cs] /b br / protected void txtAsset_TextChanged object sender, EventArgs e br / { br / // Replace code below with data access code to find the asset br / if txtAsset.Text.StartsWith a , StringComparison.InvariantCultureIgnoreCase br / { br / txtSerial.Text = Found ; br / ViewState[ ItemId" target="_blank">Hi there, br / br / You can set up the Asset text box to AutoPostBack. This way, when a user enters some text and tabs away, the page posts back. In the TextChanged handler of the text box you can access the asset tag the user entered, go in the database and see if the item exists. If it does, you populate the other controls and store the ID of the asset somewhere in ViewState for example . Then in a Button s click event you retrieve the ID from ViewState. If it s null, you insert a new item using the text controls; otherwise you update an existing item. Below you find a quick example that demonstrates the idea. It used C# and a Code Behind file. It shouldn t be too difficult to convert if you re using VB instead: br / br / b [Default.aspx] /b br / <table class= style1 > br / <tr> br / <td> br / Asset Tag br / </td> br / <td> br / <asp:TextBox ID= txtAsset runat= server AutoPostBack= True br / OnTextChanged= txtAsset_TextChanged ></asp:TextBox> br / </td> br / </tr> br / <tr> br / <td> br / Serial br / </td> br / <td> br / <asp:TextBox ID= txtSerial runat= server ></asp:TextBox> br / </td> br / </tr> br / <tr> br / <td> br / br / </td> br / <td> br / <asp:Button ID= btnSave runat= server OnClick= btnSave_Click Text= Save /> br / </td> br / </tr> br / <tr> br / <td colspan= 2 > br / <asp:Label ID= Label1 runat= server Text= Label ></asp:Label> br / </td> br / </tr> br / </table> br / b [Default.aspx.cs] /b br / protected void txtAsset_TextChanged object sender, EventArgs e br / { br / // Replace code below with data access code to find the asset br / if txtAsset.Text.StartsWith a , StringComparison.InvariantCultureIgnoreCase br / { br / txtSerial.Text = Found ; br / ViewState[ ItemId = Guid.NewGuid(); } else { txtSerial.Text = "NOT Found"; ViewState["ItemIdhttp://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
TheNinthPlayer
|
Reply Date:
|
5/15/2008 12:19:50 PM
|
Imar,
Thanks for your speedy reply. I've been reading your book and feel its well written like many wrox books. I hope to be able to move onto the professional version in the next month or two, once I feel more comfortable working in ASP.Net.
I am having some trouble understand your reply. For the sake of my amatuer skills I'm just gonna focus on the Postback aspect of this problem. I understand the first part of your reply suggesting to use the postback feature and the first chuch of code, but the second chunk has me somewhat confused. I put comments in your code where I am lost with some pesudo code.
[Default.aspx.cs]
protected void txtAsset_TextChanged(object sender, EventArgs e)
{
// Shouldn't this IF be ( txtAsset.Text = "SELECT assettag
// FROM computers" from DB).
//
if (txtAsset.Text.StartsWith("a", StringComparison.InvariantCultureIgnoreCase))
{
// Shouldn't this part be : txtSerial.Text = SELECT serial
// FROM computers WHERE assettag = "xxxxxx" ?
//
txtSerial.Text = "Found";
// Do I need to post it back to the viewstate? In truth I want the form to reply with a lot more fields (15 or so), I only mentioned 3 in the post for simplicity. In your book you mention not to go overboard with the viewstate cause of bandwidth issues.
ViewState["ItemId"] = Guid.NewGuid();
}
else
{
txtSerial.Text = "NOT Found";
ViewState["ItemId"] = null;
}
}
I can use an SQL Server database. So I created a new DB based of my access one and populated it with sample data.
To reiterate:
How can I compare txtAsset.Text to find a matching entry in the database?
|
|
Reply By:
|
Imar
|
Reply Date:
|
5/16/2008 3:57:39 AM
|
OK, I quickly threw an example together that accesses a SQL Server database. You need to replace SqlConnection etc with OleDbConnection if you are using an Access database.[code]protected void txtAsset_TextChanged object sender, EventArgs e br / { br / string connectionString = ConfigurationManager.ConnectionStrings[ Assets.ConnectionString; using (SqlConnection myConnection = new SqlConnection(connectionString)) { string sql = "SELECT AssetTag, SerialNumber FROM Assets WHERE AssetTag = @assetTag"; SqlCommand myCommand = new SqlCommand(sql, myConnection); myCommand.Parameters.AddWithValue("@assetTag", txtAsset.Text); myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.Read()) { txtSerial.Text = myReader.GetString(1); ViewState["AssetTaghttp://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
TheNinthPlayer
|
Reply Date:
|
5/20/2008 1:59:08 PM
|
I definitly understand this a lot better. I was able to use your example and make a few modifications to fit my needs. Question though, what does this command do?
myCommand.Parameters.AddWithValue("@pcAssetTag", txtPCAsset.Text); My next step is to add a Drop Down List for PC Models. I added it to the SQL query and I am able to display the current Model based on a specific AssetTag in the txtPCDescription TextBox.
string sql = "SELECT pcAssets.pcAssetTag, pcAssets.pcSerialNumber, pcModels.pcModelName FROM pcAssets INNER JOIN pcModels ON pcAssets.pcModelID = pcModels.pcModelID WHERE pcAssetTag = @pcAssetTag";
I went ahead and created a DropDownList which sucessfuly querys the Dataase for a list of Models. Now I just need to set the default value in the drop down list equal to what Model is returend in the AssetTag query.
Here is what I have so far:
<tr>
<td>PC Description</td>
<td>
<asp:TextBox ID="txtPCDescription" runat="server"></asp:TextBox>
<asp:DropDownList ID="ddlPCDescription" runat="server"
DataSourceID="SqlDataSource1" DataTextField="pcModelName"
DataValueField="pcModelName"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:PC Assets DatabaseConnectionString1 %>"
SelectCommand="SELECT [pcModelName] FROM [pcModels]">
</asp:SqlDataSource>
</td>
</tr>
How can I set the default value from the dropdown list based on its currently listed model name in the database?
|
|
Reply By:
|
Imar
|
Reply Date:
|
5/21/2008 12:51:13 PM
|
Take a look at the SQL statement. It contains keywords prefixed by an @ symbol. These placeholders get their value from a Parameter, which is what this code creates:
myCommand.Parameters.AddWithValue("@pcAssetTag", txtPCAsset.Text);
Basically it says: take @pcAssetTag in the SQL statement and replace it with the value of txtPCAsset.Text.
You can preselect an item by its value with this code:
if (ddlPCDescription.Items.FindByValue("SomeValue") != null) { ddlPCDescription.Items.FindByValue("SomeValue").Selected = true; }
If you don't have the value but only the text of an Item you can also use FindByText.
Can you please add enters to your code to split up these really long line? Makes it easier to read these posts and write replies.
Cheers,
Imar
--------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
TheNinthPlayer
|
Reply Date:
|
5/21/2008 3:27:43 PM
|
Thanks for the explination and sorry about the long lines.
I combined your solution with something I came across while googleing.
ddlPCModel.SelectedIndex = ddlPCModel.Items.IndexOf
(ddlPCModel.Items.FindByText(myReader.GetString(2)));
This soltuion seems to be working for me. Thanks for the help!
|
|
Reply By:
|
TheNinthPlayer
|
Reply Date:
|
6/3/2008 8:53:59 AM
|
I've made all kinds of progress with the webapp I'be been desining but I've ran into another problem. I am attempting to add a validator the the txtAsset text box but the post-back keeps removing the validation error. I want to use a regular expression to make sure the asset tag is X numbers long. I have already created and tested the regular expression. I just need the error to stay present after the postback.
|
|
Reply By:
|
planoie
|
Reply Date:
|
6/3/2008 10:28:14 AM
|
If this is a question outside of the scope of the book, could you please post it in one of the non-book forums for ASP.NET (http://p2p.wrox.com/default.asp?CAT_ID=11). We'd be happy to answer it there, plus it will be seen by more people.
|