How to retrieve an image from an Access Database in Visual Studio? - 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.

Related

Datarowcollection cannot be converted to string In visual basic

I am working on a system right now in which When I put some value in my first text box (For eg. A primary key), I want the other textboxes to fetch the related data from my database and automatically get filled with it. I'm working on visual basic. I am new to this. For some Reason, it keeps on telling me that datarowcollection cannot be converted to string. Please help me get his working.
Here is the code:-
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim Cn As New SqlClient.SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\admin\Documents\Asset_Manager.mdf';Integrated Security=True;Connect Timeout=30")
Dim StrCmd As String = "Select * from Asset_Master where Asset_Id= '" & txt_allocate_asset_id.Text & "' "
Dim Command As SqlCommand
Dim da As SqlDataAdapter
Dim dt As New DataTable
Try
Cn.Open()
Command = New SqlCommand(StrCmd, Cn)
da = New SqlDataAdapter(Command)
Command.ExecuteNonQuery()
da.Fill(dt)
txt_demo_model_no.Text = dt.Rows[0][model_no].ToString()//The error line
Catch ex As Exception
Throw ex
End Try
End Sub

How to display selected features in ArcGIS Identify Dialog using Visual Studio 2010/ArcObjects?

I'm brand new to ArcObjects SDKs and am struggling. I have a Python Add-in performing a query to select records (works great)and now trying to call the identify dialog via an .NET addin button that displays the identify dialog box to show attributes of the selected records. Below is the code I have at this point. I currently have the identify dialog displaying, but no records appearing. I know I need to input the selected records somewhere....but not sure where. Any thoughts would be appreciated. (I'm using Visual Studio/Microsoft Visual Basic 2010 and ArcGIS 10.2.1)
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Public Class Identify_Button
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Dim pMxDoc As IMxDocument
Dim activeView As IMap
Public Sub DoIdentify(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal x As System.Int32, ByVal y As System.Int32)
pMxDoc = My.ArcMap.Application.Document
activeView = pMxDoc.FocusMap
If activeView Is Nothing Then
Return
End If
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
Dim identifyDialog As ESRI.ArcGIS.CartoUI.IIdentifyDialog = New ESRI.ArcGIS.CartoUI.IdentifyDialogClass
identifyDialog.Map = map
'Clear the dialog on each mouse click
identifyDialog.ClearLayers()
Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay
Dim display As ESRI.ArcGIS.Display.IDisplay = screenDisplay ' Implicit Cast
identifyDialog.Display = display
Dim identifyDialogProps As ESRI.ArcGIS.CartoUI.IIdentifyDialogProps = CType(identifyDialog, ESRI.ArcGIS.CartoUI.IIdentifyDialogProps) ' Explicit Cast
Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = identifyDialogProps.Layers
enumLayer.Reset()
Dim layer As ESRI.ArcGIS.Carto.ILayer = enumLayer.Next
Do While Not (layer Is Nothing)
identifyDialog.AddLayerIdentifyPoint(layer, x, y)
layer = enumLayer.Next()
Loop
identifyDialog.Show()
End Sub
Public Sub New()
End Sub
Protected Overrides Sub OnClick()
DoIdentify(activeView, 300, 100)
End Sub
Protected Overrides Sub OnUpdate()
Enabled = My.ArcMap.Application IsNot Nothing
End Sub
End Class
Give the code below a try. This is executed from the OnClick event from an ArcMap command button that Inherits from BaseCommand. It displays the selected features in the map in the identifyDialog just as you were needing it to except I used AddLayerIdentifyOID() instead of AddLayerIdentifyPoint() although both should work.
Public Overrides Sub OnClick()
'VBTest.OnClick implementation
Try
Dim mxDoc As IMxDocument = m_application.Document
Dim map As ESRI.ArcGIS.Carto.IMap = mxDoc.FocusMap
Dim layer As ESRI.ArcGIS.Carto.ILayer
Dim flayer As ESRI.ArcGIS.Carto.IFeatureLayer
Dim featSelection As ESRI.ArcGIS.Carto.MapSelection = map.FeatureSelection
Dim feat As ESRI.ArcGIS.Geodatabase.IFeature = featSelection.Next()
Dim count As Int16 = 0
While feat IsNot Nothing
count += 1 ' flag for clearing layers
flayer = New ESRI.ArcGIS.Carto.FeatureLayer()
flayer.FeatureClass = feat.Class
layer = flayer
DoIdentify(layer, feat.OID, (count = 1))
feat = featSelection.Next()
End While
Catch ex As Exception
' handle error
MsgBox("Error: " + ex.Message)
End Try
End Sub
Private Sub DoIdentify(ByVal layer As ESRI.ArcGIS.Carto.ILayer, ByVal OID As Int32, Optional ByVal clearLayers As Boolean = True)
If layer Is Nothing Or OID <= 0 Then
Return
End If
Dim pMxDoc As IMxDocument = m_application.Document
Dim activeView As ESRI.ArcGIS.Carto.IActiveView = pMxDoc.ActiveView
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
Dim identifyDialog As ESRI.ArcGIS.CartoUI.IIdentifyDialog = New ESRI.ArcGIS.CartoUI.IdentifyDialogClass()
Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay
Dim display As ESRI.ArcGIS.Display.IDisplay = screenDisplay
identifyDialog.Map = map ' REQUIRED
identifyDialog.Display = display ' REQUIRED
' Clear the dialog
If clearLayers Then
identifyDialog.ClearLayers()
End If
' Add our selected feature to the dialog
identifyDialog.AddLayerIdentifyOID(layer, OID)
' Show the dialog
identifyDialog.Show()
End Sub

Operation is not valid, because of the current state of the object

I have an Infopath 2010 form with a button and the following event code:
Public Sub txtGetCC_Changing(ByVal sender As Object, ByVal e As XmlChangingEventArgs)
' Ensure that the constraint you are enforcing is compatible
' with the default value you set for this XML node.
Dim myNav As XPathNavigator = Me.MainDataSource.CreateNavigator
If myNav.SelectSingleNode("//my:txtGetCC", Me.NamespaceManager).ToString.Length > 0 Then
Dim myIterator As XPathNodeIterator = myNav.Select("/my:myFields/my:group11/my:group12", Me.NamespaceManager)
Dim CCnummer As String = ""
Dim CCsource As DataSource = Me.DataSources("CostCenters")
Dim email As String = ""
While myIterator.MoveNext
CCnummer = myIterator.Current.SelectSingleNode("my:cbRTcc", Me.NamespaceManager).Value
Dim myNavigator As XPathNavigator = Me.DataSources("CostCenters").CreateNavigator
Dim rows As XPathNodeIterator = myNavigator.Select("/dfs:myFields/dfs:dataFields/d:CostCenters", Me.NamespaceManager)
email = ""
While (rows.MoveNext)
Dim ccnumber As String = rows.Current.SelectSingleNode("#CCnumber", Me.NamespaceManager).Value
If ccnumber = CCnummer Then
email = rows.Current.SelectSingleNode("#Email", Me.NamespaceManager).Value
Exit While
End If
End While
myIterator.Current.SelectSingleNode("//my:txtCCowner", Me.NamespaceManager).SetValue(email)
End While
End If
End Sub
The line just before the last END WHILE is not working.
I want to update the column txtCCowner in the repeating table to be updated with the email address found in the external datasource.
Everything works except this line.
The error message I get is: Operation is not valid, because of the current state of the object.
Why do I get this error, what am I doing wrong?
Any help is greatly appreciated.
rg.
Eric

filter Access 2010 database using Visual Studio 2010

I have created the following code and want to filter my database table by landlord_ID. No errors have been shown and when I debug it all seems to go well (all the landlord_IDs come up with all the information e.g how many properties).
However, when I double-click to select a certain ID nothing happens. If I add to part of the code like this :
SQLString = "SELECT = FROM Flats WHERE landlord_ID = 1" '& landlord_ID & ""
Then number 1 comes up but so do all the others (the rest should be filtered out).
Also, I have used this YouTube link to help me: http://www.youtube.com/watch?v=4H2g8H0bqEg
Finally, This is my first time using Visual Studio (2010 Ultimate and the Access database was made on Access 2010) so I do not know much so would appreciate answers that are a little more specific.
Thank you for reading all of this and I hope you can help me
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
OleDbDataAdapter2.Fill(DataSet11)
End Sub
Private Sub lstLID_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstLID.SelectedIndexChanged
Dim landlord_ID, SQLString As String
Dim dtFlats As New DataTable()
Dim dbDataAdapter As OleDbDataAdapter
Dim ConnectString As String = "Provider= Microsoft.ACE.OLEDB.12.0;" & "Data Source = Database.accdb"
landlord_ID = lstLID.Text
SQLString = "SELECT = FROM Flats WHERE landlord_ID = " '& landlord_ID & ""
dbDataAdapter = New OleDbDataAdapter(SQLString, ConnectString)
dbDataAdapter.Fill(dtFlats)
grdFlats.DataSource = dtFlats
End Sub
End Class
I see two problems with
SQLString = "SELECT = FROM Flats WHERE landlord_ID = " '& landlord_ID & ""
SELECT = FROM should probably be SELECT * FROM
WHERE landlord_ID = " '& landlord_ID & "" if the apostrophe ' is a comment character in VB.NET then everything after it will be ignored.

VB dataset issue

The idea was to create a message box that stores my user name, message, and post datetime into the database as messages are sent.
Soon came to realize, what if the user changed his name?
So I decided to use the user id (icn) to identify the message poster instead. However, my chunk of codes keep giving me the same error. Says that there are no rows in the dataset ds2.
I've tried my Query on my SQL and it works perfectly so I really really need help to spot the error in my chunk of codes here.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim name As String
Dim icn As String
Dim message As String
Dim time As String
Dim tags As String = ""
Dim strConn As System.Configuration.ConnectionStringSettings
strConn = ConfigurationManager.ConnectionStrings("ufadb")
Dim conn As SqlConnection = New SqlConnection(strConn.ToString())
Dim cmd As New SqlCommand("Select * From Message", conn)
Dim daMessages As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As New DataSet
cmd.Connection.Open()
daMessages.Fill(ds, "Messages")
cmd.Connection.Close()
If ds.Tables("Messages").Rows.Count > 0 Then
Dim n As Integer = ds.Tables("Messages").Rows.Count
Dim i As Integer
For i = 0 To n - 1
icn = ds.Tables("Messages").Rows(i).Item("icn")
Dim cmd2 As New SqlCommand("SELECT name FROM Member inner join Message ON Member.icn = Message.icn WHERE message.icn = #icn", conn)
cmd2.Parameters.AddWithValue("#icn", icn)
Dim daName As SqlDataAdapter = New SqlDataAdapter(cmd2)
Dim ds2 As New DataSet
cmd2.Connection.Open()
daName.Fill(ds2, "PosterName")
cmd2.Connection.Close()
name = ds2.Tables("PosterName").Rows(0).Item("name")
message = ds.Tables("Messages").Rows(i).Item("message")
time = ds.Tables("Messages").Rows(i).Item("timePosted")
tags = time + vbCrLf + name + ": " + vbCrLf + message + vbCrLf + tags
Next
txtBoard.Text = tags
Else
txtBoard.Text = "nothing to display"
End If
End Sub
Would it be more efficient to combine both cmd and cmd2, such that cmd becomes
SELECT msg.*,mem.Name FROM Message msg INNER JOIN Member mem ON msg.icn = mem.icn ?
This way, your Member.name would be in the same dataset as your Messages table, making your code much cleaner.
-Joel

Resources