G
Guest
I found another issue with this same heading, but mine is different so please
read on...
I have the following table:
CREATE TABLE [UsersSF] (
[UserId] [tinyint] NOT NULL ,
[SFId] [tinyint] NOT NULL ,
[SFTypeId] [tinyint] NOT NULL ,
[Description] [varchar] (30) NOT NULL ,
[Sort] [text] NOT NULL ,
[Filter] [text] NOT NULL ,
[Default] [bit] NOT NULL ,
CONSTRAINT [PK_UsersSF] PRIMARY KEY NONCLUSTERED
(
[UserId],
[SFId],
[SFTypeId]
) WITH FILLFACTOR = 90 ON [PRIMARY] ,
CONSTRAINT [FK_UsersSF(SFTypeId)_SFTypes(SFTypeId)] FOREIGN KEY
(
[SFTypeId]
) REFERENCES [SFTypes] (
[SFTypeId]
)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
I am trying to update the table using the following code:
SqlCommandBuilder pscbBuilder;
SqlDataAdapter psdaDefault;
DataTable pdttDefault;
DataRow pdtrDefault;
pstrSql = "SELECT UserId, SFId, SFTypeId, [Description], Sort, Filter,
[Default] " +
"FROM dbo.UsersSF " +
"WHERE UserId = " + UserId + " " +
"AND SFTypeId = " + (short)SortFilter.SFTypes.SFIncidents + " " +
"AND [Default] = 1";
psdaDefault = new SqlDataAdapter( pstrSql, Connection );
pscbBuilder = new SqlCommandBuilder( psdaDefault );
psdaDefault.Fill( pdtsLookups, "UserDefault" );
pdttDefault = pdtsLookups.Tables["UserDefault"];
pdtrDefault = pdttDefault.Rows[0];
pdtrDefault["Sort"] = "Some text";
pdtrDefault["Filter"] = "Some text";
psdaDefault.UpdateCommand = pscbBuilder.GetUpdateCommand();
psdaDefault.Update( pdttDefault ); <---Fails here.
pdttDefault.AcceptChanges();
When I execute the statement, I get the following error:
SqlException: Incorrect syntax near the keyword 'Default'.]
System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping) +1662
System.Data.Common.DbDataAdapter.Update(DataTable dataTable) +139
EDS.WarrantyServices.ResourceConnection.IncidentsSF.Page_Load(Object
objSender, EventArgs evaEvtArgs) in
c:\inetpub\wwwroot\resconn\incidentssf.aspx.cs:143
System.Web.UI.Control.OnLoad(EventArgs e) +67
EDS.WarrantyServices.BasePage.OnLoad(EventArgs etaEventArgs)
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +739
Now my guess is that it can't handle having a column name called 'Default'
because if I remove the 'Default' column from the select everything works. I
have tried the following to get the update to work:
- Giving 'Default' an alias
- Modifying the CommandText after it is generated by the SqlCommandBuilder
to put brackets [] around all Default column references.
Anyone have any ideas on how I can get around this other than changing the
name of the column?
Thanks in advance.
read on...
I have the following table:
CREATE TABLE [UsersSF] (
[UserId] [tinyint] NOT NULL ,
[SFId] [tinyint] NOT NULL ,
[SFTypeId] [tinyint] NOT NULL ,
[Description] [varchar] (30) NOT NULL ,
[Sort] [text] NOT NULL ,
[Filter] [text] NOT NULL ,
[Default] [bit] NOT NULL ,
CONSTRAINT [PK_UsersSF] PRIMARY KEY NONCLUSTERED
(
[UserId],
[SFId],
[SFTypeId]
) WITH FILLFACTOR = 90 ON [PRIMARY] ,
CONSTRAINT [FK_UsersSF(SFTypeId)_SFTypes(SFTypeId)] FOREIGN KEY
(
[SFTypeId]
) REFERENCES [SFTypes] (
[SFTypeId]
)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
I am trying to update the table using the following code:
SqlCommandBuilder pscbBuilder;
SqlDataAdapter psdaDefault;
DataTable pdttDefault;
DataRow pdtrDefault;
pstrSql = "SELECT UserId, SFId, SFTypeId, [Description], Sort, Filter,
[Default] " +
"FROM dbo.UsersSF " +
"WHERE UserId = " + UserId + " " +
"AND SFTypeId = " + (short)SortFilter.SFTypes.SFIncidents + " " +
"AND [Default] = 1";
psdaDefault = new SqlDataAdapter( pstrSql, Connection );
pscbBuilder = new SqlCommandBuilder( psdaDefault );
psdaDefault.Fill( pdtsLookups, "UserDefault" );
pdttDefault = pdtsLookups.Tables["UserDefault"];
pdtrDefault = pdttDefault.Rows[0];
pdtrDefault["Sort"] = "Some text";
pdtrDefault["Filter"] = "Some text";
psdaDefault.UpdateCommand = pscbBuilder.GetUpdateCommand();
psdaDefault.Update( pdttDefault ); <---Fails here.
pdttDefault.AcceptChanges();
When I execute the statement, I get the following error:
SqlException: Incorrect syntax near the keyword 'Default'.]
System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping) +1662
System.Data.Common.DbDataAdapter.Update(DataTable dataTable) +139
EDS.WarrantyServices.ResourceConnection.IncidentsSF.Page_Load(Object
objSender, EventArgs evaEvtArgs) in
c:\inetpub\wwwroot\resconn\incidentssf.aspx.cs:143
System.Web.UI.Control.OnLoad(EventArgs e) +67
EDS.WarrantyServices.BasePage.OnLoad(EventArgs etaEventArgs)
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +739
Now my guess is that it can't handle having a column name called 'Default'
because if I remove the 'Default' column from the select everything works. I
have tried the following to get the update to work:
- Giving 'Default' an alias
- Modifying the CommandText after it is generated by the SqlCommandBuilder
to put brackets [] around all Default column references.
Anyone have any ideas on how I can get around this other than changing the
name of the column?
Thanks in advance.