Object Reference not set to an instance of an object - vb.net

I have a form which has a search feature - a single text field and command button; when the text field is filled-in a database query is executed and the result (if one result returned) is shown on the form via dynamic control fields.
When the search feature is used for the first time, the fields are created and the data is returned from the database, however when the search feature is re-ran I am getting the error "Object Reference not set to an instance of an object", the error occurs at:
initSearch(txtSearchInput.Text)
I am guessing that I am not handling the textfield properly for this type of use, can anyone please advise how else I should be doing this?
The txtSearchInput is not a dynamic field, it has been created through the design mode, the same for the command button. The above code is located in the command button On Click event:
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Try
initSearch(txtSearchInput.Text)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Encountered")
End Try
End Sub
Any help would be greatly appreciated.
Thanks,
Matt

Any help would be greatly appreciated.
The error is not in the code you posted. The Text property of a TextBox, and the reference to a Form Textbox don't become null all of a sudden.
You probably have to debug into initSearch

Did you test in the debugger if txtSearchInput is null?
Exception could be bubbling up from initSearch function, best way is to debug your code.

Related

VS Studio DataGridView error

I am attempting to handle a local DB for a program I am doing, but I ran into this error that I can't get my head around! Whenever I attempt to edit a row in the gridview I get this error..
Screenshots:
https://i.gyazo.com/cc48a33863fada3e70e445620e6e9b2e.mp4
https://gyazo.com/5b184ea8aead4407e1bd2f0dd081b38c
You row has multiple column. Free column is a not null column.
When you add row, initialise the value of checkbox column.
dataGridView1.Rows[2].Cells[3].Value = true;
You need to add one event handler to your data grid and handle the error. Example
add the below code in your form loading event
AddHandler DataGridView1.DataError, AddressOf DataErrorHandler
Replace DataGridView1 with your data grid name and then create the below function and handle your error.
Private Sub DataErrorHandler(ByVal sender As Object, ByVal e As DataGridViewDataErrorEventArgs)
'handle the error or do nothing.
End Sub

Crystal Report Windows Forms Viewer : Object Reference not set to an instance on an object

I am working with a Crystal Reports in Vb.Net Forms application. A huge report that consists of 10-12 Sub-Reports,there are many formulas used in this report, sometimes this report shows "Object Reference not set to an instance on an object" I can't figure out, I have handled exceptions as well. When I click to "Next Page" button it shows below dialog.
I can't figure out why its happening.
read many blogs, some says, its formula error, some says, "Mouse Move" error.
Below is the code I have added, i will check now
Private Sub CrystalReportViewer1_HandleException(ByVal source As _
Object, ByVal e As CrystalDecisions.Windows.Forms.ExceptionEventArgs) _
Handles CrystalReportViewer1.HandleException
'Label1.Text = e.Exception.ToString();
'Here I will trap.
End Sub
Below is the stack trace I have found by applying above code for exception:
Source:CrystalDecisions.CrystalReports.Engine
Message:Object reference not set to an instance of an object.
ST: at CrystalDecisions.CrystalReports.Engine.ReportDocument.get_IsLoaded()
at CrystalDecisions.ReportSource.NonHTTPCachedReportSource.GetReport(RequestContext context, Boolean bAddToCacheWhenCreated)
at CrystalDecisions.ReportSource.LocalReportSourceBase.GetPage(PageRequestContext pageReqContext)
at CrystalDecisions.Windows.Forms.MainReportDocument.GetPage(PageRequestContext context)
I was looking a solution for this,
anyway i found the issue with my problem and so i am posting it here.
My report had a subreport and the its links where not set properly from the main report. In this scenario when i try to set only the parameters of main report and try to load the report, i was getting this object reference error.
So either link the subreport parameters to main report parameter or may be you can try setting the sub report parameters too. I have set the subreport parameters to link to the main report parameters and it works.

Weird functionality when using ScriptManager.RegisterStartupScript

I'm having this weird problem using the ScriptManager.RegisterStartupScript. Basically what I'm trying to accomplish when I click a button control, it goes back to the server, checks to see if any errors, and what I want to happen is that if there are any errors, want to use a popup box. The button control is inside an update panel. I thought the easiest way to accomplish this was to use a javascript alert box. So I finally got it working but when the alert box comes up it messes up my menu control until the alert box is going then its ok. Its kinda of hard to explain so I have insert a image to show you guys whats going on once the button is clicked. I dont know how to fix or whats going on, if someone could point me into the right direction. Also he my code im using.
Protected Sub btnPlan_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPlan.Click
'Dim sb As New System.Text.StringBuilder
sb.Append("<script language='javascript'>")
sb.Append("alert(' No data in the database ');")
sb.Append("</script>")
If (Not ClientScript.IsStartupScriptRegistered("JSScript")) Then
ScriptManager.RegisterStartupScript(UpdatePanel5, Me.GetType(), "JSScript", sb.toString(), True)
End If
You may try to use RegisterClientScriptBlock:
Protected Overrides Sub Page_Load(ByVal sender as Object, ByVal e As System.EventArgs)
MyBase.Page_Load(sender, e)
Try
If Not ScriptManager.IsInAsyncPostBack Then
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "AlertScript", "<script type='text/javascript' language='javascript'>function YourAlert(){alert(' No data in the database ');}</script>")
EndIf
Catch ex As Exception
End Try
End Sub
Thanks for all your suggestion. I skinned this a different way and got it to work the way I wanted. What i did was put the message in a hidden textbox and used jquery to ensure that the page was already load then alert the message if needed.

Trigger button click from another form

I have a little problem calling the button1_click event of form1 from my form2.
When I call form1.button1_click() it gives me an error saying:
Argument not specified for parameter 'e'.
How can I fix this?
Assuming WinForms, try using this:
form1.button1_click(Nothing, Nothing)
or
form1.button1_click(form1.button1, EventArgs.Empty)
The error means the procedure you are trying to run has parameters, but you left them out in your call. The click event is looking for two parameters, sender As Object and e As EventArgs.
You may also wish to try this:
form1.button1.PerformClick()

Lost Focus problems

I use the LostFocus Event in vb.net to check the validity of a field for the name.
After this field a have another one which is the for the password validity, and i'm handilg the same event in order to check the password.
My problem comes when i run the (name) lost focus, runs the code inside the sub and after that automatically goes to the password_lostfocus which brings me alot of troubles.
That happens even i use the error provider which works fine and bring to me the error with the red flashing.After that i put the command (name_textbox.focus), which logically has to bring the control in the name_textbox.. But NO.. the control goes to the Password_textbox sub automatically.
Please see my sub
Private Sub UsernameTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles UsernameTextBox.LostFocus
Select Case DB_Access.IfExistUser(UsernameTextBox.Text, sender, e)
Case True
PasswordTextBox.Focus()
Case False
ErrorProvider1.SetError(UsernameTextBox, "Ο χρήστης ΔΕΝ υπάρχει παρακαλώ καλέστε τον Administrator")
Beep()
UsernameTextBox.Text = ""
UsernameTextBox.Focus()
End Select
End Sub
Please if anyone have seen this issue and face it, assist me.
Excuse me for some Greek characters they are meaningless, they are comments
Well finally i found that.
In order to handle the Login Form as it give it from visual studio 2010 you need to do it in only one sub (Lost Focus) and that is only the password_LostFocus.
I believe the particular form is meant to be like that.
Any way i solve the issue and if anybody needs assistance on that just "asc a question"