Wednesday, March 21, 2012

Populate textbox with data source.

Hi all, this is my first post. I'm new-ish to .NET and have some basics to cover. I have an older VB/ASP/ActiveX scripting background but I'm tyring to bust into .NET so here's my first question.

I have an existing ASP app I'm trying to mimic as exercise. I have methodology to 'modify a user'. Quite Simple.

Pull 4-5 fields, populate text boxes with what's form the DB, let it be modified and then saved/updated.

background: I've done it with a data grid. Not what I'm after. The menu of the app lets you choose a User from a drop down. On change, querystring the dropdown's value to the 'modifyuser.aspx' page.

ModifyUser.aspx has FirstName, LastName, Password. PermissionLevel, fields.

Permission Level is a drop down, but names/password are textboxes.

I have a SQLDatasource and an ObjectDataSource on the page, (not sure the difference, so I'm trying either)
The datasources are calling GetUser() Stored Procedure which takes one param, UserID (int) from the QueryString.

That is all set... the thing I'm asking is,..

1..how do I set the values of the name/password textboxes to the values from the data sources when the page loads?

2...How do I differentiate when the page is/has updated and inform on successful update, (pause 2 secs) then return to menu?

To view the actual old ASP app, http://www.beaudamore.com/ProductionManagement is the link. I've done this all manually in the old app with Javascript but I'd like to stay as VisualStudio Drag/Drop as possible and let VS do the JS writing for every different browser, etc. Moving to using as little custom JS as possible.

(If there is a better way of laying out the flow/functionality of managing users, etc, I'm open to that as well. This app is a few years old and heavily custom so, it was what the client wanted at the time.)

Thanks a bunch and I'll be posting away like crazy.

SqlDataSource controls encapsulate the ADO.NET code that you would itherwise have to write to open connections, execute command and clean up afterwards, saving you from having to do this yourself. ObjectDataSources are different. They allow you to specify methods that you write in custom classes that perform this functionality and bind these to controls.

As far as your form is concerned, you should use a FormView or DetailsView to display the data, and configure the sqldatasource with hthe names of your procs for update, select etc.

More on how to use them here:http://quickstarts.asp.net/QuickStartv20/aspnet/doc/data/templates.aspx#formview


ok, thanks for the info. Helpful.

One issue:In formview/details view, how to I enforce requirements per column for input?

On a form with textboxes, I use data validators... how do I use a data validator with a details view or form view?

See what I mean? I need to maintain the exact look/feel of the primary 'initian input' phase. 'Modify' phases should look exactly like the input. I do not use a detail view for inputs, I'm using textboxes and data validators... I just want to use the same thing, but populate the textboxes on load using a querystring'd 'UserID' to the 'Modify User' page. Form/Details views, don't do it.

thanks again.


UPDATE:

I did this--

Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim strSQLFirstName As String

strSQLFirstName = "SELECT FirstName FROM Users " _
& "WHERE uniqueid='" & Request.QueryString("UserID") & "'"

myConnection = New OleDbConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))

myCommand = New OleDbCommand(strSQLFirstName, myConnection)

myConnection.Open()
FirstNameTextbox.Text = myCommand.ExecuteScalar()
myConnection.Close()

BUT..... Do I have to write a seperate SQL string for EACH text box? Forgive me, I'm used to old ASP, I get a recordset from "select * from Users", and can access each field via rs.(FieldName")

I don't want to have to do a .ExecuteScalar() for each textbox.... unless there's no other way?... Is there a way to get a recordset in .NET and access each Field, and plop them into the textbox.text properties?... please say yes.

Otherwise, I have to write a seelct per field and executeScalar to each .text property like I've done above.

? Thanks again


no its much more easy in asp.net you can use the DataReader for this add the following after myConnection.Open(), I am not a VB guy but its easy to covert it to vb.net i think you should remove the braces and semi colons in VB.NET

OleDbDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

while (dr.Read())

{

FirstNameTextbox.Text =Convert.ToString(dr["FirstName"]);

}


As SharpGuy said, the closest thing to an old ASP recordset is the DataReader, which provides forward only access to a bunch of records. So the equivalent of the following classic ASP:

Do Until rs.EOF
'process various records
'such as placing them in variables
rs.MoveNext
Loop

is:

While MyDataReader.Read()
TextBox1.Text = MyDataReader("FirstName").ToString()
TextBox2.Text = MyDataReader("LastName").ToString()
TextBox3.Text = MyDataReader("Telephone").ToString()
End While

Alternatively, you would set your SqlDataSource up with a SelectCommand, as well as the InsertCommand, using the querystring value as a Parameter. Then your FormView would contain a ItemTemplate to show the record details that were returned by the SelectCommand.

You are using OleDb, so I assume Access? If so, you will probably find this helpful:http://msconline.maconstate.edu/tutorials/ASPNET20/default.htm


Yes, that was it, thanks both of you. Very much. Here's where I am now.

Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim strSQLFirstName As String
Dim myDataReader As OleDbDataReader

strSQLFirstName = "SELECT * FROM Users " _
& "WHERE uniqueid='" & Request.QueryString("UserID") & "'"

myConnection = New OleDbConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))

myCommand = New OleDbCommand(strSQLFirstName, myConnection)

myConnection.Open()
myDataReader = myCommand.ExecuteReader()
Do While (myDataReader.Read())
TextboxFirstName.Text = Convert.ToString(myDataReader("FirstName"))
TextboxLastName.Text = Convert.ToString(myDataReader("LastName"))
TextboxAddress1.Text = Convert.ToString(myDataReader("Address1"))
TextboxAddress2.Text = Convert.ToString(myDataReader("Address2"))
TextboxCity.Text = Convert.ToString(myDataReader("City"))
DropDownState.SelectedValue = Convert.ToString(myDataReader("State"))
TextBoxZipCode.Text = Convert.ToString(myDataReader("Zip"))
TextBoxPhone.Text = Convert.ToString(myDataReader("Phone"))
TextBoxEmail.Text = Convert.ToString(myDataReader("emailaddress"))
TextboxUsername.Text = Convert.ToString(myDataReader("Username"))
TextboxPassword.Text = Convert.ToString(myDataReader("Password"))
DropDownListPermissionLevel.SelectedValue = myDataReader("PermissionLevel")
CheckBoxDeleted.Checked = Convert.ToBoolean(myDataReader("Deleted"))
Loop
myDataReader.Close()
myConnection.Close()

ok...NOW....lol... how should I handle it, for posting the new data? something about 'ispostback' conditional? (I am ASP/VB fluent, just new to .NET)

How do I catch it that the page's been resubmitted after populated? Already have the StoredProcedure written to take proper params. Because now it's filling in all my text boxes and allowing my DataValidators to work their expression wonders :)

Very happy, moving right along.

(should I make a seperate post now for the submit/posting questions and mark the 'answer' for this post?
I wanna conform to forum standard practice here, ya know?)

--

PS I'm using SQL 2005

--


If there's nothing else on the page that could cause a postback, then yes - check for Page.isPostBack in the load event.

If there are other things on the page that could cause a postback, then you might want to use the onClick method of an <asp:button .../>. If you want to pass arguments aswell, then still using an asp:button, use the commandArgument and onCommand method. Example below:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx


You can create a conditional in your Page_Load event.

If Page.IsPostBack Then
'validate and perform the insert
'set the FromView.Visible = False
'Display a "Success" message
End If

As far as supplementary questions go in the future, that's a grey area. I reckon the one you asked here is acceptable, but any more should go in a new post.


Thanks all.

0 comments:

Post a Comment