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
Related
i want to select a column like cl1 from different tables for each row on gridview and show it on my repeater. how can i do this?
here is my code:
<asp:gridview cssclass="text-center table table-bordered table-condensed" id="GridView1" runat="server" autogeneratecolumns="False" datakeynames="PID" datasourceid="SqlDataSource2" emptydatatext="There are no data records to display.">
<Columns>
<asp:TemplateField HeaderText="مشخصات اختصاصی">
<ItemTemplate>
<asp:Label ID="CatPageLabel" runat="server" Text='<%#Eval("catpage").ToString().Replace(".aspx","") %>'></asp:Label>
<asp:Repeater ID="SecondPageRepeater" runat="server" DataSourceID="SecondPageSqlDataSource">
<ItemTemplate>
<%#Eval("cl1") %>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource runat="server" ID="SecondPageSqlDataSource" ConnectionString='<%$ ConnectionStrings:DrB_DBConnectionString %>' SelectCommand='select cl1 from #dest where cl1=3'>
<SelectParameters>
<asp:ControlParameter Name="dest" ControlID="CatPageLabel"/>
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
and this is the error:
Invalid object name '#dest'.
thank you
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 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>
I want here that user can see the answer under the question by selecting its heading. The question should be a hyperlink to redirect me on the page Answer.aspx. I mean to say that when user take cursor over the How to do this? it should redirect the user to the desired page. How can I do that?
here is the code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Width="100%" BorderStyle="None">
<Columns>
<asp:BoundField DataField="QuestionHEAD" HeaderText="Question"
SortExpression="QuestionHEAD" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown"/>
<asp:BoundField DataField="Problem" HeaderText="Problem"
SortExpression="Problem" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown" />
<asp:BoundField DataField="Forum" HeaderText="Forum" SortExpression="Forum" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown"/>
<asp:BoundField DataField="Username" HeaderText="Asked By"
SortExpression="Username" HeaderStyle-ForeColor="white" HeaderStyle-BackColor="Brown" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connectionstring %>"
SelectCommand="SELECT [QuestionHEAD], [Problem], [Forum], [Username] FROM [Question]">
</asp:SqlDataSource>
You can use a HyperLinkField for this:
<asp:HyperLinkField DataNavigateUrlFormatString="/somepage.aspx?a={0}&b={1}" DataNavigateUrlFields="Column1, Column2" />
Try something like this
Eval("id") should be the question id and Eval("Question") should be the question field in your database
<asp:TemplateField HeaderText="Question" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Eval("Question")%>
</ItemTemplate>
</asp:TemplateField>