Working with a legacy SQL Server table, it shows an optional col named [Group]
, which if omitted from code completely, the insert command functions just fine. The problem is related to that specific col.
What I have:
daCust.InsertCommand = new SqlCommand("insert into dbo.TimeRouteTest ([Group], Route, Step_1) values (@Group, @Route, @Step_1)", con);
daCust.InsertCommand.Parameters.Add(new SqlParameter("[Group]", SqlDbType.Int, 4, "[Group]"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("Route", SqlDbType.NVarChar, 100, "Route"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("Step_1", SqlDbType.NVarChar, 50, "Step_1"));
then
DataRow newRow = ds.Tables[0].NewRow();
newRow["[Group]"] = Convert.ToInt32(myGroup);
newRow["Route"] = mroute;
newRow["Step_1"] = mStep_1;
Note: I pre-process this data to make sure it's clean, I strip single quotes, etc. mroute and mStep_1 are just local variable holding text data passed from another function. myGroup is also a local var that is passed a value from a webservice. Then I follow up with:
ds.Tables[0].Rows.Add(newRow);
daCust.Update(ds);
If I comment out the [group]
line, the command works fine. Any help on why this fails and how I can fix it? I know "group" is a reserved keyword, but adding the brackets is supposed to work to make it usable, or so I thought. The funny thing is, I can query this table, and get the value from the [group]
col from other rows, so I know the col is truly there. Just unsure why I can't find it on insert...
Edit: if I use this code, it works fine:
daCust.InsertCommand = new SqlCommand("insert into dbo.TimeRouteTest (Route, Step_1) values (@Route, @Step_1)", con);
daCust.InsertCommand.Parameters.Add(new SqlParameter("Route", SqlDbType.NVarChar, 100, "Route"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("Step_1", SqlDbType.NVarChar, 50, "Step_1"));
DataRow newRow = ds.Tables[0].NewRow();
newRow["Route"] = mroute;
newRow["Step_1"] = mStep_1;
ds.Tables[0].Rows.Add(newRow);
daCust.Update(ds);
Which pretty much isolates the issue to that specific col named [Group] and no combination of brackets or otherwise that I could think to try will make it work. Doing a select * from TimeRouteTest and then pulling the value from dr[2] does work, which is the index position of that col. Other than scrapping the table and using a better col name, how to fix? Curious note, looking at table definition in Visual Studio, it shows that col name as [Group], but if i look at data in table, it shows just Group as header/label in results...
Edit:
The line:
daCust.InsertCommand.Parameters.Add(new SqlParameter("[Group]", SqlDbType.Int, 4, "[Group]"));
and
daCust.InsertCommand.Parameters.Add(new SqlParameter("@Group", SqlDbType.Int, 4, "[Group]"));
and
daCust.InsertCommand.Parameters.Add(new SqlParameter("@[Group]", SqlDbType.Int, 4, "[Group]"));
all fail with error:
System.ArguementException: Column '[Group]' does not belong to table Table.
Edit: found (syntax) error. After smashing my head against wall for 3 hours, the following code actually works:
daCust.InsertCommand = new SqlCommand("insert into dbo.TimeRouteTest ([Group], Route, Step_1) values (@Group, @Route, @Step_1)", con);
daCust.InsertCommand.Parameters.Add(new SqlParameter("Group", SqlDbType.Int, 4, "Group"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("Route", SqlDbType.NVarChar, 100, "Route"));
daCust.InsertCommand.Parameters.Add(new SqlParameter("Step_1", SqlDbType.NVarChar, 50, "Step_1"));
DataRow newRow = ds.Tables[0].NewRow();
newRow["Group"] = Convert.ToInt32(myGroup);
newRow["Route"] = mroute;
newRow["Step_1"] = mStep_1;
Turns out the only place you need brackets is the insert statement when declaring the col name, adding them anywhere else will cause various errors. My best guess is that Visual Studio causes partial confusion when it encounters a keyword col name by automatically showing brackets on col name so when exporting table definition, the brackets appear to be part of the name when in fact they are not.
The brackets are only required when using certain SQL statements and the col name is a reserved SQL Server keyword. Aggravating to say the least!