Cannot find SQL variable in SqlDataSource - asp.net

I have the following SqlDataSource on a GridView. All works except for Delete Function.
What am I doing wrong , no error is given but no result either.
How can this be solved?
<asp:SqlDataSource ID="BookingsSQL" runat="server" ConnectionString="<%$ ConnectionStrings:BookingsConnectionString %>"
OldValuesParameterFormatString="original_{0}" ConflictDetection="OverwriteChanges"
SelectCommand="SELECT [ID], [programme name] AS programme_name, [Start], [Finish], [Source], [Destination], [Comment] FROM [Bookings]"
DeleteCommand="DELETE FROM [Bookings] WHERE [programme name] = #ID" InsertCommand="INSERT INTO [Bookings] ([programme name], [Start], [Finish], [Source], [Destination], [Comment]) VALUES (#programme_name, #Start, #Finish, #Source, #Destination, #Comment)"
UpdateCommand="UPDATE [Bookings] SET [programme name] = #programme_name, [Start] = #Start, [Finish] = #Finish, [Source] = #Source, [Destination] = #Destination, [Comment] = #Comment WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="programme_name" Type="String" />
<asp:Parameter Name="Start" Type="DateTime" />
<asp:Parameter Name="Finish" Type="DateTime" />
<asp:Parameter Name="Source" Type="String" />
<asp:Parameter Name="Destination" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="Start" Type="DateTime" />
<asp:Parameter Name="Finish" Type="DateTime" />
<asp:Parameter Name="Source" Type="String" />
<asp:Parameter Name="Destination" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
<asp:Parameter Name="programme_name" Type="String" />
</UpdateParameters>

I think this:
DELETE FROM [Bookings] WHERE [programme name] = #ID
should be this:
DELETE FROM [Bookings] WHERE [ID] = #ID

You delete by [programme name] where from your other queries it looks like you want to delete by the [ID] column.
Since that columns exists, you won't get an error but also no matches to delete. Change the query to:
DELETE FROM Bookings WHERE ID=#ID

Related

SELECT query with querystring parameters using NULL value

Hello to all respected members,
Presently I using following SELECT command in asp.net for populating a Gridview based on Query string values:
SelectCommand="SELECT ID, Date, Train, I_R, Dir_Ind, Detn, Rly, DiV, Loco, Shed, locoClass, loco_type FROM PunctualityMain WHERE Date >= #strdt AND Date <= #enddt AND ([Rly] = #rly OR #rly IS NULL) AND ([shed] = #shed OR #shed IS NULL) Order by Date ASC">
<SelectParameters>
<asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
<asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
<asp:QueryStringParameter Name="rly" QueryStringField="rly" Type="string" />
<asp:QueryStringParameter Name="shed" QueryStringField="shed" Type="string" />
</SelectParameters>
It is working fine also with two values null as :
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=&shed=
Now I want to use same for Gridview with Delete, Insert & Update Commands which is as under:
DeleteCommand="DELETE FROM [PunctualityMain] WHERE [ID] = #ID"
InsertCommand="INSERT INTO [PunctualityMain] ([Date], [Train], [Dir_Ind], [Detn], [Rly], [DiV], [Loco], [Shed], [locoClass], [loco_type]) VALUES (#Date, #Train, #Dir_Ind, #Detn, #Rly, #DiV, #Loco, #Shed, #locoClass, #loco_type)"
SelectCommand="SELECT * FROM [PunctualityMain] WHERE (([Date] >= #strdt) AND ([Date] <= #enddt) AND ([Shed] = #shed OR #shed IS NULL) AND ([Rly] = #rly OR #rly IS NULL)) ORDER BY [Date]"
UpdateCommand="UPDATE [PunctualityMain] SET [Date] = #Date, [Train] = #Train, [Dir_Ind] = #Dir_Ind, [Detn] = #Detn, [Rly] = #Rly, [DiV] = #DiV, [Loco] = #Loco, [Shed] = #Shed, [locoClass] = #locoClass, [loco_type] = #loco_type WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Train" Type="Int32" />
<asp:Parameter Name="Dir_Ind" Type="String" />
<asp:Parameter Name="Detn" Type="Int32" />
<asp:Parameter Name="Rly" Type="String" />
<asp:Parameter Name="DiV" Type="String" />
<asp:Parameter Name="Loco" Type="String" />
<asp:Parameter Name="Shed" Type="String" />
<asp:Parameter Name="locoClass" Type="String" />
<asp:Parameter Name="loco_type" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
<asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
<asp:QueryStringParameter Name="rly" QueryStringField="rly" Type="string" />
<asp:QueryStringParameter Name="shed" QueryStringField="shed" Type="string" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Date" Type="DateTime" />
<asp:Parameter Name="Train" Type="Int32" />
<asp:Parameter Name="Dir_Ind" Type="String" />
<asp:Parameter Name="Detn" Type="Int32" />
<asp:Parameter Name="Rly" Type="String" />
<asp:Parameter Name="DiV" Type="String" />
<asp:Parameter Name="Loco" Type="String" />
<asp:Parameter Name="Shed" Type="String" />
<asp:Parameter Name="locoClass" Type="String" />
<asp:Parameter Name="loco_type" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
Why the above SELECT command not working with NULL values for rly & shed? However, with all the four query string values it is working like this
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=CR&shed=KYN
Please suggest any solution for the problem so that it can work with this string also (NULL string values)
http://elocos.railnet.gov.in/Punctuality/Punctualitydetail.aspx?strdt=5/1/2013&enddt=5/14/2013&rly=&shed=
Because you get Empty value from empty querystring value =/= null
In order to get this working you should set null value manually.
I can suggest you at least 2 ways:
1) In codebehind:
string rly = Request.QueryString["rly"];
if (rly == "")
rly = null;
And then use it.
2) In SQL command:
Rly=NullIf('#rly','')
But I'm not sure if 2nd one will work like this, if it won't then you can try to workaround it like:
codebehind string empt = "";
SQL command Rly=NullIf('#rly','+empt+')
Or you can go some other way, the point is to verify it "rly" query is "", if it is then set it to null, otherwise leave it alone.

ASP.net, SQL database, Insert, TextBoxes

I am currently working in Visual Basic 2010 with a webform. I created an SQL database inside of Visual Basic and I'm trying to make the insert command move values from the textboxes into the database.
So my question is: how do I get the values from the textboxes (i.e:NameTextBox, AddressTextBox..) and associate them with the parameters (#Name, #Address1)?
The database works, I tested it by inputing values through the code.
Here is the relevant code:
<script runat="server">
' InsertShipper
Private Sub InsertShipper (ByVal Source As Object, ByVal e As EventArgs)
SqlDataSource1.Insert()
End Sub
</script>
<asp:Button ID="OrderButton" runat="server" onclick="InsertShipper" Text="Order" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:OrderListConnectionString %>"
DeleteCommand="DELETE FROM [ORDER] WHERE [OID] = #OID"
InsertCommand="INSERT INTO [ORDER] ([Name], [Address1], [Address2], [City], [State], [Zip], [Phone], [Email], [Subtotal], [Shipping]) VALUES ( #Name, #Address1, #Address2, #City, #State, #Zip, #Phone, #Email, #Subtotal, #Shipping)"
SelectCommand="SELECT * FROM [ORDER]"
UpdateCommand="UPDATE [ORDER] SET [Name] = #Name, [Address1] = #Address1, [Address2] = #Address2, [City] = #City, [State] = #State, [Zip] = #Zip, [Phone] = #Phone, [Email] = #Email, [Subtotal] = #Subtotal, [Shipping] = #Shipping WHERE [OID] = #OID">
<DeleteParameters>
<asp:Parameter Name="OID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter name="Name" Type="String" />
<asp:Parameter name="Address1" Type="String" />
<asp:Parameter Name="Address2" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="Zip" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Subtotal" Type="String" />
<asp:Parameter Name="Shipping" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Address1" Type="String" />
<asp:Parameter Name="Address2" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="Zip" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Subtotal" Type="String" />
<asp:Parameter Name="Shipping" Type="String" />
<asp:Parameter Name="OID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Try setting the parameters' ControlID and PropertyName as such:
<asp:ControlParameter Name="Title"
ControlID="txtFoo"
PropertyName="Text"/>
Refer to http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx
Your parameters' Name property are missing the "#" character. You need to add this or it sees it as a completely different parameter.

No value given for one or more required parameters. showing error

<asp:AccessDataSource ID="productsubtype" Runat="server" DataFile="~/access_db/db_tables.mdb"
SelectCommand="SELECT [type], [id],[subtype] FROM [prod_subtype]"
UpdateCommand="UPDATE [prod_subtype] SET [subtype]=#subtype WHERE [id]=#id"
DeleteCommand="DELETE FROM [prod_subtype] WHERE [id]=#id"
FilterExpression="type='{0}'">
<UpdateParameters>
<asp:Parameter Name="subtype" Type="string" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="string" />
</DeleteParameters>
<FilterParameters>
<asp:ControlParameter Name="type" DefaultValue="-1"
Type="String" ControlID="gdproducttype"
PropertyName="SelectedValue"></asp:ControlParameter>
</FilterParameters>
</asp:AccessDataSource>
When i press delete button in gridview i am getting error
Try changing your UpdateParameters to the following:
<UpdateParameters>
<asp:Parameter Name="id" Type="string" />
<asp:Parameter Name="subtype" Type="string" />
</UpdateParameters>

sqldatasource and a Grid delete command error

I am using sqldatasource to binding to the grid.
SqlDatasource is like this:
<asp:SqlDataSource ID="DS_ValidUsers" runat="server"
ConnectionString="<%$ ConnectionStrings:conString %>"
ProviderName="<%$ ConnectionStrings:conString.ProviderName %>"
SelectCommand="SELECT ValidUsers.ID, ValidUsers.DomainID, ValidUsers.isAdmin, ValidUsers.isSubmitToTimberline, ValidUsers.isHistoryDetails, ValidUsers.isSearch, ValidUsers.isAttachments, ValidUsers.isUpload, ValidUsers.isDelete, a.Name AS FullName, a.DepartmentCode FROM ValidUsers INNER JOIN db_common.dbo.DomainUsers_Active AS a ON a.DomainID = ValidUsers.DomainID WHERE (ISNULL(a.DomainID, '') NOT IN ('', 'XX0000'))"
UpdateCommand="UPDATE ValidUsers SET isAdmin = ?, isSubmitToTimberline = ?, isHistoryDetails = ?,isSearch = ?, isAttachments = ?, isUpload = ?, isDelete = ? WHERE (ID = ?)"
DeleteCommand="DELETE FROM ValidUsers WHERE (ID = ?)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter DefaultValue="false" Name="isAdmin" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSubmitToTimberline" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isHistoryDetails" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSearch" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isAttachments" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isUpload" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isDelete" Type="Boolean" />
<asp:Parameter DefaultValue="0" Name="original_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>When I click delete, I am getting following error. I don’t have a clue where this
parameter 1 and 2 are coming from?
Microsoft JScript runtime error:
Sys.WebForms.PageRequestManagerServerErrorException:
Command parameter[1] '' is invalid.
Command parameter[2] '' is invalid.
Thanks!
ConnectionString="<%$ ConnectionStrings:conString %>"
ProviderName="<%$ ConnectionStrings:conString.ProviderName %>"
SelectCommand="SELECT ValidUsers.ID, ValidUsers.DomainID, ValidUsers.isAdmin, ValidUsers.isSubmitToTimberline, ValidUsers.isHistoryDetails, ValidUsers.isSearch, ValidUsers.isAttachments, ValidUsers.isUpload, ValidUsers.isDelete, a.Name AS FullName, a.DepartmentCode FROM ValidUsers INNER JOIN db_common.dbo.DomainUsers_Active AS a ON a.DomainID = ValidUsers.DomainID WHERE (ISNULL(a.DomainID, '') NOT IN ('', 'XX0000'))"
UpdateCommand="UPDATE ValidUsers SET isAdmin = ?, isSubmitToTimberline = ?, isHistoryDetails = ?,isSearch = ?, isAttachments = ?, isUpload = ?, isDelete = ? WHERE (ID = ?)"
DeleteCommand="DELETE FROM ValidUsers WHERE (ID = ?)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter DefaultValue="false" Name="isAdmin" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSubmitToTimberline" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isHistoryDetails" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSearch" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isAttachments" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isUpload" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isDelete" Type="Boolean" />
<asp:Parameter DefaultValue="0" Name="original_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
So is update working properly? ID is the primary Key right?
Try setting DataKeyName="ID" for your GV and remove the DeleteParameters from SqlDataSource.
Edit:
I would suggest you to follow this tutorial to setup your sqldatasource for Update and Delete. It could be some parameters issue. I would also suggest to handle SqlDataSource Update event and check for e.Exception to info.

Add aspnet_users UserName value into my own table

I would like to insert UserName from aspnet_users into my own table called Members. My sqldatasource looks like as follows. At the end in the InsertParameters I have added ProfileParameter but its not working. Just to get the values going, I have hardcoded 'testhere' value in the insertcommand. My SQLDatasource looks as follows.
Please help !!!
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
OnInserted= "on_inserted"
OnUpdated = "on_updated"
ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>"
DeleteCommand="DELETE FROM [member] WHERE [id] = #id"
InsertCommand="INSERT INTO [member] ([firstname], [lastname],[username]) VALUES (#firstname, #lastname,'testhere');
set #id = ##identity"
SelectCommand="SELECT * FROM [member] WHERE ([id] = #id)"
UpdateCommand="UPDATE [member] SET [firstname] = #firstname, [lastname] = #lastname WHERE [id] = #id"
onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="id" Type=Int32 Direction=Output />
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:ProfileParameter Name="username" Type=String />
</InsertParameters>
</asp:SqlDataSource>
Okay.. as a simple one:
Try changing the InsertParameter (username) to be an asp:Parameter.
Add a asp:HiddenField control with ID of username
onpageload, set the value of the username hiddenfield to be Page.User.Identity.Name
Magic!

Resources