I've got a table, I've got textboxes, let's make a connection! I'm using ASP.NET and sql server2008; I've got my connection string coded into the webconfig file already, but I'm hung up on how to populate specific textboxes with specific fields from my table. More specifically, the user will submit the data on the home 开发者_如何学运维page, and then be given the chance to edit this data on the edit page. I know, session would be the easiest way to do this, but my trainer specifically forbade it on the grounds that high traffic would cause problems with session objects, and commanded me to use a sql connection. He's the boss. So how do I get the data just inserted from the home page to display in editable textboxes on the edit page?
Take a look at this : Data-Binding
You can use FormView to display the data stored on SQL Server.
You have to define a SqlDataSource like this :
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
</asp:SqlDataSource>
Then you can use FormView like this :
<asp:FormView ID="FormView1"
DataSourceID="SqlDataSource1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right"><b>Product ID:</b></td>
<td><%# Eval("ProductID") %></td>
</tr>
<tr>
<td align="right"><b>Product Name:</b></td>
<td><%# Eval("ProductName") %></td>
</tr>
<tr>
<td align="right"><b>Category ID:</b></td>
<td><%# Eval("CategoryID") %></td>
</tr>
<tr>
<td align="right"><b>Quantity Per Unit:</b></td>
<td><%# Eval("QuantityPerUnit") %></td>
</tr>
<tr>
<td align="right"><b>Unit Price:</b></td>
<td><%# Eval("UnitPrice") %></td>
</tr>
</table>
Hope this help....
You can do a SELECT command and then put whatever it is your data to the TextBox using the following code:
YourTextBox.Text = yourDBValue;
Assuming that there is some kind of login system (ie you can at least identify your user) then the task is fairly straightforward.
When the user enters their data on the home page / data entry page, you validate the data server side, and assuming all is well, store this data in the database for the current user. So, perhaps they are entering their address (as an example). You might have something like this:
// NB: this is NOT code, just shorthand for a hypothetical database schema
Table: User
Columns: User_ID, Email, Name
Table: Address
Columns: User_ID (foreign key into User), Line1, Line2, ... PostCode, Country ... (you get the picture)
So, after they've entered the data, you store the details into this table, setting the User_ID to be the ID of the current user.
Then, on the edit page, you can just look up the existing address details for the current user and set the value of the text boxes on your page to be the various column values.
Better still, you can make the edit page the initial entry page as well, where if it doesn't find existing data it leaves the text boxes empty, thus saving you writing 2 separate pages.
If you don't have this kind of schema, where you have user accounts or whatever, but the data is user specific, you can still use a scheme like this, but instead of keying the Address
table on a User_ID, you could use a value like a GUID which you assign to the user and store in their session. This would allow you to track a user for the duration of thier session at least. To extend this you could write this value to a cookie, remembering to set the expiration date, which would allow you to track them for several sessions, until the cookie expires, or they change browser etc.
Table: Address
Columns: Session_Guid, Line1, Line2, ... PostCode, Country ...
This is the basis of most websites with a login, they use a unique value in a cookie to track a users.
You're asking how to take a textbox, enter some data, and then submit that to a database?
So Insert statements? I would use LINQ, but I'm not sure you are using it. Or prepare a Stored Procedure which handles the insert. However, in the most simple form:
try {
using ( SqlConnection conn = new SqlConnection(cstr)) {
SqlParameter name = cmd.Parameters.Add( "@name", SqlDbType.NVarChar, 15 );
name.Value = TextBox1.Text;
string insertString = @"insert into Table(columnName) values (@name)";
SqlCommand cmd = new SqlCommand(insertString, conn);
cmd.ExecuteNonQuery();
}
} catch (Exception ex) {
//do some logging
} finally {
conn.Close();
}
精彩评论