Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

value cannot be null - using dataset

Status
Not open for further replies.

HomerJS

Programmer
Jun 25, 2001
86
US
I'm building a windows application that has a datagrid. I want to make a few columns invisible and have found a way to do it using a dataset. When I load the datagrid with a datatable, it works fine. However when I load it with a dataset I get the following error:

An unhandled exception of type 'System.ArgumentNullException' occurred in system.data.dll

Additional information: Value cannot be null.


All of the fields I'm loading into the grid have a value other than null.
So I guess my question is:
Does anyone know a way to make columns in a datagrid invisible using a datatable?
OR
Does anyone know what the difference is between the following code?



'Works -------------
Dim ad As SqlClient.SqlDataAdapter
Dim tab As New DataTable()
Dim ds As DataSet

OpenSQLConnection()

ad = New SqlClient.SqlDataAdapter("select TaskID, ProjectID, [Description], StartDate, DueDate, CompleteDate, Status, [Dependency] from Task where ProjectID = 1 order by [Order]", con)
ad.Fill(tab)
TaskGrid.DataSource = tab

CloseSQLConnection()


'Does not work -----
Dim ad As SqlClient.SqlDataAdapter
Dim tab As New DataTable()
Dim ds As DataSet

OpenSQLConnection()

ad = New SqlClient.SqlDataAdapter("select TaskID, ProjectID, [Description], StartDate, DueDate, CompleteDate, Status, [Dependency] from Task where ProjectID = 1 order by [Order]", con)

ad.Fill(ds, "Tasks")
TaskGrid.DataSource = ds.Tables("Tasks")

CloseSQLConnection()
 
Ok, it's definately a Monday. Here's the solution to both my own questions.

My problem with the dataset was when I declared the adapter I included the select statement. Apparently you have to do the steps separately.


Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim ds As DataSet = New DataSet()
Dim strSQL As String

Dim conn As New SqlClient.SqlConnection("...connection string ...")
Dim adapter As New SqlClient.SqlDataAdapter()
strsql = "select TaskID, ProjectID, [Description] from Task where ProjectID = 1 order by [Order]"
adapter.SelectCommand = New SqlClient.SqlCommand(strSQL, conn)
adapter.Fill(ds, "Task")

'TaskGrid.DataSource = myDataSet

Dim dv As DataView

Dim gts As New DataGridTableStyle()
gts.MappingName = "Task"

Dim gc As New DataGridTextBoxColumn()
With gc
.MappingName = "TaskID"
.Width = 50
.HeaderText = "Tid"
End With
gts.GridColumnStyles.Add(gc)

Dim gc1 As New DataGridTextBoxColumn()
With gc1
.MappingName = "ProjectID"
.Width = 0 'setting the width to zero makes it 'invislble'
.HeaderText = "Pid"
End With
gts.GridColumnStyles.Add(gc1)

Dim gc2 As New DataGridTextBoxColumn()
With gc2
.MappingName = "Description"
.Width = 150
.HeaderText = "Descr"
End With
gts.GridColumnStyles.Add(gc2)

TaskGrid.TableStyles.Add(gts)
TaskGrid.DataSource = ds.Tables("Task").DefaultView

conn.Close()
conn = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top