-
Apr 20th, 2016, 08:18 AM
#1
Thread Starter
New Member
Variable is used before it has been assigned a value. A null reference except
Hey everyone,
I was working on my homework, making a website obiously, but I have a small problem with the griedview. I wanted to add a button on the end of the row, but I receive some error notifications and warnings too... I seached every corner on the internet and I didn't find the solution. Can someone help me?
Code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Class Search
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Session("Username") IsNot Nothing Then
HyperLink1.Text = Session("Username")
HyperLink1.NavigateUrl = "Profile.aspx"
HyperLink2.Text = "Logout"
HyperLink2.NavigateUrl = "Logout.aspx"
End If
If Not IsPostBack Then
GridView1.DataSource = GetData("Select * From Vluchten Where Vertrek='" + Session("Vertrek") + "' and Bestemming = '" + Session("Bestemming") + "'")
GridView1.DataBind()
End If
End Sub
Public Function GetData(query As String) As DataTable
Dim strConnString As String = "Data Source=EZEPHRA\SQLEXPRESS;Initial Catalog=Default; User ID=sa; Password=DitIs1SuperGoedW8woord!"
Using con As New SqlConnection(strConnString)
Using cmd As New SqlCommand()
cmd.CommandText = query
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
Dim dt As New DataTable()
Dim btnfld As ButtonField
sda.Fill(dt)
dt.Columns.Add(btnfld)
btnfld.Text = "Reservatie"
btnfld.CommandName = "Reserveren"
btnfld.HeaderText = "Reservatie"
btnfld.ButtonType = "Button"
Return dt
End Using
End Using
End Using
End Using
End Function
End Class
There is this error too:
Severity Code Description Project File Line Suppression State
Error BC30518 Overload resolution failed because no accessible 'Add' can be called with these arguments:
'Public Overloads Sub Add(column As DataColumn)': Value of type 'ButtonField' cannot be converted to 'DataColumn'.
'Public Overloads Function Add(columnName As String) As DataColumn': Value of type 'ButtonField' cannot be converted to 'String'. GIP H:\GIP\Search.aspx.vb 31 Active
-
Apr 20th, 2016, 09:19 AM
#2
Re: Variable is used before it has been assigned a value. A null reference except
I was working on my homework, making a website obiously,
Actually, no it wasn't obvious, good thing you pointed that out... ASP.NET has some differences from VB.NET, the most notable difference is the grid... WinForms uses the dataGridView, while ASP.NET uses a DataGrid... and they are close enough that people get them confused, and yet just enough different that people get into trouble trying to use the methods of one over the other. Point is, this thread's in the wrong spot - you'll get WinForms DataGridView answers where, when what you need are ASP.NET DataGrid answers. I'll have a mod move it.
-tg
-
Apr 20th, 2016, 09:23 AM
#3
Re: Variable is used before it has been assigned a value. A null reference except
Meanwhile - reagrding the first warning it just means you've defined a variable, and but didn't initialize it initially so there's a chance that when you go to use it, it could be Nothing which will cause an error.
In fact, that's the problem with these lines:
Code:
Dim btnfld As ButtonField
sda.Fill(dt)
dt.Columns.Add(btnfld)
You used btnFld before assigning it a value ...
change
Dim btnfld As ButtonField
to
Dim btnfld As New ButtonField
-tg
-
Apr 20th, 2016, 10:04 AM
#4
Thread Starter
New Member
Re: Variable is used before it has been assigned a value. A null reference except
-
Apr 20th, 2016, 10:12 AM
#5
Thread Starter
New Member
Re: Variable is used before it has been assigned a value. A null reference except
But dt.Columns.Add(btnfld), the Add gives still the error:
Error BC30518 Overload resolution failed because no accessible 'Add' can be called with these arguments:
'Public Overloads Sub Add(column As DataColumn)': Value of type 'ButtonField' cannot be converted to 'DataColumn'.
'Public Overloads Function Add(columnName As String) As DataColumn': Value of type 'ButtonField' cannot be converted to 'String'.
-
Apr 20th, 2016, 10:28 AM
#6
Re: Variable is used before it has been assigned a value. A null reference except
right... it's a different problem from the original... and it should be abundantly clear what the problem is - you can't drive a square peg through a round hole.
There are two ways to call the .Add - one is to provide a DataColumn and the other is to provide a ColumnName (a string) ... neither of those method (the round holes) accept a ButtonField (your square peg) ... A column isn't a field. A field isn't a column.
-tg
-
Apr 20th, 2016, 12:18 PM
#7
Thread Starter
New Member
Re: Variable is used before it has been assigned a value. A null reference except
-
Apr 20th, 2016, 02:35 PM
#8
Re: Variable is used before it has been assigned a value. A null reference except
Beats me... this is why the forum you post in is important... it's going to take someone familair with ASP.NET and has used the DataGrid to help you figure it out...
My GUESS though based on just the NAMES alone... that you create teh button EACH time and add it to the field... I'm sure it's called ButtonField and not ButtonColumn for a reason.
-tg
-
Apr 20th, 2016, 02:46 PM
#9
Thread Starter
New Member
Re: Variable is used before it has been assigned a value. A null reference except
Your guess is right, that what I want to do but you helped me so far, so you have my thanks.
-
Apr 28th, 2016, 08:03 AM
#10
Re: Variable is used before it has been assigned a value. A null reference except
Actually, there's a bit more to the whole grid thing than TG described. There are DataGrid controls in both ASP and Winforms but they're not the same as each other and they've been deprecated in both technologies. The WinForms replacement is a DataGridView and the ASP replacement is the GridView. Confused yet? Just wait until you chuck DataTables and DataViews into the mix and work out the number of different types there are that represent some kind of row... it's a veritable cornucopia of potential confusion.
Anyway, it looks from your code like you're using a GridView (which is good) and to add to that you actually use a TemplateColumn with a Template that describes a button. Here's an example.
Basically, you add a column based on a TemplateField to the definition of your GridView (not the DataTable as you have done).
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|