Datarowcollection cannot be converted to string In visual basic - visual-studio

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

Related

Deleting a row for the second time in a datagrid view is not working

I have a list of data from a database displayed in a datagrid view. Whenever I delete one record, it successfully deletes it from the table and also in the database but when I try to delete another record, the delete function doesn't work anymore.
Here is my code:
Private Sub cmdDelete_Click()
Set Connect = New Class1
Set rxdelete = New ADODB.Recordset
Dim sqlString, dataID, answer As String
dataID = lblID.Caption
sqlString = "DELETE FROM tblloan WHERE ID = '" & Trim$(dataID) & "'"
answer = MsgBox("Are you sure you want to delete this record?", vbYesNo, "RheaLending")
If answer = vbYes Then
rxdelete.Open sqlString, con, 3, 3
Call refreshList
Else
Call refreshList
End If
End Sub
Here is the code for refreshList:
Sub refreshList()
Set Connect = New Class1
Set rxloan = New ADODB.Recordset
rxloan.Open "SELECT * FROM tblloan LIMIT 100", con, 3, 3
lblLNumberRecords.Caption = Format(rxloan.RecordCount, "###,###,###.##")
Set DatLoans.DataSource = rxloan
DatLoans.SetFocus
End Sub
Please somebody help me!
I am using vb6, adodb and mysql database.
your delete statement does not return a result set other than the number of records deleted. use an adodb.command instead of a .recordset. another thing is you are not killing your objects it can cause you greef in the long run because the app can slow down.
Set rxdelete = New ADODB.command
set rxdelete.currentconneciton = [your connection object]
rxdelete.commandtext = "DELETE FROM tblloan WHERE ID = '" & Trim$(dataID) & "'"
rxdelete.execute
'at the bottom of your sub
set rxdelete=nothing
set connect = nothing

system.rank, system.keywords from SystemIndex

Looking to fetch some nice results working with Microsoft Search.
Got some problems...
First system.rank returns allways 1000.
I tried system.HitCount, it returns allways 65535.
Then I'm trying to match a word in the Keywords using System.Keywords...
The results of the request looks allways empty on Keywords?
strQuery = "SELECT SYSTEM.FILENAME, System.Keywords FROM SYSTEMINDEX WHERE scope= '" & scope & "' AND SYSTEM.FILENAME NOT LIKE '%.tmp' AND SYSTEM.FILENAME NOT LIKE '~$%' "
Dim constring As String = "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
Using con As New OleDbConnection(constring)
con.Open()
Dim da As New OleDbDataAdapter(strQuery, con)
Dim table AS new DataTable()
Dim ds As New DataSet()
Dim dc AS New DataColumn()
Dim cmdSearch As New System.Data.OleDb.OleDbCommand(strQuery, con)
Dim reader As OleDbDataReader = cmdSearch.ExecuteReader()
Dim result As New ArrayList()
table.Columns.Add("filename")
table.Columns.Add("DocKeywords")
If reader.HasRows Then
Do While reader.Read()
Dim row As Datarow = table.NewRow()
row("filename") = reader.Item("system.filename")
row("DocKeywords") = reader.Item("System.Keywords") 'String.Join(",", reader.Item("System.Keywords")) 'system.string[]: empty
table.Rows.Add(row)
Loop
End If
DataGrid1.DataSource = table
DataGrid1.DataBind()

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.

Visual Studio Login Program Issue

I am new to Visual Studio and I'm having a problem with my Login program.
Im attempting to allow a user 3 attempts to login to a system before the program aborts on the 4th attempt.
The problem I am having is even when I enter the correct username/password the program still view the credentials as a bad login attempt.
If I comment out the if/Elseif code I can login without issue..but obviously without the handling attempts which I need.
I am only a couple of days into Visual Studio so simple errors would be completely unknown to me at this stage..so any suggestions or obvious errors pointed out would be most appreciated.
Imports Oracle.DataAccess.Client
Public Class Form1
Public con As New OracleConnection
Public ds As New DataSet
Public Function Connect() As OracleConnection
Dim username As String = txtUsername.Text
Dim password As String = txtPassword.Text
Dim connectString As String = "Data Source=XE; user id=" & username & ";" & "Password=" & password & ";"
Dim con As New OracleConnection(connectString)
con.Open()
Return con
End Function
Public Function populateDS() As DataSet
ds = New DataSet
Dim sql As String
Dim da As OracleDataAdapter
sql = "SELECT * from Customer"
da = New OracleDataAdapter(sql, Connect())
da.Fill(ds, "DT_Customer")
con.Close()
Return ds
End Function
Dim Attempt As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
**If txtUsername.Text = "username" And txtPassword.Text = "Password"** Then
MsgBox("Welcome")
Customer.ShowDialog()
Me.Hide()
populateDS()
ElseIf Attempt = 3 Then
MsgBox("Maxium Attempts Reached")
Close()
Else
MsgBox("Username & Password Incorrect - Attempt " & Attempt & " of 3 ")
Attempt = Attempt + 1
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.Focus()
End If
End Sub
End Class

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