How to write select SQL to use like when using control parameter? - asp.net

How to change to use like instead of = ?
([Card_no] = #Search)
Moreover, if i want to show all in default, this method force to user to type
something first. How to show all in default when using control parameter?
SelectCommand="SELECT [Serial_no], [Transaction_Date], [Card_no], [Company],[IsCredit], [Fee] FROM [CarPark] WHERE ([Card_no] = #Search) or ([Company] = #Search)"
UpdateCommand="UPDATE [CarPark] SET
Card_no=#Card_no,
Company=#Company,
IsCredit=#IsCredit,
Fee=#Fee
WHERE Serial_no=#Serial_no">
<UpdateParameters>
<asp:Parameter Name="Card_no" Type="String" />
<asp:Parameter Name="Company" Type="String" />
<asp:Parameter Name="IsCredit" Type="Boolean" />
<asp:Parameter Name="Fee" Type="Double" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="Search_TextBox" DefaultValue="" Name="Search"
PropertyName="Text" Type="String" />
</SelectParameters>

Try this:
Card_no like '%{0}%'
Or if you must use named parameters:
Card_no like '%'+#search +'%'

Related

How to configure SqlDataSource to handle a return value from Stored Procedure

I have a listview and in the listview Update commmand I am attempting to set the parameters defined in my SqlDataSource UpdateParameters property. When Update command fires, I recieve an error message stating
Procedure or function 'MyStoredProcedure' expects parameter '#ReturnResult', which was not supplied.
Here's the aspx with the SQL DataSource:
<asp:SqlDataSource ID="sqlDataForCustomers" runat="server"
ConnectionString="<%$ ConnectionStrings:GS_ConnectionString %>"
CancelSelectOnNullParameter="False"
SelectCommand="usp_WDE_Get_DG"
SelectCommandType="StoredProcedure"
UpdateCommand="usp_WDE_Save"
UpdateCommandType="StoredProcedure" >
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="GID" Type="Int32" />
<asp:Parameter DefaultValue="0" Name="Order" Type="Int32" />
<asp:Parameter DefaultValue="0" Name="PID" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="PID" Type="Int32" />
<asp:Parameter Name="ProID" Type="Int32" />
<asp:Parameter Name="GID" Type="Int32" />
<asp:Parameter Name="DGapID" Type="Int32" />
<asp:Parameter Name="DEntryID" Type="Int32" />
<asp:Parameter Name="DEntrySetID" Type="Int32" />
<asp:Parameter Name="DEntryResult" Type="String" Size="250"/>
<asp:Parameter Name="DEServiceDate" Type="DateTime" />
<asp:Parameter Name="UserLoginID" Type="String" Size="50"/>
<asp:Parameter Name="UserPersonID" Type="Int32" />
<asp:Parameter Name="ServiceDate" Type="DateTime" />
<asp:Parameter Name="DGRowId" Type="Int32" />
<asp:Parameter Name="SRowId" Type="Int32" />
<asp:Parameter Direction="ReturnValue" Name="RetResult" Type="Boolean" DefaultValue="True" />
<asp:Parameter Direction="ReturnValue" Name="RetMsg" Type="String" Size="50" DefaultValue=""/>
</UpdateParameters>
Here is the ItemUpdating event of the LV in the code behind:
Sub CustomersGroupedByDataField_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs) Handles CustomersGroupedByDataField.ItemUpdating
Dim rowValue As Integer = e.ItemIndex
Dim DataGapTypeBeingUpdated As String = CustomersGroupedByDataField.DataKeys.Item(rowValue).Value.ToString()
Dim boolTest As Boolean
boolTest = True
GetRowValues(DataGapTypeBeingUpdated)
CheckOOS()
sqlDataForCustomers.UpdateParameters("PID").DefaultValue = intPID.Value
sqlDataForCustomers.UpdateParameters("ProID").DefaultValue = intProID.Value
sqlDataForCustomers.UpdateParameters("GID").DefaultValue = intGID.Value
sqlDataForCustomers.UpdateParameters("DGapID").DefaultValue = intDGapID.Value
sqlDataForCustomers.UpdateParameters("DEntryID").DefaultValue = intDEntryID.Value
sqlDataForCustomers.UpdateParameters("DEntrySetID").DefaultValue = intDEntrySetID.Value
sqlDataForCustomers.UpdateParameters("DEntryResult").DefaultValue = strDEntryResultValue
sqlDataForCustomers.UpdateParameters("DEServiceDate").DefaultValue = CDate(dtServiceDate.Text)
sqlDataForCustomers.UpdateParameters("UserLoginID").DefaultValue = Convert.ToString(Session("LoginID"))
sqlDataForCustomers.UpdateParameters("UserPersonID").DefaultValue = Convert.ToString(Session("PersonID"))
sqlDataForCustomers.UpdateParameters("ServiceDate").DefaultValue = CDate(dtServiceDate.Text)
sqlDataForCustomers.UpdateParameters("DGRowID").DefaultValue = intDGapRowID.Value
sqlDataForCustomers.UpdateParameters("SRowID").DefaultValue = intSRowID.Value
End Sub
I'm at a loss as to how I should specify the return value as a parameter?
How to handle the Return value (RetResult & RetMsg)?
Thanks for looking,
There is a direction attribute of select parameters that marks an input parameter as a reference parameter. The same property is accessible in your code.
<SelectParameters>
<asp:Parameter Direction="ReturnValue" Name="MyReturnParameter" Type="Int32" />
</SelectParameters>
After the command executes you can retrieve the value using parameter.Value or bind it to a ui element.
I think you can find your solution here,
https://forums.asp.net/t/1049910.aspx?How+to+get+return+value+or+output+value+from+stored+procedure+with+sqldatasource+control+
In order to get the return value you need to declare a return paramater in sqldatasource... something like this
<InsertParameters>
Your Parameters
<asp:Parameter Direction="ReturnValue" Type="Int16" Name="ReturnValue" />
</InsertParameters>
Then you need to override sqldatasource's Inserted event like this ( here you can get the return value)
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
Response.Write(e.Command.Paramaters["ReturnValue"].Value);
}

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.

Update Gridview Error - "oldValues is empty"

So I'm trying to update my GridView, and I get this error:
You have specified that your update command compares all values on
SqlDataSource 'SqlDataSource1', but the dictionary passed in for
oldValues is empty. Pass in a valid dictionary for update or change
your mode to OverwriteChanges.
Here is the SQL Data Source in question:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT field1, field2, field3, field4, field5, field6
FROM table
ORDER BY field4"
UpdateCommand="UPDATE table2
SET field2 = #field2 , field3 = #field3, field4 = #field4
WHERE (field1 = #original_field1) AND (field6 = #original_field6)"
FilterExpression="field1 LIKE '{0}'">
<FilterParameters>
<asp:ControlParameter ControlID="SearchBox" Name="field1" Type="String" DefaultValue="" />
</FilterParameters>
<SelectParameters>
<asp:ControlParameter ControlID="SearchBox" Name="field1" Type="String" DefaultValue="" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="field2" Type="String" />
<asp:Parameter Name="field3" Type="String" />
<asp:Parameter Name="field4" Type="Int32" />
<asp:Parameter Name="original_field1" Type="String" />
<asp:Parameter Name="original_field5" Type="Int32" />
<asp:Parameter Name="original_field2" Type="String" />
<asp:Parameter Name="original_field3" Type="String" />
<asp:Parameter Name="original_field4" Type="Int32" />
<asp:Parameter Name="original_field6" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Now I understand one way to get around this is to change 'CompareAllValues' to 'OverwriteChanges', however I have never needed to do this before (I have other GridViews on the page that update just fine) and it never seems to update the data when I DO try anyway.
Is it the nature of the data that would cause this Data Source to act differently to the rest? Or have I done something stupid?
Thanks! :)
I discovered the answer! The data field 'field6' was not included in my Gridview as a field.
Once I added it as a column in the GridView I was able to make it work :)
(Can also add it as a DataKeyName)

Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query

I am creating a website now using Microsoft Visual Web Developer 2010 Express and I used the toolbox of "createUserWizard". Then I put the userId for my "CustomerInfo" table to the "data type=uniqueidentifier" because I need to link it to the user name in the aspnet_ table.
Later on, I need to link my "Order" table to the "CustomerInfo" table so I put my orderId data type=uniqueidentifier. Then, I plan insert my order details to the "Order" table but I have the problem of:
"Implicit conversion from data type
sql_variant to uniqueidentifier is not
allowed. Use the CONVERT function to
run this query".
I search and find some answer like datatype of the parameter set to "Empty"or delete it. but then I have this error
" Conversion failed when converting from character string to uniqueidentifier."
This is my SQL
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Order] WHERE [orderId] = #orderId"
InsertCommand="INSERT INTO [Order] ([orderId], [userId], [Services], [PickUpDate], [PickUpTime], [SpecialDate], [SpecialTime]) VALUES (#orderId, #userId, #Services, #PickUpDate, #PickUpTime, #SpecialDate, #SpecialTime)"
SelectCommand="SELECT * FROM [Order]"
UpdateCommand="UPDATE [Order] SET [userId] = #userId, [Services] = #Services, [PickUpDate] = #PickUpDate, [PickUpTime] = #PickUpTime, [SpecialDate] = #SpecialDate, [SpecialTime] = #SpecialTime WHERE [orderId] = #orderId">
<DeleteParameters>
<asp:Parameter Name="orderId" Type="Object" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="orderId" Type="Object" />
<asp:Parameter Name="userId" Type="Object" />
<asp:Parameter Name="Services" Type="String" />
<asp:Parameter Name="PickUpDate" Type="String" />
<asp:Parameter Name="PickUpTime" Type="String" />
<asp:Parameter Name="SpecialDate" Type="String" />
<asp:Parameter Name="SpecialTime" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="userId" Type="Object" />
<asp:Parameter Name="Services" Type="String" />
<asp:Parameter Name="PickUpDate" Type="String" />
<asp:Parameter Name="PickUpTime" Type="String" />
<asp:Parameter Name="SpecialDate" Type="String" />
<asp:Parameter Name="SpecialTime" Type="String" />
<asp:Parameter Name="orderId" Type="Object" />
</UpdateParameters>
</asp:SqlDataSource>
Now that I think of it, maybe there is a problem with my code in vb.
I will put it here, please tell me how to do the correct way of inserting data to the database.
Protected Sub OrderButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderButton.Click
SqlDataSource1.InsertParameters(0).DefaultValue = Now
SqlDataSource1.Insert()
Response.Redirect("~/Customer/AfterOrder.aspx")
End Sub
If you want to insert GUID from your code, you should make parameter type GUID
<asp:Parameter Name="ParamName" DbType="Guid" />
And you should pass GUID value as (c#):
SqlDataSource1.InsertParameters["ParamName"].DefaultValue = System.Guid.NewGuid().ToString();
If you want to pass a string value, you have to use CONVERT(UNIQUEIDENTIFIER,'YOUR VALUE') in the insert query like this:
InsertCommand="INSERT INTO [Order] ([UserId], [etc])
VALUES (CONVERT(UNIQUEIDENTIFIER,#UserId), #etc)"

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>

Resources