search gridview with objectdatasource - asp.net

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

Related

Making a dynamic gridview matrix with 'LIKE'

Earlier I had a question that and was given some excellent direction from Nikki9696 reguarding dynamic datasource values for my gridview which I'm showing below as I currently have it implemented and working. This works quite well. I have laid out a series of text boxes with label above them directly above my grid view and the user can either leave them blank (resulting in no filtering of records) or provide values that would match items in any one of the corresponding columns represented by the text boxes which would limit that column to only that value as part of the filter and at the same time allow the user to compound multiple boxes to narrow the filter as well. Works really well!
I would really like to take it one step further and have wrestled with it for about week and gotten no where despite experimenting and searching for examples on the web of where someone is doing similar filtering. I'd like each of these to function instead of being = or null to LIKE or null in that way my users could supply partial information in the text boxes to pull up results rather than an exact match. I just can't figure out how to alter my WHERE clause to do this.
<asp:SqlDataSource ID="InventoryList" runat="server" ConnectionString='<%$ ConnectionStrings:CMDB_testConnectionString %>' SelectCommand="SELECT [AssetID], [AssetType], [AssetName], [AssetShortDesc], [AssetLongDesc], [AssetAddNotes], [AssetManuf], [AssetModel], [AssetTag], [AssetSerialNum], [AssetAcqDate], [AssetDTAssetID], [AssetLocGrp], [AssetLoc1], [AssetLoc2], [AssetLoc3], [AssetParent], [AssetStatus], [AssetPropType], [AssetPrimUser], [AssetEntered], [AssetEnteredBy], [AssetOwner], [AssetCompany], [AssetPriIPAddr], [AssetPriMACAddr], [AssetPriOS], [AssetPriOSSP], [AssetNotes], [AssetAdminGrp], [AssetOrgID], [AssetOperType], [AssetOperStatus] FROM [cmdbv_Assets_CInTrac] where AssetID=isnull(#AssetID,AssetID) and AssetName=isnull(#AssetName,AssetName) and AssetType=isnull(#AssetType,AssetType) and AssetManuf=isnull(#AssetManuf,AssetManuf) and AssetModel=isnull(#AssetModel,AssetModel) and AssetTag=isnull(#AssetTag,AssetTag) and AssetSerialNum=isnull(#AssetSerialNum,AssetSerialNum) and AssetDTAssetID=isnull(#AssetDTAssetID,AssetDTAssetID) and AssetLocGrp=isnull(#AssetLocGrp,AssetLocGrp) and AssetLongDesc=isnull(#AssetLongDesc,AssetLongDesc) and AssetOrgID=isnull(#AssetOrgID,AssetOrgID) and AssetPriIPAddr=isnull(#AssetPriIPAddr,AssetPriIPAddr) and AssetStatus=isnull(#AssetStatus,AssetStatus)" CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:Controlparameter Name="AssetID" ControlID="AssetIDTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetName" ControlID="AssetNameTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetType" ControlID="AssetTypeTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetShortDesc" ControlID="AssetShortDescTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetManuf" ControlID="AssetManufTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetModel" ControlID="AssetModelTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetTag" ControlID="AssetTagTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetSerialNum" ControlID="AssetSerialNumTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetDTAssetID" ControlID="AssetDTAssetIDTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetLocGrp" ControlID="AssetLocGrpTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetLongDesc" ControlID="AssetLongDescTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetOrgID" ControlID="AssetOrgIDTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetPriIPAddr" ControlID="AssetPriIPAddrTbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetStatus" ControlID="AssetStatusTbx" Type="String" ConvertEmptyStringToNull="true" />
</SelectParameters>
I have tried variations of AssetName='%#AssetName%' and AssetName='%'+#AssetName+'%' as well as sever other contraptions but not had anything that even did anything more than completely blocked all rows from being returned.
Any ideas would be greatly appreciated because I really would like to see this on my filters.
Regards,
Ken...
Okay we seem to be making a little progress here but not fully. Here is what I modified the AssetName to reflect in the datasource I may have messed up something on the syntax but I thought I had it exactly as you had stated it.
([AssetName] LIKE '%' + CASE WHEN #AssetName is null Then '' Else #AssetName End + '%')
Latest Changes to the DataSource per your suggestion:
<asp:SqlDataSource ID="InventoryList" runat="server"
ConnectionString='<%$ ConnectionStrings:CMDB_testConnectionString %>'
SelectCommand="SELECT [AssetID], [AssetType], [AssetName], [AssetShortDesc], [AssetLongDesc], [AssetAddNotes], [AssetManuf], [AssetModel], [AssetTag], [AssetSerialNum], [AssetAcqDate], [AssetDTAssetID], [AssetLocGrp], [AssetLoc1], [AssetLoc2], [AssetLoc3], [AssetParent], [AssetStatus], [AssetPropType], [AssetPrimUser], [AssetEntered], [AssetEnteredBy], [AssetOwner], [AssetCompany], [AssetPriIPAddr], [AssetPriMACAddr], [AssetPriOS], [AssetPriOSSP], [AssetNotes], [AssetAdminGrp], [AssetOrgID], [AssetOperType], [AssetOperStatus] FROM [cmdbv_Assets_CInTrac]"
FilterExpression=" Convert(AssetID, 'System.String') LIKE '{0}%' 
and AssetName LIKE '{1}%' 
and AssetType LIKE '{2}%' 
and AssetShortDesc LIKE '{3}%' 
and AssetManuf LIKE '{4}%' 
and AssetModel LIKE '{5}%' 
and AssetTag LIKE '{6}%' 
and AssetSerialNum LIKE '{7}%' 
and AssetDTAssetID LIKE '{8}%' 
and AssetLocGrp LIKE '{9}%' 
and AssetLongDesc LIKE '{10}%' 
and AssetOrgID LIKE '{11}%' 
and AssetPriIPAddr LIKE '{12}%' 
and Convert(AssetStatus, 'System.string') LIKE '{13}%'">
<FilterParameters>
<asp:ControlParameter Name="AssetID" ControlID="AssetIDTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetName" ControlID="AssetNameTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetType" ControlID="AssetTypeTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetShortDesc" ControlID="AssetShortDescTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetManuf" ControlID="AssetManufTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetModel" ControlID="AssetModelTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetTag" ControlID="AssetTagTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetSerialNum" ControlID="AssetSerialNumTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetDTAssetID" ControlID="AssetDTAssetIDTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetLocGrp" ControlID="AssetLocGrpTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetLongDesc" ControlID="AssetLongDescTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetOrgID" ControlID="AssetOrgIDTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetPriIPAddr" ControlID="AssetPriIPAddrTbx"  PropertyName="Text" />
<asp:ControlParameter Name="AssetStatus" ControlID="AssetStatusTbx"  PropertyName="Text" />
</FilterParameters> 
</asp:SqlDataSource>
Then you need to pass the value as string.empty and also you need to allow covertingEmptyStringToNull for each and every QueryStringParameter.
So the steps are as simple as the following:
1- Add ConvertEmptyStringToNull="true" for each QueryStringParameter in you query. The following example shows how your QueryStringParameter elements should look like:
<asp:QueryStringParameter QueryStringField="#AssetName"
DefaultValue=""
Name="AssetName"
Type="String"
ConvertEmptyStringToNull="true">
</asp:QueryStringParameter>
2- Now, In order to allow string.Empty to be passed in the queryString in the SqlDataSource, you can use CASE WHEN in your Where clause. The following is an example to demonstrate this. Then in your scenario you can apply this to each parameter.
SELECT [AssetName]
FROM [cmdbv_Assets_CInTrac]
WHERE ([AssetName] LIKE '%' +
CASE WHEN #AssetName is NULL
Then ''
Else #AssetName
End + '%')
To map LIKE directly with a textbox, you can also use [FilterExpression][1]. For example, assuming you have the following Textbox for filtering
<asp:TextBox ID="searchTextBox" runat="server"></asp:TextBox>
You can code your SqlDataSource as shown in the following example:
<asp:SqlDataSource ID="gridDataSource" runat="server"
ConnectionString='<%$ ConnectionStrings:CMDB_testConnectionString %>'
SelectCommand="SELECT [AssetName] FROM [cmdbv_Assets_CInTrac]"
FilterExpression="AssetName LIKE '{0}%'">
<FilterParameters>
<asp:ControlParameter Name="AssetName" ControlID="searchTextBox" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
In your scenario you will have to create ControlParameter for each column you want to map to a TextBox. Make sure you map by the textbox control id as shown in the example above.
Update 2
Your statement should look like the following.. In your answer you repeated FilterExpression and didn't increment the {0} for each column. So here's how your SqlDataSource should be
<asp:SqlDataSource ID="InventoryList" runat="server"
ConnectionString='<%$ ConnectionStrings:CMDB_testConnectionString %>'
SelectCommand="SELECT [AssetID], [AssetType], [AssetName], [AssetShortDesc],
[AssetLongDesc], [AssetAddNotes], [AssetManuf], [AssetModel], [AssetTag]
[AssetSerialNum], [AssetAcqDate], [AssetDTAssetID], [AssetLocGrp], [AssetLoc1],
[AssetLoc2], [AssetLoc3], [AssetParent], [AssetStatus], [AssetPropType],
[AssetPrimUser], [AssetEntered], [AssetEnteredBy], [AssetOwner], [AssetCompany],
[AssetPriIPAddr], [AssetPriMACAddr], [AssetPriOS], [AssetPriOSSP], [AssetNotes],
[AssetAdminGrp], [AssetOrgID], [AssetOperType], [AssetOperStatus]
FROM [cmdbv_Assets_CInTrac]"
FilterExpression=" Convert(AssetID , 'System.String') LIKE '{0}%'
AND AssetName LIKE '{1}%'
And AssetType LIKE '{2}%'
AND AssetShortDesc LIKE '{3}%'
AND AssetManuf LIKE '{4}%'
AND AssetModel LIKE '{5}%'
AND AssetTag LIKE '{6}%'
AND Convert(AssetSerialNum , 'System.String') LIKE '{7}%'
AND Convert(AssetDTAssetID , 'System.String') LIKE '{8}%'
AND AssetLocGrp LIKE '{9}%'
AND AssetLongDesc LIKE '{10}%'
AND Convert( AssetOrgID, 'System.String') LIKE '{11}%'
AND AssetPriIPAddr LIKE '{12}%'
AND AssetStatus LIKE '{13}%'">
<FilterParameters>
<asp:Controlparameter Name="AssetID" ControlID="AssetIDTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetName" ControlID="AssetNameTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetType" ControlID="AssetTypeTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetShortDesc" ControlID="AssetShortDescTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetManuf" ControlID="AssetManufTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetModel" ControlID="AssetModelTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetTag" ControlID="AssetTagTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetSerialNum" ControlID="AssetSerialNumTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetDTAssetID" ControlID="AssetDTAssetIDTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetLocGrp" ControlID="AssetLocGrpTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetLongDesc" ControlID="AssetLongDescTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetOrgID" ControlID="AssetOrgIDTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetPriIPAddr" ControlID="AssetPriIPAddrTbx" PropertyName="Text" />
<asp:ControlParameter Name="AssetStatus" ControlID="AssetStatusTbx" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>

Filtering gridview Between dates does not get records for Date To

I am trying to filter a gridview to show orders between specific dates. I managed to make it work but it does not show the records for the Date To. It only shows the between of that dates. The records that exist in Date from shows up...
<asp:EntityDataSource ID="EDS_Orders" runat="server" EnableUpdate="true" EnableFlattening="False" OrderBy="it.Order_date ASC"
ConnectionString="name=Bohemian1Entities" DefaultContainerName="Bohemian1Entities" EntitySetName="t_customer_orders"
Where="it.UserName is not null and (it.Order_date Between #DateFrom AND #DateTo) AND (it.UserName Like '%' + #UserName + '%') " >
<WhereParameters>
<asp:ControlParameter ControlID="ddlDateFrom" Name="DateFrom"
Type="DateTime" DefaultValue="%"/>
<asp:ControlParameter ControlID="ddlDateTo" Name="DateTo"
Type="DateTime" DefaultValue="%"/>
<asp:ControlParameter ControlID="ddlCustomers" Name="UserName" PropertyName="Text"
Type="String" DefaultValue="%"/>
</WhereParameters>
</asp:EntityDataSource>
Try to rewrite Where clause as below:
Where="it.UserName is not null and (CAST(it.Order_date AS DATE) Between #DateFrom AND #DateTo)
AND (it.UserName Like '%' + #UserName + '%')"

Syntax error using INNER JOIN and SqlDataSource with MySQL

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).

GridView SelectCommand missing WHERE at runtime

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!

SWL Error Function has too many arguments specified

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";

Resources