|
-
Jul 3rd, 2008, 09:45 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] custom form databinding
Hey I have quite a fiew fields for a specific form I'm writing. So instead of using a gridview, or detailsview, or formview, I just slapped a bunch of aps:controls on the page and setup my DropDownLists to get their values from the appropriate database tables. Now I don't see a <FORM> tag in design time, but when I run it and view source it shows up so I'm assuming its working fine and has something to do with my masterpage, but whats the easiest way to bind the data to the controls without having to do it manually at Page_Load on each control?? Is it as easy as when you use a GridView or FormView? Can I do the Bind and Eval methods in my asp code or will that not work?
NOTE I do have strongly typed tableadapters with methods for getting my data, I'm just not sure how to get it into these controls without using a GridView, formview, etc...
Thanks!

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Jul 3rd, 2008, 11:31 PM
#2
Thread Starter
Fanatic Member
Re: [2005] custom form databinding
I think what I'm going to end up doing is just fill my dataset with the method to get all my data, then manually fill the controls on Page_Load(). Unless anyone can think of something more efficient 
Code:
protected void contactFill()
{
int cid = Convert.ToInt32(HttpContext.Current.Request.QueryString["contactid"]);
ds = new DataSet();
adapterContacts = new ContactsTableAdapter();
ds.Tables.Add(adapterContacts.GetContactByContactID(cid));
txtCompany.Text = (string)ds.Tables[0].Rows[0]["CompanyName"];
}

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Jul 4th, 2008, 01:52 AM
#3
Re: [2005] custom form databinding
If you're trying to avoid coding the binding of the data, do a search on SqlDataSource. It is a control that can be placed on the page, SP specified, controls pointed to it, and so on
-
Jul 4th, 2008, 09:49 AM
#4
Thread Starter
Fanatic Member
Re: [2005] custom form databinding
I considered that but then I feel like my DAL methods would be useless because I can't use my Select/Update/Insert methods with the SQLDataSource 
Here's what I ended up doing:
Code:
int cid = Convert.ToInt32(HttpContext.Current.Request.QueryString["contactid"]);
ds = new DataSet();
adapterContacts = new ContactsTableAdapter();
ds.Tables.Add(adapterContacts.GetContactByContactID(cid));
if (ds.Tables[0].Rows[0]["CompanyName"] != DBNull.Value)
{
txtCompany.Text = (string)ds.Tables[0].Rows[0]["CompanyName"];
}
if (ds.Tables[0].Rows[0]["ContactTypeID"] != DBNull.Value)
{
ddlContactType.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["ContactTypeID"]);
}
Is there a better way to handle the null values though?

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Jul 4th, 2008, 11:46 AM
#5
Re: [2005] custom form databinding
So if you want to use a DAL, you'll have to code in the Page Load event. I don't see what the issue is here.
You do have another option though; you can explore creating your own ObjectDataSource and try binding to that.
-
Jul 4th, 2008, 11:47 AM
#6
Re: [2005] custom form databinding
Try
!(string.IsNullOrEmpty(ds.Tables[0].Rows[0]["ContactTypeID"]))
-
Jul 4th, 2008, 12:09 PM
#7
Thread Starter
Fanatic Member
Re: [2005] custom form databinding
Ok thanks for clearing up the DAL issue. I wasn't sure if there was a way to bind a DAL method to a SqlDataSource. At this point I'm very proud of my DAL methods and want to use them I spent alot of time on them.
As for the null, do I replace
if (ds.Tables[0].Rows[0]["CompanyName"] != DBNull.Value)
with
!(string.IsNullOrEmpty(ds.Tables[0].Rows[0]["ContactTypeID"])) ? Or are you telling me to use a Try statement? Just curious how that code is more efficient.
Once again, thanks for everything!

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Jul 4th, 2008, 01:03 PM
#8
Re: [2005] custom form databinding
Haha... no no, I meant "try this". Then wanted to show the string.IsNullOrEmpty().
But I've just realized that you're meant to check for DBNull.Value because IsNullOrEmpty expects a string and you can't .ToString if you don't know if it's DbNull or not first anyways, so ignore all this. 
Important thing is that you get to use your methods and whichever way you choose - DAL or ObjectDataSource, both are just as efficient.
-
Jul 4th, 2008, 02:13 PM
#9
Thread Starter
Fanatic Member
Re: [2005] custom form databinding
Ahh I see what you mean. I just noticed something to. Here's how I'm checking for null:
Code:
if (ds.Tables[0].Rows[0]["AddressLine1"] != DBNull.Value)
{
txtAddressLine1.Text = (string)ds.Tables[0].Rows[0]["AddressLine1"];
}
Now I just did some reading and it seems if I use the Convert.ToString() method then I don't have to check for null because if its null it will return string.Empty instead. Does string.Empty mean null? If I have a null value in my database, and it is updated with a control that is assigned string.Empty, will it remain null?
Now I'm thinking I should use this instead of the above code:
Code:
txtAddressLine2.Text = Convert.ToString(ds.Tables[0].Rows[0]["AddressLine2"]);
Am I right in thinking that I should use Convert.ToString or should I stick with my cast (or ToString)?
Last edited by drpcken; Jul 4th, 2008 at 02:25 PM.

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Jul 4th, 2008, 03:15 PM
#10
Re: [2005] custom form databinding
This depends on how important this bit of data integrity is to you. In this specific case, you are reading a string value out of a database. It can be null, empty string or something. For your intents and purposes, the empty string is the same thing as the null. So if you have a textual field which you don't want to display or do something different if the value coming back is null or empty, then perform the string.isnullorempty check before deciding what to assign to the label's text property.
If this were a numeric field, my answer would be different. I'd assume that the value coming back is important and it does make a difference whether the value coming back is null or 0. I'd therefore attempt to preserve the null by using nullable types.
-
Jul 4th, 2008, 03:21 PM
#11
Thread Starter
Fanatic Member
Re: [2005] custom form databinding
These are textboxes in a form (I think you think they are labels?). If the value is null of course I want the textbox to be empty and if the user chooses they can put something in the textbox and update the record.
Ok with that knowledge I think I'll use the Convert.ToString() method on my string fields. With my number fields I'll check for nulls first and decide what to do from there.
Thank you!!

In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional 
-
Jul 5th, 2008, 02:26 AM
#12
Re: [2005] custom form databinding
No prob, keep posting if you have questions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|