I've got a GridView. My SelectCommand in the code works if I copy and paste it into SQL Server Management Studio. It executes fine.
If I run the page and click Edit for any row I get the error:
"Incorrect syntax near the keyword 'IS'."
I've tried to see what's going on using SQL Server Profiler. Here's what the SelectCommand looks like and what SQL is receiving...
SelectCommand from supplies.ascx
SELECT some stuff FROM here WHERE (this IS NULL) and (that=1) ORDER BY this DESC
SQL Server Profiler reports
SELECT some stuff FROM here (this IS NULL) and (that=1) ORDER BY this DESC
I didn't put any code here because I'm not really sure what would be relevant and didn't want to load up the post with unnecessary lines of code. Please let me know what you'd like to see to help.
Requested code below. I didn't put everything from the GridView in...It's pretty big. If needed though I can update.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="fldSupReqID"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." AllowSorting="True"
CellPadding="4" ForeColor="#333333" Font-Size="10px">
<RowStyle BackColor="#EFF3FB" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
DeleteCommand="DELETE FROM [tblSupplyRequests] WHERE [fldSupReqID] = #fldSupReqID"
InsertCommand="INSERT INTO [tblSupplyRequests] ([fldManufacturerID], [fldItemID], [fldFeeTypeID], [fldRequestDate], [fldStamp], [fldAddedByUser], [fldBID]) VALUES (#fldManufacturerID, #fldItemID, #fldFeeTypeID, #fldRequestDate, #fldStamp, #fldAddedByUser, #fldBID)"
ProviderName="<%$ ConnectionStrings:myConnectionString.ProviderName %>" SelectCommand="SELECT tblSupplyRequests.fldSupReqID, tblSupplyRequests.fldOrderToShipDate, tblSupplyRequests.fldBID AS BID, tblMerchantIDs.fldMerchantID AS MerchantID, salescontactinfo.sname_of_business AS [Merchant Name], salescontactinfo.scontactname1 AS Contact, salescontactinfo.scontactn1phone1 AS Phone, tblSupplyRequests.fldRequestDate, tblSupplyRequests.fldAddedByUser AS RequestedBy, INVtblManufacturers.fldManufacturerName AS [Term Manuf], INVtblInventoryItems_1.fldItemName AS [Term Model], INVtblInventoryTypes.fldInventoryTypename AS [Item Type], tblSupplyRequests.fldNumItemsRequested AS [Num Items], tblSupplyRequests.fldPaperItemID, tblSupplyRequests.fldPerItemCost, INVtblInventoryItems.fldMiscDesc, tblSupplyRequests.fldPersonCalling, tblSupplyRequests.fldTitlePersonCalling, tblSupplyRequests.fldNotes, salescontactinfo.sstate, tblSupplyRequests.fldManufacturerID, tblSupplyRequests.fldItemID, tblSupplyRequests.fldStamp, tblSupplyRequests.fldOrderToShipDate AS Expr1, tblSupplyRequests.fldOrderToShipBy FROM INVtblInventoryItems INNER JOIN tblSupplyRequests INNER JOIN salescontactinfo ON tblSupplyRequests.fldBID = salescontactinfo.sbusinessid LEFT OUTER JOIN tblMerchantIDs ON salescontactinfo.sbusinessid = tblMerchantIDs.fldBID LEFT OUTER JOIN INVtblManufacturers ON tblSupplyRequests.fldManufacturerID = INVtblManufacturers.fldManufacturerID ON INVtblInventoryItems.fldItemID = tblSupplyRequests.fldPaperItemID LEFT OUTER JOIN INVtblInventoryTypes ON tblSupplyRequests.fldInventoryTypeID = INVtblInventoryTypes.fldInventoryTypeID LEFT OUTER JOIN INVtblInventoryItems AS INVtblInventoryItems_1 ON tblSupplyRequests.fldItemID = INVtblInventoryItems_1.fldItemID WHERE (tblSupplyRequests.fldOrderToShipDate IS NULL) AND (tblMerchantIDs.fldMIDStatusID=1) ORDER BY tblSupplyRequests.fldRequestDate DESC"
UpdateCommand="UPDATE tblSupplyRequests SET fldOrderToShipDate=#fldOrderToShipDate, fldNotes = #fldNotes, fldTitlePersonCalling = #fldTitlePersonCalling, fldPersonCalling = #fldPersonCalling, fldRequestDate = #fldRequestDate WHERE (fldSupReqID = #fldSupReqID)">
<DeleteParameters>
<asp:Parameter Name="fldSupReqID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="fldManufacturerID" Type="Int32" />
<asp:Parameter Name="fldItemID" Type="Int32" />
<asp:Parameter Name="fldFeeTypeID" Type="Int32" />
<asp:Parameter Name="fldRequestDate" Type="DateTime" />
<asp:Parameter Name="fldStamp" Type="DateTime" />
<asp:Parameter Name="fldAddedByUser" Type="String" />
<asp:Parameter Name="fldBID" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="fldOrderToShipDate" Type="DateTime" />
<asp:Parameter Name="fldNotes" Type="String" />
<asp:Parameter Name="fldTitlePersonCalling" Type="String" />
<asp:Parameter Name="fldPersonCalling" Type="String" />
<asp:Parameter Name="fldRequestDate" Type="DateTime" />
<asp:Parameter Name="fldSupReqID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
And here's what SQL Server Profiler catches...
SELECT tblSupplyRequests.fldSupReqID, tblSupplyRequests.fldOrderToShipDate, tblSupplyRequests.fldBID AS BID, tblMerchantIDs.fldMerchantID AS MerchantID, salescontactinfo.sname_of_business AS [Merchant Name], salescontactinfo.scontactname1 AS Contact, salescontactinfo.scontactn1phone1 AS Phone, tblSupplyRequests.fldRequestDate, tblSupplyRequests.fldAddedByUser AS RequestedBy, INVtblManufacturers.fldManufacturerName AS [Term Manuf], INVtblInventoryItems_1.fldItemName AS [Term Model], INVtblInventoryTypes.fldInventoryTypename AS [Item Type], tblSupplyRequests.fldNumItemsRequested AS [Num Items], tblSupplyRequests.fldPaperItemID, tblSupplyRequests.fldPerItemCost, INVtblInventoryItems.fldMiscDesc, tblSupplyRequests.fldPersonCalling, tblSupplyRequests.fldTitlePersonCalling, tblSupplyRequests.fldNotes, salescontactinfo.sstate, tblSupplyRequests.fldManufacturerID, tblSupplyRequests.fldItemID, tblSupplyRequests.fldStamp, tblSupplyRequests.fldOrderToShipDate AS Expr1, tblSupplyRequests.fldOrderToShipBy FROM INVtblInventoryItems INNER JOIN tblSupplyRequests INNER JOIN salescontactinfo ON tblSupplyRequests.fldBID = salescontactinfo.sbusinessid LEFT OUTER JOIN tblMerchantIDs ON salescontactinfo.sbusinessid = tblMerchantIDs.fldBID LEFT OUTER JOIN INVtblManufacturers ON tblSupplyRequests.fldManufacturerID = INVtblManufacturers.fldManufacturerID ON INVtblInventoryItems.fldItemID = tblSupplyRequests.fldPaperItemID LEFT OUTER JOIN INVtblInventoryTypes ON tblSupplyRequests.fldInventoryTypeID = INVtblInventoryTypes.fldInventoryTypeID LEFT OUTER JOIN INVtblInventoryItems AS INVtblInventoryItems_1 ON tblSupplyRequests.fldItemID = INVtblInventoryItems_1.fldItemID (tblSupplyRequests.fldOrderToShipDate IS NULL) AND (tblMerchantIDs.fldMIDStatusID=1) ORDER BY tblSupplyRequests.fldRequestDate DESC
I hate to say this, but it could just be that ASP.NET is parsing the query incorrectly because of all the nested INNER JOINs that precede it all on one line. I wonder if formatting your SQL would help at all? There is a very cool tool online for neatly formatting your SQL statements.
Here's the formatted version of your SQLDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
DeleteCommand=" DELETE FROM [tblSupplyRequests]
WHERE [fldSupReqID] = #fldSupReqID"
InsertCommand=" INSERT INTO [tblSupplyRequests] ([fldManufacturerID],[fldItemID],[fldFeeTypeID],[fldRequestDate],[fldStamp],[fldAddedByUser],[fldBID])
VALUES (#fldManufacturerID,#fldItemID,#fldFeeTypeID,#fldRequestDate,#fldStamp,#fldAddedByUser,#fldBID)"
ProviderName="<%$ ConnectionStrings:myConnectionString.ProviderName %>"
SelectCommand=" SELECT tblsupplyrequests.fldsupreqid,
tblsupplyrequests.fldordertoshipdate,
tblsupplyrequests.fldbid AS bid,
tblmerchantids.fldmerchantid AS merchantid,
salescontactinfo.sname_of_business AS [Merchant Name],
salescontactinfo.scontactname1 AS contact,
salescontactinfo.scontactn1phone1 AS phone,
tblsupplyrequests.fldrequestdate,
tblsupplyrequests.fldaddedbyuser AS requestedby,
invtblmanufacturers.fldmanufacturername AS [Term Manuf],
invtblinventoryitems_1.flditemname AS [Term Model],
invtblinventorytypes.fldinventorytypename AS [Item Type],
tblsupplyrequests.fldnumitemsrequested AS [Num Items],
tblsupplyrequests.fldpaperitemid,
tblsupplyrequests.fldperitemcost,
invtblinventoryitems.fldmiscdesc,
tblsupplyrequests.fldpersoncalling,
tblsupplyrequests.fldtitlepersoncalling,
tblsupplyrequests.fldnotes,
salescontactinfo.sstate,
tblsupplyrequests.fldmanufacturerid,
tblsupplyrequests.flditemid,
tblsupplyrequests.fldstamp,
tblsupplyrequests.fldordertoshipdate AS expr1,
tblsupplyrequests.fldordertoshipby
FROM invtblinventoryitems
INNER JOIN tblsupplyrequests
INNER JOIN salescontactinfo
ON tblsupplyrequests.fldbid = salescontactinfo.sbusinessid
LEFT OUTER JOIN tblmerchantids
ON salescontactinfo.sbusinessid = tblmerchantids.fldbid
LEFT OUTER JOIN invtblmanufacturers
ON tblsupplyrequests.fldmanufacturerid = invtblmanufacturers.fldmanufacturerid
ON invtblinventoryitems.flditemid = tblsupplyrequests.fldpaperitemid
LEFT OUTER JOIN invtblinventorytypes
ON tblsupplyrequests.fldinventorytypeid = invtblinventorytypes.fldinventorytypeid
LEFT OUTER JOIN invtblinventoryitems AS invtblinventoryitems_1
ON tblsupplyrequests.flditemid = invtblinventoryitems_1.flditemid
WHERE ( tblsupplyrequests.fldordertoshipdate IS NULL )
AND ( tblmerchantids.fldmidstatusid = 1 )
ORDER BY tblsupplyrequests.fldrequestdate DESC"
UpdateCommand=" UPDATE tblsupplyrequests
SET fldordertoshipdate = #fldOrderToShipDate,
fldnotes = #fldNotes,
fldtitlepersoncalling = #fldTitlePersonCalling,
fldpersoncalling = #fldPersonCalling,
fldrequestdate = #fldRequestDate
WHERE ( fldsupreqid = #fldSupReqID )" >
<DeleteParameters>
<asp:Parameter Name="fldSupReqID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="fldManufacturerID" Type="Int32" />
<asp:Parameter Name="fldItemID" Type="Int32" />
<asp:Parameter Name="fldFeeTypeID" Type="Int32" />
<asp:Parameter Name="fldRequestDate" Type="DateTime" />
<asp:Parameter Name="fldStamp" Type="DateTime" />
<asp:Parameter Name="fldAddedByUser" Type="String" />
<asp:Parameter Name="fldBID" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="fldOrderToShipDate" Type="DateTime" />
<asp:Parameter Name="fldNotes" Type="String" />
<asp:Parameter Name="fldTitlePersonCalling" Type="String" />
<asp:Parameter Name="fldPersonCalling" Type="String" />
<asp:Parameter Name="fldRequestDate" Type="DateTime" />
<asp:Parameter Name="fldSupReqID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Note: if your queries in the SQLDataSource are getting this complex, you may want to consider moving this logic into a class / data access layer for future maintainability. Having said that, I understand that sometimes you are stuck with what you have for the time-being =) Good luck!
Related
I have inherited some ASP.NET code that I need to update which has resulted in my needing to change the ASP SqlDataSource's UpdateCommandType from a string (hard coded SQL Update statement) to a stored procedure containing the Update statement.
The string executes fine and uses parameters that are bound to controls in a details view (I know this is not best practice and it pains me having to work with data connections from the client side...! But I dont have time to re-write all the data connections for this and many pages just yet).
Anyway, I have just changed the UpdateCommand to a stored procedure that does the same thing and I just get the error
Input String was not in a correct format.
when I try to update on the page.
I can supply code if requested, but it is big & horrible so I am tentatively asking if anyone has any initial ideas? I will put a few small bits below though. I have been looking at the UpdateParameterCollection as I wonder if the parameter collection is getting cached anywhere - but cannot see anything.
I have controls bound to the DataSource items like so:
<EditItemTemplate>
<asp:CheckBox ID="chkNonReview" runat="server" Checked='<%# Bind("NonReview") %>' />
</EditItemTemplate>
And the SqlDataSource has been changed from this...
<asp:SqlDataSource ID="dsEventDV" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnString %>"
DeleteCommand="DELETE FROM [I_TRAINEE_EVENTS] WHERE [EVENTID] = #EVENTID"
InsertCommand="INSERT_TRAINEE_EVENTS_ED3"
InsertCommandType="StoredProcedure"
OnInserted="DSEvent_Inserted"
SelectCommand="Get_Candidate_Events_I3"
SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE I_TRAINEE_EVENTS (etc...)"
.....
to this:
<asp:SqlDataSource ID="dsEventDV" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnString %>"
DeleteCommand="DELETE FROM [I_TRAINEE_EVENTS] WHERE [EVENTID] = #EVENTID"
InsertCommand="INSERT_TRAINEE_EVENTS_ED3"
InsertCommandType="StoredProcedure"
OnInserted="DSEvent_Inserted"
SelectCommand="Get_Candidate_Events_I3"
SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE_TRAINEE_EVENTS"
UpdateCommandType="StoredProcedure">
.....
With the new UpdateCommand values.
The update parameters:
<UpdateParameters>
<asp:Parameter Name="EVENTID" Type="Int32"/>
<asp:Parameter Name="EVENTTYPEID" Type="Int32"/>
<asp:Parameter Name="EVENTDATE" Type="DateTime"/>
<asp:Parameter Name="STAFFID" Type="Int32"/>
<asp:Parameter Name="REVIEWNO" Type="Int32"/>
<asp:Parameter Name="COMMENTS" Type="String"/>
<asp:Parameter Name="DESTINYVERIFIED" Type="Int32"/>
<asp:Parameter Name="DESTINYVERIFIEDBY" Type="Int32"/>
<asp:Parameter Name="DESTINYVERIFIEDDATE" Type="DateTime"/>
<asp:Parameter Name="REASONFORSUSPENSIONID" Type="Int32"/>
<asp:Parameter Name="RETURNTOWORKDATE" Type="DateTime"/>
<asp:Parameter Name="ContactDetailsUpdated" Type="Int32"/>
<asp:Parameter Name="RETENTION_STATUS_ID" Type="Int32"/>
<asp:Parameter Name="RETENTION_REASON_ID" Type="Int32"/>
<asp:Parameter Name="NonReview" Type="Int32"/>
<asp:Parameter Name="FalsifiedEventReason" Type="Int32"/>
<asp:Parameter Name="SusReqReasonID" Type="Int32"/>
<asp:Parameter Name="SusReqReturnDate" Type="DateTime"/>
</UpdateParameters>
The stored procedure declaration is as follows:
CREATE PROCEDURE [dbo].[UPDATE_TRAINEE_EVENTS]
#EVENTID int,
#EVENTTYPEID int,
#EVENTDATE datetime,
#STAFFID int,
#REVIEWNO int,
#COMMENTS varchar(2100),
#DESTINYVERIFIED int,
#DESTINYVERIFIEDBY int,
#DESTINYVERIFIEDDATE datetime,
#REASONFORSUSPENSIONID int,
#RETURNTOWORKDATE datetime,
#ContactDetailsUpdated int,
#RETENTION_STATUS_ID int,
#RETENTION_REASON_ID int,
#NonReview int,
#FalsifiedEventReason int,
#SusReqReasonID int,
#SusReqReturnDate datetime
AS
......
I have also run SQL Server Profiler against the session to see what was being passed to the database, however the error comes in before anything hits the database which appears to suggest the problem is within the ASP.NET side of things.
Your <asp:Parameter Name="EVENTID" /> should have a datatype.
I think it should be like this..
<asp:Parameter Name="EVENTID" Type="Int32" />
I have this procedure for performing the search:
ALTER PROCEDURE dbo.book_serchbook
#BookTitle nvarchar(100) ,
#CategoryName nvarchar (100) ,
#AuthorName nvarchar(100),
#PublisherName nvarchar(100)
AS
SELECT DISTINCT
dbo.BookTable.BookID,
dbo.BookTable.BookCover,
dbo.BookTable.BookPrice,
dbo.BookTable.BookTitle,
dbo.PublisherTable.PublisherName,
dbo.CategoryTable.CategoryName
FROM
BookTable
INNER JOIN
CategoryTable ON CategoryTable.CategoryID = BookTable.CategoryID
INNER JOIN
PublisherTable ON PublisherTable.PublisherID = BookTable.PublisherID
LEFT OUTER JOIN
BookAuthorTable
INNER JOIN
AuthorTable ON AuthorTable.AuthorID = BookAuthorTable.AuthorID
ON BookTable.BookID = BookAuthorTable.BookID
WHERE
(PublisherTable.PublisherName LIKE '%' + #PublisherName + '%') and
(BookTable.BookTitle LIKE '%' + #BookTitle + '%') and
(AuthorTable.AuthorFName + ' ' + AuthorTable.AuthorLName LIKE '%' + #AuthorName + '%') and
(CategoryTable.CategoryName= #CategoryName )
On my ASPX page I use a gridview with ObjectDataSource to display results, the object source code Is :
<asp:ObjectDataSource ID="ObjectDataSource4" runat="server"
DataObjectTypeName="BookDataBaseComponent.BookDetails" OldValuesParameterFormatString="original_{0}"
DeleteMethod="DeleteBook" InsertMethod="InsertBook" SelectMethod="SearchBook"
TypeName="BookDataBaseComponent.BookDB" UpdateMethod="UpdateBook">
<DeleteParameters>
<asp:Parameter Name="BookID" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="booktb" DefaultValue="%" Name="BookTitle"
PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="%"
Name="CategoryName" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="afnbk" DefaultValue="%" Name="AuthorName"
PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="pubtb" DefaultValue="%" Name="PublisherName"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
My problem: the page doesn't display any results while the and operator between conditions in the where clause is present. When I change it to OR it displays results
And I can't search when I click the search button nothing happens.
Please any help .I really need help
I have a grid that has some date values. What I like to do is to convert those date values into UTC date format and then enter that inside the table.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KruSQL %>"
UpdateCommand="UPDATE [dbo].[Microbiology] SET RoomNum=#RoomNum, CollDate=#CollDate, WaterFixure=#WaterFixure Where ID=#ID">
<UpdateParameters>
<asp:Parameter Name="RoomNum" Type="String" />
<asp:Parameter Name="WaterFixure" Type="String" />
<asp:Parameter Name="CollDate" Type="DateTime" />
</UpdateParameters>
My qustion is how can I make use of :
UpdateCommand="UPDATE [dbo].[Microbiology] SET RoomNum=#RoomNum, CollDate=#CollDate, WaterFixure=#WaterFixure Where ID=#ID"
and still update the value of CollDate to UTC. Is there anyway to do this in the code behind and still make us of the UpdateCommand
try this :
UpdateCommand="UPDATE [dbo].[Microbiology] SET RoomNum=#RoomNum, CollDate=DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), #ColDate), WaterFixure=#WaterFixure Where ID=#ID"
convert Date to UTCDate :
DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), YOUR_DATE)
I am trying to write an update command that joins 2 tables using an SqlDataSource. I have it working with 1 table, but when I put my INNER JOIN syntax in I get thrown an error. It says "My syntax is wrong. Check MySql manual for correct syntax"
Here is my Code from my ASPX page.:
<asp:SqlDataSource ID="AdminSalesmanDetailDS" runat="server"
ConnectionString="<%$ ConnectionStrings:intelliairConnectionString %>"
ProviderName="<%$ ConnectionStrings:intelliairConnectionString.ProviderName %>"
SelectCommand="individual_AddressByIndividualID"
SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE individual SET
FarmName = #FarmName,
FirstName = #FirstName,
MiddleName = #MiddleName,
Address1 = #Address1,
City = #City
INNER JOIN address a ON i.IndividualID = a.IndividualID,
WHERE IndividualID=#IndividualID">
<SelectParameters>
<asp:ControlParameter ControlID="gvSalesman" Name="oIndividualID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="FarmName" ControlId="fvAdminSalesmanDetail$CompanyTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="FirstName" ControlId="fvAdminSalesmanDetail$FirstNameTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="MiddleName" ControlId="fvAdminSalesmanDetail$MiddleNameTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="Address1" ControlId="fvAdminSalesmanDetail$Address1TextBox" PropertyName="Text"/>
<asp:ControlParameter Name="City" ControlId="fvAdminSalesmanDetail$cityTextBox" PropertyName="Text"/>
</UpdateParameters>
</asp:SqlDataSource>
UPDATE
I have it working with 2 tables now. However it updates every Individual in the Table. For example: I am trying to update 1 person and I change the first name to Mark. When I click update , it changes everyone in the Database First Name to Mark.
Here is my new Code from my ASPX page.:
<asp:SqlDataSource ID="AdminSalesmanDetailDS" runat="server"
ConnectionString="<%$ ConnectionStrings:intelliairConnectionString %>"
ProviderName="<%$ ConnectionStrings:intelliairConnectionString.ProviderName %>"
SelectCommand="individual_AddressByIndividualID"
SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE individual i
inner join address a
on a.individualID = i.individualID
set
i.FarmName = #FarmName,
i.FirstName = #FirstName,
i.LastName = #LastName,
i.MiddleName = #MiddleName,
i.Phone = #Phone,
i.PhoneExtention = #PhoneExtention,
i.MobilPhone = #MobilPhone,
i.Fax = #Fax,
i.Email = #Email,
a.Address1 = #Address1,
a.Address2 = #Address2,
a.City = #City,
a.State = #State,
a.Zip = #Zip,
a.Country = #Country
where
i.IndividualID = i.IndividualID">
<SelectParameters>
<asp:ControlParameter ControlID="gvSalesman" Name="oIndividualID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="FarmName" ControlId="fvAdminSalesmanDetail$CompanyTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="FirstName" ControlId="fvAdminSalesmanDetail$FirstNameTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="MiddleName" ControlId="fvAdminSalesmanDetail$MiddleNameTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="Address1" ControlId="fvAdminSalesmanDetail$Address1TextBox" PropertyName="Text"/>
<asp:ControlParameter Name="City" ControlId="fvAdminSalesmanDetail$cityTextBox" PropertyName="Text"/>
</UpdateParameters>
</asp:SqlDataSource>
You have your JOIN after the SET, and you're not being specific about which table each field is referencing. I think, to use JOIN in an UPDATE, you need syntax more like this:
UpdateCommand=" UPDATE
individual i
INNER JOIN
address a
ON i.IndividualID = a.IndividualID
SET
i.FarmName = #FarmName,
i.FirstName = #FirstName,
i.MiddleName = #MiddleName,
a.Address1 = #Address1,
a.City = #City
WHERE
i.IndividualID=#IndividualID" >
Edit: Based on the update to your question, it looks like you have (in your WHERE clause)
i.IndividualID=i.IndividualID
This is what's causing all your records to be updated (because that statement is always true). As in my above example, you need to have
i.IndividualID=#IndividualID
This way only the row(s) whose ID matches your parameter gets updated (presumably just one).
I am using ASP.NET 3.5 and SQL Server 2008.
I have a SQLDataSource & Gridview. I am trying to update 2 tables at the same time using a stored procedure in SQL.
SQLDatasource is passing 7 parameters.
5 parameters that the stored procedaure needs, Return value & StudentID.
Not sure if the error is in my SQLDatasorce or my stored procedure.
Here's my ASPX Code:
<asp:SqlDataSource ID="sqldsUserLoginNLevels" runat="server"
ConnectionString="<%$ ConnectionStrings:QuizStarConnectionString %>"
SelectCommand="SELECT UserLogins.StudentID, UserLogins.StudentName, UserLogins.UserID,
UserLogins.Password, UserLevels.GrammarStart, UserLevels.GrammarCurrent,
UserLevels.MathStart, UserLevels.MathCurrent
FROM UserLogins
INNER JOIN UserLevels ON UserLogins.StudentID = UserLevels.StudentID"
DeleteCommand="DELETE FROM [UserLogins] WHERE [StudentID] = #original_StudentID"
InsertCommand="INSERT INTO [UserLogins] ([StudentName], [UserID], [Password])
VALUES (#StudentName, #UserID, #Password)"
UpdateCommand="UpdateUserLoginsAndUserLevels"
UpdateCommandType="StoredProcedure" >
<DeleteParameters>
<asp:Parameter Name="original_StudentID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="StudentName" Type="String" />
<asp:Parameter Name="UserID" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="GrammarStart" Type="String" />
<asp:Parameter Name="MathStart" Type="String" />
<asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="StudentName" Type="String" />
<asp:Parameter Name="UserID" Type="String" />
<asp:Parameter Name="Password" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
My Stored Procedure is:
ALTER PROCEDURE [dbo].[UpdateUserLoginsAndUserLevels] (
#StudentName VARCHAR(50),
#UserID NCHAR(10),
#Password NCHAR(10),
#GrammarStart NCHAR(10),
#MathStart NCHAR(10))
AS
DECLARE #StudentID INT;
BEGIN Transaction
BEGIN TRY
SELECT * From UserLogins
Where StudentID = #StudentID
UPDATE UserLogins
SET
StudentName= #StudentName,
UserID = #UserID,
Password = #Password
Where StudentID = #StudentID
UPDATE UserLevels
SET
GrammarStart= #GrammarStart,
MathStart = #MathStart
FROM UserLevels
INNER JOIN UserLogins ON UserLogins.StudentID = UserLevels.StudentID
WHERE (UserLevels.StudentID = #StudentID)
END TRY
BEGIN CATCH
DECLARE #ErrorMessage NVARCHAR(4000), #ErrorSeverity INT
-- Assign variables to error-handling functions that
-- capture information for RAISERROR.
SELECT #ErrorMessage = ERROR_MESSAGE(), #ErrorSeverity = ERROR_SEVERITY()
-- Rollback the failed transaction
ROLLBACK;
-- Raise an error: with the original error information.
RAISERROR(#ErrorMessage, #ErrorSeverity, 1);
END CATCH
COMMIT Transaction;
Have't figured how to post code yet. Sorry.
Make sure you are not double adding parameters. Since you are adding them in the aspx page you do not need to add them in your code behind.
For example if your code behind has this in it:
sqldsUserLoginNLevelsUserID.UpdateParameters.Add(new Parameter("UserID ", TypeCode.Int32));
It could explain your error.
If you need to set the value from code behind do it like this:
sqldsUserLoginNLevelsUserID.UpdateParameters["UserID "].DefaultValue = "1";