Results 1 to 10 of 10

Thread: Variable is used before it has been assigned a value. A null reference except

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    5

    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

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    5

    Re: Variable is used before it has been assigned a value. A null reference except

    Oh, thanks tg

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    5

    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'.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    5

    Re: Variable is used before it has been assigned a value. A null reference except

    Then, what should I use?

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    5

    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.

  10. #10
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    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
  •  



Click Here to Expand Forum to Full Width