Search in LINQ to SQL - linq

I have some code like this:
Function GetTypeFromTableName(ByVal _TableName As String, ByVal _DataContext As DataContext)
Dim Mytype As Type = (From t In _DataContext.Mapping.GetTables Where t.TableName = "dbo." + _TableName Select t.RowType.Type).SingleOrDefault
Return Mytype
End Function
Dim DBA As New LINQDataContext
_TBLName="City"
TableType = GetTypeFromTableName(_TBLName, DBA)
Dim GridQuery = From T In DBA.GetTable(TableType) Select T
If Chk.Checked Then
CallByName(obj, "CName", CallType.Set, Txt_Name.Text)
GridQuery = From T In DBA.GetTable(TableType) Where T Like obj
End If
This search does not work, and I get this error:
{"Method 'System.Object
LikeObject(System.Object,
System.Object,
Microsoft.VisualBasic.CompareMethod)'
has no supported translation to
SQL."} System.Exception
what should i do for this filter?
vb.net linq

where T.Contains(obj)

Related

How to retrieve an image from an Access Database in Visual Studio?

I'm trying to retrieve an image stored in an Access Database to a Picturebox.
I'm not very fluent with this language, but my current code is as follows:
Private Sub lstUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsers.SelectedIndexChanged
Dim dt As New DataTable
dt = runSQL("Select * from tblUser where UserName = '" & lstUsers.SelectedItem.ToString & "'")
txtForename.Text = dt.Rows(0)(2)
txtSurname.Text = dt.Rows(0)(3)
txtCode.Text = dt.Rows(0)(6)
txtFinish.Text = dt.Rows(0)(7)
Dim data As Byte() = DirectCast(dt.Rows(0)(8), Byte())
Using ms As New MemoryStream(data)
Me.PictureBox1.Image = Image.FromStream(ms)
End Using
End Sub
When I try to run it and I select a username I get the following error message pointing to Image.FromStream(ms):
Parameter is not valid.
Any help would be appreciated.

Argument not specified for parameter 'e' of 'Protected Sub TextBox1(sender As Object, e As System.EventArgs)'

Imports System.Data.OleDb
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub TextBox1(sender As Object, e As System.EventArgs) Handles TextBox1.TextChanged
Dim userid As String = TextBox1.Text
Dim params(1) As OleDbParameter
params(0) = New OleDbParameter("uid", TextBox1.Text)
params(1) = New OleDbParameter("up", TextBox2.Text)
Dim sql = "select * from user where userid=#uid and userpassword=#up"
Dim ds As DataSet = AccessHelper.ExecuteDataSet(sql, params)
If ds.Tables(0).Rows.Count = 1 Then
Session("userid") = TextBox1.Text
Else
Dim paramst(1) As OleDbParameter
paramst(0) = New OleDbParameter("tid", TextBox1.Text)
paramst(1) = New OleDbParameter("tp", TextBox2.Text)
Dim sqlt = "select * from teacher where teacherid=#tid and userpassword=#tp"
Dim dst As DataSet = AccessHelper.ExecuteDataSet(sqlt, paramst)
If dst.Tables(0).Rows.Count = 1 Then
Session("teacherid") = TextBox1.Text
End If
End If
End Sub
End Class
Can't really figure out why I get the errors (http://i.imgur.com/Zb2hBlo.png)
No idea what I'm doing really but it's a check to see if the login exists. Beforehand it was saying that textbox1/2 wasn't declared but now it's switched to this; either way it doesn't work and I don't know what to do.

system.invalidoperationexception: Fill: selectCommand.Connection property has not been initialized

I'm working on connecting the MS Access database in Visual basic. Unfortunately, I cannot link the records from my data base into visual basic.
My database is located at C:\Users\lenovo\Desktop\GUI references\WindowsApplication1\WindowsApplication1\bin\Debug
Database name is smsenabler.mdb
Table to be connected is ProfessorListTable
The table contains fields of
ID | LastName | FirstName | MI | Department | Year Employed
My codes on my form are shown below:
Imports System.Data.OleDb
Public Class ProfessorList
Dim con As OleDbConnection
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub ProfessorList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim con As OleDbConnection = New OleDbConnection
con.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ..\smsenabler.mdb"
con.Open()
showRecords()
con.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Public Sub showRecords()
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter("Select * from ProfessorListTable", con)
da.Fill(dt)
Dim myRow As DataRow
For Each myRow In dt.Rows
ListView1.Items.Add(myRow.Item(0))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(1))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(2))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(3))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(4))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(5))
Next
End Sub
ERROR MESSAGE
system.invalidoperationexception: Fill: selectCommand.Connection property has not been initialized. At System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter adapter,IDbCommand command String method) at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables,Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) at THESIS_GUI.ProfessorList.ProfessorList_Load(Object sender, EventArgs e) in C:\Users\lenovo\Desktop\GUI references\WindowsApplication1\WindowsApplication1\ProfessorList.vb:line17
LINE 17
showRecords()
It gives me an output with no records at all.It only shows a GUI with a field written on it. Thank you ..
One suggestion is to make sure your connection ('con') is available for all subroutines. I have used global variables before to prevent the need for opening and closing the connection (which would lead to problems in the old days). We both do a similar approach, but here is some code I have stripped bare (removed error traps, debug aids, etc.):
Global cnLocalData As ADODB.Connection
Public Sub Get_Connection()
Set cnLocalData = New ADODB.Connection
With cnLocalData
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Properties("Data Source") = "C:\data\SomeDB.mdb"
.Open
End With
End Sub
you are disconnected to the server, you must open you connection in the showRecords method.
This error error appear when you want to execute a command with the closed connection.

Dynamic Linq Library to generate particular type object for selected properties

Im using Dynamic LINQ Library for my Application, In the sample of Dynamic LINQ Library,
We can pass string of Comma separated columns name or property names to select clause of LINQ
like below
.Select("new (AccountingDocumentNbr,DocumentFiscalYearNbr)");
Can we pass some object to instantiate and populate property values into the object like below
.Select("new AccountingObject(AccountingDocumentNbr,DocumentFiscalYearNbr)");
AccountingObject will have AccountingDocumentNbr,DocumentFiscalYearNbr. Is it possible to do it with Dynamic LINQ Library .
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Need your inputs on this..
Well, in theory your code should looks something like this:
Function ParseNew() As Expression
NextToken()
ValidateToken(TokenId.OpenParen, Res.OpenParenExpected)
NextToken()
Dim properties As New List(Of DynamicProperty)()
Dim expressions As New List(Of Expression)()
Do
Dim exprPos = tokenVal.pos
Dim expr = ParseExpression()
Dim propName As String
If TokenIdentifierIs("as") Then
NextToken()
propName = GetIdentifier()
NextToken()
Else
Dim [me] As MemberExpression = TryCast(expr, MemberExpression)
If [me] Is Nothing Then Throw ParseError(exprPos, Res.MissingAsClause)
propName = [me].Member.Name
End If
expressions.Add(expr)
properties.Add(New DynamicProperty(propName, expr.Type))
If tokenVal.id <> TokenId.Comma Then Exit Do
NextToken()
Loop
ValidateToken(TokenId.CloseParen, Res.CloseParenOrCommaExpected)
NextToken()
Dim type As Type = If(newResultType, DynamicExpression.CreateClass(properties))
Dim bindings(properties.Count - 1) As MemberBinding
For i As Integer = 0 To bindings.Length - 1
bindings(i) = Expression.Bind(type.GetProperty(properties(i).Name), expressions(i))
Next
Return Expression.MemberInit(Expression.[New](type), bindings)
End Function
But how are you calling the Select method? It should looks more or less like this:
.Select<ObjectHolder>("new (Activity as Activity, ActivityName as ActivityName)")

Entity Framework problem with Anonymous type return and manipulation

so I am working with entity framework, I had written a function before with ado.net in it to fetch two values and return a datatable later to which I did some manipulation.
code is below
Protected Sub ddlFieldMappingProfile_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlFieldMappingProfile.SelectedIndexChanged
If ddlFieldMappingProfile.SelectedValue >= 0 Then
btnImportData.Enabled = True
End If
If ddlFieldMappingProfile.SelectedValue <= 0 Then Return
Dim lstHeaders As List(Of String)
Dim dtMapping As New DataTable()
Dim intCount As Integer = 0
Try
Using com As New Common
With com
dtMapping = .getProfileFields(Convert.ToInt32(ddlFieldMappingProfile.SelectedValue))
IEMapping = .getProfileFields(Convert.ToInt32(ddlFieldMappingProfile.SelectedValue))
lstHeaders = .FileHeaders(FileUpl.FiletoRead, FileUpl.Delimiter)
End With
End Using
dtMapping.Columns.Add("MatchIndex")
For Each row As DataRow In dtMapping.Rows
For intCount = 0 To lstHeaders.Count() - 1
If lstHeaders(intCount) = row.Item("Dump_FieldName") Then
row.Item("MatchIndex") = intCount
Exit For
Else
row.Item("MatchIndex") = -1
End If
Next
Next
gdvFieldData.DataSource = dtMapping
gdvFieldData.DataBind()
gdvFieldData.Visible = True
Catch ex As Exception
Throw ex
Finally
dtMapping.Dispose()
lstHeaders = Nothing
End Try
End Sub
But now that I am using entity frame work I have written the LINQ query like
Dim Context As New ICOM_Model.IcomsEntities()
Dim query = From F In Context.Incentive_Prepaid_FieldMapping Where F.Profile_ID = Profile_ID
Select New With {.Activations_FieldName = F.Activations_FieldName, .Dump_FieldName = F.Dump_FieldName}
An anonymous type.
Please help me what is going to be the return type and how am I going to manipulate in the code for ddlFieldMappingProfile_SelectedIndexChanged
In your code return type will be anonymous with two property "Activations_FieldName and Dump_FieldName" and you can access that values by "query" object.
you can make a class with properties which you are selecting by entity and fill that class object.
class ABC{
string Activations_FieldName{get;set;}
string Dump_FieldName{get;set;}
}
Dim Context As New ICOM_Model.IcomsEntities()
List<ABC> query = From F In Context.Incentive_Prepaid_FieldMapping Where F.Profile_ID = Profile_ID
Select New ABC With {.Activations_FieldName = F.Activations_FieldName, .Dump_FieldName = F.Dump_FieldName}
Now you can use this class object in your code. hope this help.

Resources