How to use DataReader from Oledb and get results in Richtextbox - visual-studio-2010

Dim srch As String
srch = ccode.Text
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + IO.Directory.GetCurrentDirectory + "\ptdr.accdb"
cmd.Connection = conn
conn.Open()
Dim dsrch As New OleDbCommand("SELECT pname, page, vdate, psex, summer, bldp, blds, photh, diag, rl, trtmnt, nvdate FROM ptnt_data WHERE pcode = " & srch & "", conn)
Dim rdr As OleDbDataReader = dsrch.ExecuteReader()
Dim dg As String = ""
'diagnosis'
dg = rdr.GetString(8).ToString()
If String.IsNullOrEmpty(dg) Then
diag1.Text = ""
ElseIf String.IsNullOrWhiteSpace(dg) Then
diag1.Text = ""
Else
diag1.Text = dg
End If
It works fine but when I search for a null it gives an error.
It says that I've an error at the line of
dg = rdr.GetString(8).Tostring()
any suggests?

rdr.GetString(8).ToString()
if rdr.GetString(8) is null this is a classic null reference exception
rdr.GetString(8) IS a string so just remove the .ToString()
As per my comment below you can only run GetString if the DB has an underlying type of string for this data, what is the datatype of column diag?
(ie run rdr.GetFieldType(8) in the debugger and provide the type name)
As you have confirmed this is a string type and the issue is just with null handling (sorry i didn;t spot that from your question) you need:
If rdr.IsDBNull(8) Then diag1.Text = "" Else diag1.Text = rdr.GetString(8) EndIf
Which should replace everything from Dim dg... down

Related

BC42104 error code how can I fix this error?

Warning BC42104: Variable 'pass' is used before it has been assigned a value. A null reference exception could result at runtime.
This is my code:
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Dim uname As String = ""
Dim pword As String
Dim username As String = ""
Dim pass As String
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Please fill the info")
Else
uname = TextBox1.Text
pword = TextBox2.Text
Dim query As String = "Select Password From Register where Username= '" & uname & "';"
Dim dbsource As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gui\Documents\Database4.accdb"
Dim conn = New OleDbConnection(dbsource)
Dim cmd As New OleDbCommand(query, conn)
conn.Open()
Try
pass = cmd.ExecuteScalar().ToString
Catch ex As Exception
MsgBox("Username does not exit")
End Try
If (pword = pass) Then
MsgBox("Login success")
Reg.Show()
If Reg.Visible Then
Me.Hide()
End If
Else
MsgBox("login Failed")
TextBox1.Clear()
TextBox2.Clear()
End If
End If
End Sub
As the error is saying, you are not initializing the variable pass and under some condition, you may end up with using it.
To be exact, if the control lands in the 'catch' block, the variable 'pass' will not have any values, which means it is possible that If (pword = pass) statement is reached without this variable having any values.
To fix the error, just assign a null value or empty string to the variable at the point of initialization. For example use this statement:
Dim pass As String = "";

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()

Getting the cursor location in Visual Studio to clipboard

Often when describing an issue in code, I need to reference it by line/column/function.
Is there a macro/add-in for Visual Studio that copies that information for me?
It would be perfect if it could copy to clipboard: File, Line, column, function name
But I'd take any combination :).
Thanks!
I ended up doing a macro. Unfortunately I was unable to access the clipboard from the macro so I had to use NirCmd for that part. Other than that, it works great!
Public Sub CopyLocation()
Dim fileName = DTE.ActiveDocument.Name
Dim line = ""
Dim column = ""
Dim functionName = ""
Dim className = ""
Dim textDocument = TryCast(DTE.ActiveDocument.Object, TextDocument)
If textDocument IsNot Nothing Then
Dim activePoint = textDocument.Selection.ActivePoint
column = activePoint.DisplayColumn
line = activePoint.Line
Dim codeElement As CodeElement
codeElement = activePoint.CodeElement(vsCMElement.vsCMElementFunction)
If codeElement IsNot Nothing Then
functionName = codeElement.Name
End If
codeElement = activePoint.CodeElement(vsCMElement.vsCMElementClass)
If codeElement IsNot Nothing Then
className = codeElement.Name
End If
End If
Dim output As String = String.Format("File: {0} ", fileName)
If (String.IsNullOrEmpty(line) = False) Then output = output & String.Format("Line: {0} ", line)
If (String.IsNullOrEmpty(column) = False) Then output = output & String.Format("Column: {0} ", column)
If (String.IsNullOrEmpty(className) = False) Then output = output & String.Format("at {0}", className)
If (String.IsNullOrEmpty(functionName) = False) Then output = output & String.Format(".{0}", functionName)
Dim process As System.Diagnostics.Process
process.Start("c:\NoInstall files\nircmd.exe", String.Format("clipboard set ""{0}""", output))
End Sub

Using ItextSharp I am getting Unbalanced begin/end text operators. But they are Matched

I working on a app that uses ItextSharp to generate PDF files for students to print name tags and parking permits... However it keeps throwing: Unbalanced begin/end text operators. at the doc.close()
The blocks appear to be properly openned and closed.. Below is the function:
Function ID_and_Parking(ByVal id As Integer) As ActionResult
Dim _reg_info As reg_info = db.reg_info.Single(Function(r) r.id = id)
Dim _conf_info As conf_info = db.conf_info.Single(Function(f) f.id = 0)
Dim _LastName As String = _reg_info.last_name
Dim _Employer As String = _reg_info.business_name
Dim _Class_1 As String = _reg_info.tues_class
Dim _Class_2 As String = _reg_info.wed_class
Dim _Class_3 As String = _reg_info.thur_class
Dim _Class_4 As String = _reg_info.fri_class
Dim _BeginDate As String = _conf_info.conf_start_date
Dim _endDate As String = _conf_info.conf_end_date
If IsDBNull(_reg_info.tues_class) Then
_Class_1 = ""
End If
If IsDBNull(_reg_info.wed_class) Then
_Class_2 = ""
End If
If IsDBNull(_reg_info.thur_class) Then
_Class_3 = ""
End If
If IsDBNull(_reg_info.fri_class) Then
_Class_4 = ""
End If
'Dim pdfpath As String = Server.MapPath("PDFs")
'Dim imagepath As String = Server.MapPath("Images")
Dim pdfpath As String = "C:\temp\"
Dim imagepath As String = "C:\temp\"
Dim doc As New Document
doc.SetPageSize(iTextSharp.text.PageSize.A4)
doc.SetMargins(0, 0, 2, 2)
Try
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdfpath + "/Images.pdf", FileMode.Create))
doc.Open()
Dim cb As PdfContentByte = writer.DirectContent
cb.BeginText()
cb.SetTextMatrix(100, 400)
cb.ShowText(_LastName)
cb.EndText()
doc.Add(New Paragraph("JPG"))
Dim jpg As Image = Image.GetInstance(imagepath + "/Asads_Tags.jpg")
jpg.Alignment = iTextSharp.text.Image.UNDERLYING
jpg.ScaleToFit(576, 756)
doc.Add(jpg)
Catch dex As DocumentException
Response.Write(dex.Message)
Catch ioex As IOException
Response.Write(ioex.Message)
Catch ex As Exception
Response.Write(ex.Message)
Finally
doc.Close()
End Try
Return RedirectToAction("Index")
End Function
Anyone know where I could be going wrong at??????
The Try Catch block is what caused the unbalanced start/end exception.....Once I moved the doc.close() up to the bottom of try the error went away... Oh well maybe someone else will need the insight... –

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