I have a datalist control which is connected to a SQLDataSource when i test the data source query with the query builder it returns the data set however when i then run the website in any browser the query does not return a data set.
It seems like the website is not recompiling properly however i am not getting any build errors.
Does anyone have any idea for a possible solution.
Thanks in advance
my sqldatasource code is-
<asp:SqlDataSource ID="SqlDataSourceSearch" runat="server" ConnectionString="<%$ ConnectionStrings:BazaarCeramicsConnectionString %>"
SelectCommand="SELECT Categories.Name, Products.ProductID, Products.Name AS Expr1 FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID WHERE (Products.Name LIKE '%' + #SearchQuery + '%')">
<SelectParameters>
<asp:QueryStringParameter Name="SearchQuery" QueryStringField="search"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
my datalist code is-
<asp:DataList ID="DataList1" runat="server" DataKeyField="ProductID" DataSourceID="SqlDataSourceSearch">
<ItemTemplate>
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
<br />
Expr1:
<asp:Label ID="Expr1Label" runat="server" Text='<%# Eval("Expr1") %>' />
<br />
Here is my input-
<div class="searchBox">
<asp:TextBox ID="SearchTextBox" runat="server"></asp:TextBox>
<asp:Button ID="SearchButton" runat="server" onclick="Button1_Click" Text="Search" />
<img id="cartIcon" src="Images/shoppingCartIcon.png" />
</div>
Here is the code behind
protected void Button1_Click(object sender, EventArgs e)
{
string SearchTerm = SearchTextBox.Text;
Response.Redirect("SearchResults.aspx?SearchQuery=" + Server.UrlEncode(SearchTerm));
}
here is my connection string-
<add name="BazaarCeramicsConnectionString" connectionString="Data Source=CATHERINE\SQLEXPRESS;Initial Catalog=BazaarCeramics;User ID=****;Password=******"
providerName="System.Data.SqlClient" />
(Products.Name LIKE '%' + #SearchQuery + '%')
Related
I have a gridview on my aspx page with 2 textboxes (Firstname and Lastname) that allows the users to filter the gridview when they click on the "Search" button.
When I run this locally on my dev machine it works perfect. Once I deployed it to my IIS server...when a user clicks on the search button nothing happens. I tried to open the page on several differnt computers and the same thing happens on all of them. I noticed when I load the page from the IIS server itself the search button works. Any idea what could cause this?
Below is the code for my textboxes, button and filter parameter:
<!DOCTYPE html>
<p>
Last Name: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br /><br />First Name:
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Button ID="Button4" runat="server" Text="Search" />
<asp:GridView ID="TDetails" DataSourceID="SqlDataSource1" DataKeyNames="PersonID" AutoGenerateColumns="false" runat="server" OnSelectedIndexChanged="Details_SelectedIndexChanged" AllowPaging="True" PageSize="7" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnSelect" runat="server" CommandName="Select" Text="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="SqlDataSource1" runat="server"
connectionString="Data Source=Server\SQLEXPRESS;Initial Catalog=Test;persist security info=True;user id=user;password=password"
SelectCommand="SELECT * FROM Person ORDER BY LastName"
FilterExpression="[LastName] LIKE '{0}%' AND [FirstName] LIKE '{1}%'">
<FilterParameters>
<asp:ControlParameter Name="LastName" ControlID="TextBox3" PropertyName="Text" ConvertEmptyStringToNull="false"/>
<asp:ControlParameter Name="FirstName" ControlID="TextBox4" PropertyName="Text" ConvertEmptyStringToNull="false"/>
</FilterParameters>
</asp:SqlDataSource>
I am trying to insert values from a textbox but I get an error that it cannot find the controlid in the controlparameter. The TextBox is inside a formview, which is inside a listview. The SqlDataSource is outside the ListView.
My InsertButton_Click Method
protected void InsertButton_Click(object sender, EventArgs e)
{
var ctrl = (Control)sender;
var formview = (FormView)ctrl.NamingContainer;
formview.ChangeMode(FormViewMode.Insert);
SectionListView.DataSource = SectionDataSource;
SectionListView.DataBind();
}
The InsertItem Template
<InsertItemTemplate>
<tr style="">
<td>
<div style="font-size: .8em;">
<asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
<ItemTemplate>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" Font-Size="1.2em" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
<asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemTextBox" runat="server" />
<asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemLabelTextBox" runat="server"/>
</ItemTemplate>
</asp:FormView>
</div>
</td>
</tr>
</InsertItemTemplate>
My SqlDataSource
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
When you want to use the child controls as control parameters, you need to use the qualified id as control id. Try changing the SQL data source as below
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionFormView$SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionFormView$SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
I have the following SqlDataSource:
<asp:SqlDataSource ID="LDetails" runat="server" ConnectionString="<%$ ConnectionStrings:NormCon %>"
SelectCommand ="SELECT l.Link_ID,l.Link_Name,l.Link_Path,l.Link_Desc
FROM HyperLinks l
WHERE l.link_id = #Link_ID;"
UpdateCommand ="UPDATE Hyperlinks SET Link_Name = #Link_Name, Link_Path = #Link_Path, Link_Desc = #Link_Desc WHERE Link_ID = #Link_ID;"
DeleteCommand ="DELETE FROM Hyperlinks_Groups WHERE Join_Link = #Link_ID; DELETE FROM Hyperlinks WHERE Link_ID = #Link_ID;"
<SelectParameters>
<asp:ControlParameter Name="Link_ID" ControlID="linkList" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Link_Name" />
<asp:Parameter Name="Link_Path" />
<asp:Parameter Name="Link_Desc" />
<asp:ControlParameter Name="Link_ID" ControlID="linkList" PropertyName="SelectedDataKey.Value" />
</UpdateParameters>
<DeleteParameters>
<asp:ControlParameter Name="Link_ID" ControlID="linkList" PropertyName="SelectedDataKey.Value" />
</DeleteParameters>
</asp:SqlDataSource>
this is the detailsview I have:
<asp:DetailsView ID="LinkDetails" runat="server" DataSourceID="LDetails"
DataKeyNames="Link_ID" AutoGenerateRows="False" OnItemDeleted="deleteLinkReload" >
<Fields>
<asp:BoundField DataField="Link_ID" />
<asp:TemplateField HeaderText="Link Name">
<ItemTemplate>
<asp:Label ID="lblLinkName" runat="server" Text='<%# Bind("Link_Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLinkName" runat="server" Text='<%# Bind("Link_Name") %>' MaxLength="50" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:HyperLink ID="lnkLinkPath" runat="server" Text='<%# Bind("Link_Path") %>' NavigateUrl='<%# Bind("Link_Path") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLinkActual" runat="server" Text='<%# Bind("Link_Path") %>' MaxLength="250" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:Label ID="lblLinkComment" runat="server" Text='<%# Bind("Link_Desc") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLinkComment" runat="server" Text='<%# Bind("Link_Desc") %>' MaxLength="250" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="linkDetailsEdit" runat="server" CommandName="edit" Text="Edit" />
<asp:LinkButton ID="linkDetailsDelete" runat="server" OnClientClick="return confirm('OK to delete link?');" CommandName="Delete" Text="Delete Link" /> <%--OnClick="DeleteLink" --%>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="linkDetailsAccept" runat="server" CommandName="update" Text="Accept" />
<asp:LinkButton ID="linkDetailsCancel" runat="server" CommandName="Cancel" CausesValidation="false" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
When I click a delete button in the detailsview which is linked to this, the page refreshes but the item is not deleted. I think the DeleteCommand is returning 0 rows or erroring without me being able to catch the error. I've tried seeing if its a case of the DeleteCommand even firing by switching the SQL to this:
DeleteCommand ="INSERT INTO mis_test (string) VALUES ('Hi');"
and it still didn't do anything. Any ideas?
Try using SQL Server Profiler to see what is run on the database, and what error it gives.
I would bet that your connection string includes things like User Instance=true and AttachDbFileName=..., and that after you run the delete command, you are connecting via SSMS or Visual Studio (not your app) and checking the table.
If this is the case, then you need to stop using this User Instance feature (which has been deprecated). What this does is create a copy of your database for each instance of any application that connects.
What you need to do is properly attach your database to a real instance of SQL Server, and connect to that copy of the database from all applications.
i want to update the records from the gridview using SqlDataSource, here is what i am doing.
below is my gridview markup
<asp:GridView ID="grdManageFaculties" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="LocalServerDataSource" AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
Width="100%" OnRowUpdating="grdManageFaculties_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="MANAGE">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMAIL">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditEmail" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MOBILE">
<ItemTemplate>
<asp:Label ID="lblMobileNumber" runat="server" Text='<%# Eval("Mobile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditMobileNumber" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LOCKED">
<ItemTemplate>
<asp:CheckBox ID="chkIsLocked" runat="server" Enabled="false" Checked='<%# Eval("Locked") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkEditIsLocked" runat="server" Checked='<%# Bind("Locked") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CREATED">
<ItemTemplate>
<asp:Label ID="lblCreated" runat="server" Text='<%# Eval("Created") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditCreated" runat="server" Text='<%# Eval("Created") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
below is my markup for the SqlDataSource
<asp:SqlDataSource ID="LocalServerDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>"
SelectCommand="users_GetAllUsers" SelectCommandType="StoredProcedure" UpdateCommand="users_UpdateFaculty" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="EMAIL" Type="String" />
<asp:Parameter Name="ISLOCKEDOUT" Type="Boolean" />
<asp:Parameter Name="MOBILENUMBER" Type="Int64" />
<asp:Parameter Name="USERNAME" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
below is my code-behind for the Row_Updating function
protected void grdManageFaculties_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
TextBox email = grdManageFaculties.Rows[e.RowIndex].FindControl("txtEditEmail") as TextBox;
Label username = grdManageFaculties.Rows[e.RowIndex].FindControl("lblEditUserName") as Label;
CheckBox locked = grdManageFaculties.Rows[e.RowIndex].FindControl("chkEditIsLocked") as CheckBox;
TextBox mobilenumber = grdManageFaculties.Rows[e.RowIndex].FindControl("txtEditMobileNumber") as TextBox;
LocalServerDataSource.UpdateParameters["EMAIL"].DefaultValue = email.Text;
LocalServerDataSource.UpdateParameters["ISLOCKEDOUT"].DefaultValue = locked.Checked.ToString();
LocalServerDataSource.UpdateParameters["MOBILENUMBER"].DefaultValue = mobilenumber.Text;
LocalServerDataSource.UpdateParameters["USERNAME"].DefaultValue = username.Text;
LocalServerDataSource.Update();
}
catch { }
}
below is my Stored Procedure for Update
ALTER PROCEDURE users_UpdateFaculty
#EMAIL NVARCHAR(100),
#ISLOCKEDOUT BIT,
#MOBILENUMBER BIGINT,
#USERNAME nvarchar(100)
AS
BEGIN
UPDATE aspnet_Users SET MOBILENUMBER=#MOBILENUMBER where USERNAME=#USERNAME
UPDATE ASPNET_MEMBERSHIP SET EMAIL = #EMAIL, LOWEREDEMAIL = LOWER(#EMAIL), ISLOCKEDOUT=#ISLOCKEDOUT WHERE USERID = (SELECT USERID FROM ASPNET_USERS WHERE USERNAME=#USERNAME)
END
my records in database is getting updated, but when i click on update button, i gets the below error:
Procedure or function users_UpdateFaculty has too many arguments specified.
Can anyone help me what could be causing this issue, i am using all the parameters properly.
Found the solution:
The Select Columns and the Update Parameters should match in order to update using SqlDataSource, that means if you select(query or procedure) is returning 3 fields in gridview, then all of them should be the parameter for the update, you can miss out the actual updations in database if not required, but the <UpdateParameters> should have all the fields: say for example if below is my select query
SELECT USERNAME, MOBILENUMBER, EMAIL FROM USERS
then the update parameters should be
<UpdateParameters>
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="MobileNumber" Type="Int64" />
<asp:Parameter Name="Email" Type="String" />
<UpdateParameters>
you cannot skip any of the parameters, even though you do not intend to update that field
Hope this will help others, as i wasted lots of time researching on this.
I dont think your solution is OK, since I have to use the atribute OldValuesParameterFormatString="original_{0}" that means that I would have doble parameters, the ones with the original values and the ones with edited values. So there is no way to match the order.
I have 4 parameters, Im getting the right values for 2 of them, an null at the others.
I tried your solution and did not work. Thanks anyway.
Read this:
The order of the parameters in the UpdateParameters collection might be important, depending on the ADO.NET provider. The System.Data.OleDb and System.Data.Odbc providers associate the parameters in the collection according to the order that the parameters appear in the parameterized SQL query. The System.Data.SqlClient provider, which is the default ADO.NET provider for the SqlDataSource control, associates the parameters in the collection by matching the name of the parameter with a placeholder alias in the SQL query. For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.updateparameters(v=vs.110).aspx
I am trying to perform a single search on a single column of data in an asp:GridView, however I want multiple filter (search) boxes above each column so one could search within its own perspective column. What would clearly fix this theoretically is to have multiple FilterExpressions. I am using VB and am very new and easily misunderstood with VB, please bare with:
PS. I have gotten it to work no problem when only having ONE filter (search) box, this currently doesn't do anything
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="xxx"
ProviderName="xxxx"
SelectCommand="SELECT [ddiID], [volusionID], [Customer], [email], [Total], [SumOfTotal] FROM [BBnewsalesQry] ORDER BY [Customer]"
FilterExpression="customer like '%{0}%' OR ddiID like '%{0}%'">
<FilterParameters>
<asp:ControlParameter Name="ddiID" ControlID="ddiIDSearch" PropertyName="Text" />
<asp:ControlParameter Name="customer" ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
<div>
<b>DDI Search:</b> <asp:TextBox ID="ddiIDSearch" runat="server" />
<b>Customer Search:</b> <asp:TextBox ID="txtSearch" runat="server" />
<asp:ImageButton ID="btnSearch" ImageUrl="http:xxx" runat="server" />
<asp:ImageButton ID="btnClear" ImageUrl="http:xxx" runat="server" />
</div>
Then inside the GridView:
<asp:TemplateField HeaderText="ddiID" SortExpression="ddiID">
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblddiid" Text='<%#HighlightText(Eval("ddiid")) %>'
CssClass="TextField" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="volusionID" HeaderText="volusionID" ReadOnly="True" SortExpression="volusionID" />
<asp:TemplateField HeaderText="customer" SortExpression="customer">
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblcustomer" Text='<%#HighlightText(Eval("customer")) %>'
CssClass="TextField" runat="server" />
</ItemTemplate>
</asp:TemplateField>
you are referencing the same parameter twice - try the following:
FilterExpression="customer like '%{0}%' OR ddiID like '%{1}%'"
I have resolved the problem of create two or more "Like clause" in the "Where" statement using:
Where CHARINDEX(#Par1, Col1)<>0) and ...