How can I use SQL IN Operator with Objectdatasource? - asp.net

I have done IN statement straight to SQL but I was trying to use it the objecdatasource. I have a textbox where users need query multiple item codes like this 1001,1002,1003 but how can I use this IN statement with a parameter with objectdatasource?
TIA

Just pass the textbox value of "1,2,3" to the parameter as a full string, and split the contents by "," within the object your passing the data to.

Related

Passing a Guid through to another page ASP.NET

I have a Guid loaded with a ID that a user selected from a GridView.
I want to pass this Guid to another page, but i dont know how to append it to the statement below:
Response.Redirect("LicenseDetailsView.aspx?idLicense=" guidLicense)
Here is the full code:
Dim dvrLicense As GridViewRow
Dim guidLicense As Guid
dvrLicense = grdAllUserLicense.SelectedRow
guidLicense = StringToGUID(dvrLicense.Cells(1).Text.ToString)
Response.Redirect("LicenseDetailsView.aspx?idLicense=" guidLicense)
the variable i want to pass is called: guidLicense.
Use a format string:
Response.Redirect(String.Format("LicenseDetailsView.aspx?idLicense={0}", guidLicense))
Or perhaps just concatenate it (though the former is preferred):
Response.Redirect("LicenseDetailsView.aspx?idLicense=" & guidLicense)
The point being that there has to be some operation performed on the two values (the string literal and the variable). Putting them next to each other doesn't mean anything to the compiler. A method call with the values as arguments, or an operator between the values, will instruct the code to do something with those values.

column name as parameter

I have some columns like text_en, text_es, text_de in a SQL table. Now I want to retrieve the value from just one column depending on the language.
So I created an sql string
SELECT #textfield FROM <table>
and in the vb code I use
cmd.AddWithValue("textfield", "text_" + lang)
But sql returns the name of the column instead of the value of that column. How can I get the value?
You can also do
SELECT CASE #textfield
WHEN 'text_en' THEN text_en
WHEN 'text_es' THEN text_es
WHEN 'text_de' THEN text_de
END AS local_text
FROM TableName
You can't use variables are column names in SQL, not like this, anyway.
You need to use dynamic SQL in order to specify column names like this.
I suggest reading The Curse and Blessings of Dynamic SQL for a comprehensive treatment of the subject.
Don't pass it as a parameter, pass it as a string literal. Your sql statement should be in the form:
string col1name = 'somename';
string sql = 'SELECT ' + col1name + ' FROM TableName';
But if you have a parameter passed to the WHERE clause you should pass it as a parameter like what you did in your question.
Note that: Your query, this way is vulnerable to SQL Injection. In your case, you can pass your concatenated SQL statement to a stored procedure then use sp_executesql, not EXEC().

Pass a table method to addLink method of a QueryBuildDataSorce object

Is there any way to pass a table method instead of fieldid to addLink method of a QueryBuildDataSource object?
I have this code:
qbdsLedgerTrans.addLink(
FieldNum(LedgerTable, AccountNum), // Here i need to pass a conditional value of a LedgerTable method instead of a field
FieldNum(LedgerTrans, AccountNum)
);
I think no because the query API is tightly bound to tables in SQLServer and tables does not contain any method in SQLServer.
The #addLink will be used to generate query join statement so there must use data present inside the database and method are not in the database but in the aod files.
Depending on what you want to do you can have a method to encapsulate your logic and return the corrected fieldId.
The first argument to the addLink method must be an field of the prior joined table, in this case LedgerTable. It may be returned by a function, but I doubt it will solve your hidden (unstated) problem.
The second argument to the addLink method must be an field of the current datasource table.
In other scenarios (form delayed joins) you could use the addDynalink method instead.

Equivalent of RecordSet.MoveNext while Not EOF in ASP.NET

I'm using a DataReader to display informations stored in a table.
I created Two button to go to next record and to go back.
In VB6 I used this code :
While Not Recordset1.EOF
Recordset1.MoveNext
End While
In ASP.NET I didn't find a way to do like it, because DataReader hasn't the EOF property.
EDIT :
While Not Recordset1.BOF
Recordset1.MovePrevious
End While
How can I convert this last code (VB6) to ASP.NET ??
You use Read instead of MoveNext and it'll return false if there aren't any more records.
So:
While rdr.Read()
.... ' process this row
End While
Azirar, ho1 is correct in that you should use a DataTable. If you're updating after every post back and only need a single record you could still use a DataReader, but set up your SQL statement to get a single row (storing the appropriate information needed in your SQL statement (or better yet stored procedure) to get that single record back within query strings or session variables).

How do I sort an ASP.NET DataGrid by the length of a field?

I have a DataGrid where each column has a SortExpression. I would like the sort expression to be the equivalent of "ORDER BY LEN(myField)".
I have tried
SortExpression="LEN(myField)"
but this throws an exception as it is not valid syntax. Any ideas?
What about returning the len by the query already, but don't show that column, only use it as your original column's sortexpression?
I don't think that your idea is supported by default.
Depending on your SQL flavor the following could work:
SELECT
ColumnA as FieldA
, ColumnB as FieldB
, LEN(ColumnA) as FieldL
FROM TableName
ORDER BY L
And then do
SortExpression="FieldL"
Using Linq, you could write your query like:
query.OrderBy(column => column.MyField.Length);
Hmmm. Had some time to test. I was able to get SortExpression="Description.Length" to work. Is this 1.1, 2.0 or 3.5?
The SortExpression parameter specifies the name of the column to sort, followed by "ASC" or "DESC" to control the order.
You could change the DataType property of the column to specifiy a user defined type whose comparer function compares string lengths. It won't be a trivial task.

Resources