Can I get the DataTable properties from an arbitrary DataRow?
-
I have a method which takes a DataRow as an out argument. The method fills out the DataRow with data, and returns it. So,
public CreateTheFancyTable()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("First_Column", System.Int32);
DataColumn dc2 = new DataColumn("Second_Column", System.String);
dc1.AllowDBNull = false;
dc2.AlllowDBNull = false;
DataRow dr = dt.NewRow();bool fSucceeded = FillOutMyRow(dr);
}public bool FillOutMyRow(DataRow dr)
{
// fetch some data from an outside source here// fill out the row here dr\["First\_Column"\] = 1; dr\["Second\_Column"\] = "";
}
Please don't focus on the bugs in my quick sample here, the real code is a lot more complex than this. Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false. How, in the code of FillOutMyRow(), can I find out the properties of the columns in the row that was passed to me? I only know how to get the column properties of a table, and I don't have the table. Can I refer directly to the column properties of a row (and if so, how?)? Or do I need to get the parent container of the row (the table), and if so, how? Thanks, Joe
-
I have a method which takes a DataRow as an out argument. The method fills out the DataRow with data, and returns it. So,
public CreateTheFancyTable()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("First_Column", System.Int32);
DataColumn dc2 = new DataColumn("Second_Column", System.String);
dc1.AllowDBNull = false;
dc2.AlllowDBNull = false;
DataRow dr = dt.NewRow();bool fSucceeded = FillOutMyRow(dr);
}public bool FillOutMyRow(DataRow dr)
{
// fetch some data from an outside source here// fill out the row here dr\["First\_Column"\] = 1; dr\["Second\_Column"\] = "";
}
Please don't focus on the bugs in my quick sample here, the real code is a lot more complex than this. Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false. How, in the code of FillOutMyRow(), can I find out the properties of the columns in the row that was passed to me? I only know how to get the column properties of a table, and I don't have the table. Can I refer directly to the column properties of a row (and if so, how?)? Or do I need to get the parent container of the row (the table), and if so, how? Thanks, Joe
-
I have a method which takes a DataRow as an out argument. The method fills out the DataRow with data, and returns it. So,
public CreateTheFancyTable()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("First_Column", System.Int32);
DataColumn dc2 = new DataColumn("Second_Column", System.String);
dc1.AllowDBNull = false;
dc2.AlllowDBNull = false;
DataRow dr = dt.NewRow();bool fSucceeded = FillOutMyRow(dr);
}public bool FillOutMyRow(DataRow dr)
{
// fetch some data from an outside source here// fill out the row here dr\["First\_Column"\] = 1; dr\["Second\_Column"\] = "";
}
Please don't focus on the bugs in my quick sample here, the real code is a lot more complex than this. Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false. How, in the code of FillOutMyRow(), can I find out the properties of the columns in the row that was passed to me? I only know how to get the column properties of a table, and I don't have the table. Can I refer directly to the column properties of a row (and if so, how?)? Or do I need to get the parent container of the row (the table), and if so, how? Thanks, Joe
JoeRip wrote:
Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false.
:confused: No it wont, you're setting the value to an empty string not DbNull.Value. Anyway, you should be able to go back to the table/columns from the datarow. dr.Table.Columns so you could do
if(dr.Table.Columns["Second_Column"].AllowDBNull) { ... }
-
DataRow
has aTable
property.xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
JoeRip wrote:
Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false.
:confused: No it wont, you're setting the value to an empty string not DbNull.Value. Anyway, you should be able to go back to the table/columns from the datarow. dr.Table.Columns so you could do
if(dr.Table.Columns["Second_Column"].AllowDBNull) { ... }
Meanwhile, I'm stuck on the fact that I can't test the passed in DataRow to see if it's null or not.. given
public bool FillOutMyRow(out DataRow dr)
{
if (dr == null)
{
return false;
}
// fill out the data
dr["first_column"] = 1;
return true;
}The compiler is complaining: "use of unassigned parameter dr" What's the problem here?
-
Meanwhile, I'm stuck on the fact that I can't test the passed in DataRow to see if it's null or not.. given
public bool FillOutMyRow(out DataRow dr)
{
if (dr == null)
{
return false;
}
// fill out the data
dr["first_column"] = 1;
return true;
}The compiler is complaining: "use of unassigned parameter dr" What's the problem here?
-
Use
ref
, notout
.xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
Meanwhile, I'm stuck on the fact that I can't test the passed in DataRow to see if it's null or not.. given
public bool FillOutMyRow(out DataRow dr)
{
if (dr == null)
{
return false;
}
// fill out the data
dr["first_column"] = 1;
return true;
}The compiler is complaining: "use of unassigned parameter dr" What's the problem here?
-
That's true, it wasn't. My original post was a quick and dirty bit done from memory, and therefore incomplete. Are you saying that ref or out is not required here at all? I can pass a DataRow and modify it to my heart's content? I did test out "ref" when "out" failed, and that does work. Still not sure why "out" doesn't work in the "if (dr == null) case, but it's not clear to me what "unassigned" means here when the caller has already assigned a value to the variable...
-
That's true, it wasn't. My original post was a quick and dirty bit done from memory, and therefore incomplete. Are you saying that ref or out is not required here at all? I can pass a DataRow and modify it to my heart's content? I did test out "ref" when "out" failed, and that does work. Still not sure why "out" doesn't work in the "if (dr == null) case, but it's not clear to me what "unassigned" means here when the caller has already assigned a value to the variable...
the ref keyword forces the argument to be passed by reference, it has no effect on reference types such as DataRow, but consider the following example with a value type:
static void Main()
{
int i = 1;
NoRef(i);
Console.WriteLine(i); // prints '1'WithRef(ref i);
Console.WriteLine(i); // prints '2'
}static void NoRef(int i)
{
i = i +1;
}static void WithRef(ref int i)
{
i = i + 1;
}as for the out keyword, this means that the method itself has to set the value of the param before using it. Hence why you cant check it for null before initializing it to some value.
static void Main()
{
int i = 0;
SetOut(out i);
Console.WriteLine(i); // prints '1'
}static vouid SetOut(out int i)
{
i = 1;
}So to cut a long story short, just remove the out/ref keyword altogether from your method. You can still change the values of your DataRow within your method.