I am currently experiencing a really weird problem with deleting a row from a GridView. I have the following code, and it throws the following exception:
ORA-01008: not all variables bound
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="DeleteBTN" runat="server" OnClientClick="return confirm('Are you sure you want to delete this move request?')" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UNIQUEKEY" HeaderText="Unique Key"
SortExpression="UNIQUEKEY" />
<asp:SqlDataSource ID="GridViewDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:Oracle1ConnectionString %>"
ProviderName="<%$ ConnectionStrings:Oracle1ConnectionString.ProviderName %>"
SelectCommand="SELECT ORIGINATOR, TO_CHAR(REQUESTDATE, 'MM/DD/YYYY') as REQUESTDATE, PARTNUMBER, REQUESTQTY, MOVEFROM, MOVETO, COMPLETEDBY, TO_CHAR(COMPLETION_DATE, 'MM/DD/YYYY') as COMPLETION_DATE, COMMENTS, RESPONSETIME, PROCESS_FLAG, UNIQUEKEY FROM MATERIALMOVEREQUEST WHERE RTRIM(PROCESS_FLAG) = :ProcessFlag ORDER BY REQUESTDATE DESC"
DeleteCommand="DELETE FROM MATERIALMOVEREQUEST WHERE UNIQUEKEY = :UNIQUEKEY">
<SelectParameters>
<asp:ControlParameter ControlID="RequestTypeLabel" DefaultValue="" Name="ProcessFlag" PropertyName="Text" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="UNIQUEKEY" Type="Decimal" />
</DeleteParameters>
UNIQUEKEY is a 'Number' field in my Oracle table and I have tried all kinds of data types where it is currently a 'decimal' type.
Use Deleting event of SqlDataSource and try writing following code:
e.InputParameters["UNIQUEKEY"] = value;
value is what you want to send to database
Fixed it myself, had to add the primary keys to the GridView parameters (DATAKEYNAMES). Here is my code snippet:
<asp:GridView ID="MMRGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="UNIQUEKEY"
DataSourceID="GridViewDataSource" Width="980px" OnRowDataBound="GridViewRowEventHandler"
EmptyDataText="No Data Found" AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="Delete" Visible = "true">
<ItemTemplate>
<asp:LinkButton ID="DeleteBTN" runat="server" OnClientClick="return confirm('Are you sure you want to delete this move request?')" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Related
I have the below but the update from gridview doesn't seem to work and throws this error:
Incorrect syntax near 'nvarchar'.
Must declare the scalar variable "#id".
I have added the datakeynames to the gridview and the update parameters to the SqlDataSource but still can't see what's wrong...
Doing a delete works fine.
I have tried reduced the update parameters one by one but still not found the cause
<asp:SqlDataSource ID="sqldsEvents" runat="server"
ConnectionString='<%$ ConnectionStrings:dbIPRadioConnectionString %>'
SelectCommand="SELECT id, eventid, [start time], duration, active, [event desc] FROM tblEvents"
UpdateCommand="UPDATE tblEvents SET [start time] = #starttime WHERE (id = #id)"
InsertCommand="INSERT INTO tblEvents(eventid, [start time], duration, active, [event desc]) VALUES (#eventid, #starttime, #duration, #active, #eventdesc)"
DeleteCommand="DELETE FROM tblEvents WHERE (id = #id)">
<DeleteParameters>
<asp:Parameter Name="id"></asp:Parameter>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="eventid"></asp:Parameter>
<asp:Parameter Name="starttime"></asp:Parameter>
<asp:Parameter Name="duration"></asp:Parameter>
<asp:Parameter Name="active"></asp:Parameter>
<asp:Parameter Name="eventdesc"></asp:Parameter>
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="starttime"></asp:Parameter>
<asp:Parameter Name="id"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<div class="col-sm-12 text-center">
<asp:GridView ID="gvEvents" runat="server" DataKeyNames="id"
CssClass="table table-bordered" DataSourceID="sqldsEvents"
BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
CellPadding="4" AutoGenerateColumns="False"
EmptyDataText="No Events Configured">
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update" CommandName="Update" CausesValidation="True" ID="LinkButton1"></asp:LinkButton> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" ID="LinkButton2"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CausesValidation="False" ID="LinkButton1"></asp:LinkButton> <asp:LinkButton runat="server" Text="Select" CommandName="Select" CausesValidation="False" ID="LinkButton2"></asp:LinkButton> <asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="False" ID="LinkButton3"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id"></asp:BoundField>
<asp:BoundField DataField="eventid" HeaderText="eventid" SortExpression="eventid"></asp:BoundField>
<asp:BoundField DataField="start time" HeaderText="start time" SortExpression="start time"></asp:BoundField>
<asp:BoundField DataField="duration" HeaderText="duration" SortExpression="duration"></asp:BoundField>
<asp:BoundField DataField="event desc" HeaderText="event desc" SortExpression="event desc"></asp:BoundField>
<asp:TemplateField HeaderText="active" SortExpression="active">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="0" Value="0"></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("active") %>' ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The problem occurs because DataField attribute inside GridView's BoundField does not accept column names that using whitespaces.
Better to use underscores for DataField and SortExpression field/column names, also using underscore-based alias names for every columns containing whitespaces in SqlDataSource:
GridView control
<asp:GridView ID="gvEvents" ... >
<%-- other fields --%>
<asp:BoundField DataField="start_time" HeaderText="start time" SortExpression="start_time"></asp:BoundField>
<asp:BoundField DataField="event_desc" HeaderText="event desc" SortExpression="event_desc"></asp:BoundField>
<%-- other fields --%>
</asp:GridView>
SqlDataSource control
<asp:SqlDataSource ID="sqldsEvents" runat="server" ConnectionString='<%$
ConnectionStrings:dbIPRadioConnectionString %>'
SelectCommand="SELECT id, eventid, [start time] AS start_time, duration, active, [event desc] AS event_desc FROM tblEvents"
<%-- other items --%>
</asp:SqlDataSource>
Furthermore, if you have access to modify column names in SSMS, changing all column names with whitespaces to underscores like this update command should work without hassle:
UpdateCommand="UPDATE tblEvents SET start_time = #starttime WHERE (id = #id)"
Note:
Column/field names in every table should follow database identifier convention to ensure CRUD commands are executed successfully.
Similar issue: incorrect syntax near nvarchar in gridview
I am using asp.net C# with telerik.
I have radgrid and a button add new record. I press it and fill up the informations but the problem is when I press insert it says that ParentDist and RankLevel can not be null.
In other words I can get the value that i select from the dropdowns. What I do wrong and how I can fix it?
<telerik:GridTemplateColumn DataField="ParentDist" FilterControlAltText="Filter ParentDist column" HeaderText="ParentDist" SortExpression="ParentDist" UniqueName="ParentDist">
<EditItemTemplate>
<telerik:RadDropDownList ID="ParentDistDropDown" runat="server" DataSourceID="ParentSqlDataSource" DataTextField="Name" DataValueField="IDnumber" DefaultMessage="Select If Exists">
</telerik:RadDropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ParentDistTextBox" runat="server" Text='<%# Eval("ParentDist") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="RankLevel" DataType="System.Int32" FilterControlAltText="Filter RankLevel column" HeaderText="RankLevel" SortExpression="RankLevel" UniqueName="RankLevel">
<EditItemTemplate>
<telerik:RadDropDownList ID="RadDropDownList1" runat="server" DataSourceID="RankSqlDataSource" DataTextField="RankDescr" DataValueField="RankID" > </telerik:RadDropDownList> </EditItemTemplate>
<ItemTemplate>
<asp:Label ID="RankLevelTextBox" runat="server" Text='<%# Eval("RankLevel") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
InsertCommand="INSERT INTO edmark_distriblist (ParentDist,RankLevel) VALUES ( #ParentDistDropDown, #RadDropDownList1,)"
>
<InsertParameters>
<asp:Parameter Name="ParentDistDropDown" Type="String" ></asp:Parameter>
<asp:Parameter Name="RadDropDownList1" Type="Int32"></asp:Parameter>
</InsertParameters>
Use the Bind expression (https://msdn.microsoft.com/en-us/library/vstudio/ms178366%28v=vs.100%29.aspx), as shown here: http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx. Note the SelectedValue property of the dropdown lists.
Or, just use the InsertCommand event, access the controls (http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html) and use their values to compile the query yourself.
I am creating a GridView that will initially have a blank row (consisting of 2 DropDownLists and one TextBox). There is a button in the footer that will allow a user to add another row after they have filled in the first. I am binding this to a SqlDataSource so I am able to use the InsertCommand to call a stored procedure to Insert the user's data into a database table. However, I am not sure how to go about adding an empty row. Right now, the headers will not show up until a postback call is executed. I have seen tutorials that use a DataTable to add an empty row, but I could not get this to work with the bound DropDownLists. Is there a way to add a row to the SqlDataSource from code behind? The following is the relevant part of my ASP page:
<table>
<tr>
<td colspan="2" style="width:100%">
<div>
<asp:UpdatePanel ID="upUpdateChecklist" runat="server">
<ContentTemplate>
<asp:GridView ID="gvUpdate" runat="server" AutoGenerateColumns="False"
CssClass="mGrid" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt"
ShowHeaderWhenEmpty="true"
Visible="true" CellSpacing="2"
CellPadding="2" ShowFooter="true"
DataSourceID="dsUpdate">
<Columns>
<asp:TemplateField HeaderText="Division/Context">
<ItemTemplate>
<asp:DropDownList ID="ddDivision" runat="server" DataTextField="Division" DataValueField="ID" DataSourceID="dsDivision"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Application">
<ItemTemplate>
<asp:DropDownList ID="ddApplication" runat="server" DataTextField="Application" DataValueField="ID" DataSourceID="dsApplication" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task">
<ItemTemplate>
<asp:TextBox ID="tbTask" runat="server" ReadOnly="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="btnAdd" Text="Add New Row" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsDivision" SelectCommand="SELECT ID, Division FROM Automation.dbo.Division ORDER BY Application, Division"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsUpdate" runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"
SelectCommand="">
<InsertParameters>
<asp:Parameter Name="Division" DbType="Int32" />
<asp:Parameter Name="Application" DbType="Int32" />
<asp:Parameter Name="Task" DbType="String" />
</InsertParameters>
</asp:SqlDataSource>
I am not completely sure I understand how do you intend to achieve what you want but if you want to generate an empty row, change the select command of the SQL data source to do a union with an empty dummy row.
<asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications UNION select -1 as ID, '' as ApplicationName "
I have a sqldatasource, in select command i have #status parameter. The parameter take the value from the textbox at runtime.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details] WHERE ([status] = #status)"
FilterExpression="title='{4}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
my problem is that when i run the page without entring the parameter in text box sqldatasource is not returing any row.
Looking at the documentation on MSDN, you have to alter how you have setup the SqlDataSource.
Try this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details]"
FilterExpression="title='{0}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
I have removed the Where clause from the query as this will be applied by the filter expression. Also, I have changed the filter expression from title='{4}' to title='{0}'. The documentation states that the number is a placeholder to an item in the FilterParameters collection.
Updated
I have also changed the SelectParameters to FilterParameters
Update 2
I have created a working example to finish of this answer. This will filter the Title column using the text from the text box. If this text box is empty it will return all the rows from the table (a scary thought but OK for this example). It is querying the AdventureWorks database for which I set a connection string called AWorks.
<asp:SqlDataSource ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:AWorks %>"
SelectCommand="SELECT ContactId, Title, FirstName, LastName FROM Person.Contact"
FilterExpression="Title='{0}'"
runat="server">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlID="txtTitle" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
<asp:TextBox runat="server" Id="txtTitle"></asp:TextBox>
<asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />
<asp:GridView
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:BoundField Visible="false" DataField="ContactId"></asp:BoundField>
<asp:BoundField Visible="true" DataField="Title"></asp:BoundField>
<asp:BoundField Visible="true" DataField="FirstName"></asp:BoundField>
<asp:BoundField Visible="true" DataField="LastName"></asp:BoundField>
</Columns>
</asp:GridView>
try a condition like this:
(#Status is null or #Status ='' Or Status = #Status)
set ConvertEmptyStringToNull="true" and then try....
Add CancelSelectOnNullParameter="false" to your SqlDataSource.
Gridview has many columns and a Delete button as well. Delete button is a control as TemplateField
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnDelete" CommandName="Delete" Text='<%# Eval("disabled").ToString()=="False" ? "Disabled" : "Enabled" %>'
OnClientClick="return confirm('Are you sure you want to take this action?');"
runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Now associated SQLDataSource's Delete command's stored procedure is expecting two parameters. One is going from DataKeyNames (RowID), other one i wanna pass is the Text of btnDelete (True or False).
How can i achieve it?
I would recommend doing it in the code behind. On btnDelete click i would iterate through every row in your gridview and check all the datakeynames. Once i found the one you want to delete you will need to send that back to you db. You can use either an orm like linq, ado.net, or a straight sqlcmd.
Please See this Code. I implement and works fine
<asp:TemplateField HeaderText="Add" HeaderStyle-CssClass="grid_Title">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("TrackID")%>' />
<asp:SqlDataSource ID="SqlDataSource_Projects" runat="server" ConnectionString="<%$ ConnectionStrings:SentricMusicFunctionalityConnectionString2 %>"
SelectCommand="select* from MassTraxCatProjects where Fk_AgencyId=#CatalogAgencyId and fk_trackid=#TrackID) ">
<SelectParameters>
<asp:SessionParameter DbType="Int32" DefaultValue="CatalogAgencyId" Name="CatalogAgencyId"
SessionField="CatalogAgencyId" />
<asp:ControlParameter DefaultValue="TrackID" Name="TrackID" ControlID="**HiddenField1**" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddl_ProjectType" runat="server" DataSourceID="SqlDataSource_Projects"
Width="100px" DataTextField="ProjectName" DataValueField="ProjectId">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>