liquidspaces Posted January 6, 2003 Posted January 6, 2003 I have a form that has maybe 50 fields. The user can search for specific data, though not all of the data is required in the database. Because of that, there will be some null values in some of the fields. If I don't check for DBNULL, the program crashes when I try to place the value of a null field in a textbox. I can go ahead and check for DBNULL for every field in the database, but that takes so much time in "if...else" statements. Is there a way where I can write generic code to apply to all the fields? Something like: If any field isdbnull then appropriate textbox = "" else textbox = value ??? Quote
*Experts* Nerseus Posted January 6, 2003 *Experts* Posted January 6, 2003 You can put it in a function or using DataBinding. But otherwise, there's no "magic" to a particular field and a control. For a function, use something like: void PutVal(object a, TextBox t) { if(a==System.DBNull.Value) t.Text = string.empty; else t.Text = a.ToString(); } // call it with: PutVal(dataSet.Tables["Table1"].Rows[0]["Column1"], textBox1); PutVal(dataSet.Tables["Table1"].Rows[0]["Column2"], textBox2); PutVal(dataSet.Tables["Table1"].Rows[0]["Column3"], textBox3); PutVal(dataSet.Tables["Table1"].Rows[0]["Column4"], textBox4); // The following is the same as above, but more readable DataRow row = dataSet.Tables["Table1"].Rows[0]; PutVal(row["Column1"], textBox1); PutVal(row["Column2"], textBox2); PutVal(row["Column3"], textBox3); PutVal(row["Column4"], textBox4); -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.