Datagrid Not Displaying

dotnetnubie

Member
Joined
Jun 4, 2005
Messages
9
Programming Experience
10+
Hello.

I am new to VB.Net, and am working on my first ASP.Net project. To get started, I've built a simple database in SQL 2000, and have established a connection in Visual Studio. Then, I created a new ASP.Net Web Application. I then added an SqlDataAdapter to the form, and setup the requisite items for it, which created the SqlConnection and DataSet. For the time being, I placed the SqlDataAdatper fill statement code in the Page_Load sub (I'll create an event for that later). The problem is, when I debug it, no data shows in the datagrid. In fact, no part of the datagrid is visible. I know the data connectivity is working, becuase if I use the "Preview Data" option on the SqlDataAdapter, the test records are dispalyed.

Has anyone encountered this before, or have an idea where I might look to resolve this issue? I have a long way to go, but this is an important step. If necessary, I can include some sample code. I'm sure I'm missing something really simple, but I sure can't tell what it is. Any assistance would be much appreciated!!!

Thanks to all in advance,
dotnetnubie
:cool:
 
Thank you for the reply, Levy. The code for the form follows, and thank you again for your time and effort. dotnetnubie.

Public Class WebForm1

Inherits System.Web.UI.Page

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList

Protected WithEvents Button1 As System.Web.UI.WebControls.Button

Protected WithEvents Label2 As System.Web.UI.WebControls.Label

Protected WithEvents DropDownList2 As System.Web.UI.WebControls.DropDownList

Protected WithEvents dg_hremployees As System.Web.UI.WebControls.DataGrid

Protected WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter

Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection

Protected WithEvents Ds_fspersonnel1 As FireSafeHR.ds_fspersonnel

Protected WithEvents Label_Company As System.Web.UI.WebControls.Label

Protected WithEvents company_logo As System.Web.UI.WebControls.Image

Protected WithEvents Dropdownlist3 As System.Web.UI.WebControls.DropDownList

Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand

Protected WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand

Protected WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand

Protected WithEvents SqlDeleteCommand1 As System.Data.SqlClient.SqlCommand

Public company_id As String

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter

Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection

Me.Ds_fspersonnel1 = New FireSafeHR.ds_fspersonnel

Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand

Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand

Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand

Me.SqlDeleteCommand1 = New System.Data.SqlClient.SqlCommand

CType(Me.Ds_fspersonnel1, System.ComponentModel.ISupportInitialize).BeginInit()

'

'SqlDataAdapter1

'

Me.SqlDataAdapter1.DeleteCommand = Me.SqlDeleteCommand1

Me.SqlDataAdapter1.InsertCommand = Me.SqlInsertCommand1

Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1

Me.SqlDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "fs_personnel", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("NAME", "NAME"), New System.Data.Common.DataColumnMapping("CATEGORY", "CATEGORY"), New System.Data.Common.DataColumnMapping("COMPANY_ID", "COMPANY_ID"), New System.Data.Common.DataColumnMapping("EMP_ID", "EMP_ID")})})

Me.SqlDataAdapter1.UpdateCommand = Me.SqlUpdateCommand1

'

'SqlConnection1

'

Me.SqlConnection1.ConnectionString = "workstation id=REDWATER1;packet size=4096;user id=firesafehr;data source=localhos" & _

"t;persist security info=True;initial catalog=FireSafeHR;password=firesafehr"

'

'Ds_fspersonnel1

'

Me.Ds_fspersonnel1.DataSetName = "ds_fspersonnel"

Me.Ds_fspersonnel1.Locale = New System.Globalization.CultureInfo("en-US")

'

'SqlSelectCommand1

'

Me.SqlSelectCommand1.CommandText = "[HRSelectCommand]"

Me.SqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure

Me.SqlSelectCommand1.Connection = Me.SqlConnection1

Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))

'

'SqlInsertCommand1

'

Me.SqlInsertCommand1.CommandText = "[HRInsertCommand]"

Me.SqlInsertCommand1.CommandType = System.Data.CommandType.StoredProcedure

Me.SqlInsertCommand1.Connection = Me.SqlConnection1

Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))

Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@NAME", System.Data.SqlDbType.VarChar, 25, "NAME"))

Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CATEGORY", System.Data.SqlDbType.VarChar, 1, "CATEGORY"))

Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@COMPANY_ID", System.Data.SqlDbType.VarChar, 10, "COMPANY_ID"))

Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@EMP_ID", System.Data.SqlDbType.VarChar, 10, "EMP_ID"))

'

'SqlUpdateCommand1

'

Me.SqlUpdateCommand1.CommandText = "[HRUpdateCommand]"

Me.SqlUpdateCommand1.CommandType = System.Data.CommandType.StoredProcedure

Me.SqlUpdateCommand1.Connection = Me.SqlConnection1

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@NAME", System.Data.SqlDbType.VarChar, 25, "NAME"))

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CATEGORY", System.Data.SqlDbType.VarChar, 1, "CATEGORY"))

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@COMPANY_ID", System.Data.SqlDbType.VarChar, 10, "COMPANY_ID"))

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@EMP_ID", System.Data.SqlDbType.VarChar, 10, "EMP_ID"))

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_COMPANY_ID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "COMPANY_ID", System.Data.DataRowVersion.Original, Nothing))

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_EMP_ID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "EMP_ID", System.Data.DataRowVersion.Original, Nothing))

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_CATEGORY", System.Data.SqlDbType.VarChar, 1, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "CATEGORY", System.Data.DataRowVersion.Original, Nothing))

Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_NAME", System.Data.SqlDbType.VarChar, 25, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "NAME", System.Data.DataRowVersion.Original, Nothing))

'

'SqlDeleteCommand1

'

Me.SqlDeleteCommand1.CommandText = "[HRDeleteCommand]"

Me.SqlDeleteCommand1.CommandType = System.Data.CommandType.StoredProcedure

Me.SqlDeleteCommand1.Connection = Me.SqlConnection1

Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))

Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_COMPANY_ID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "COMPANY_ID", System.Data.DataRowVersion.Original, Nothing))

Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_EMP_ID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "EMP_ID", System.Data.DataRowVersion.Original, Nothing))

Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_CATEGORY", System.Data.SqlDbType.VarChar, 1, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "CATEGORY", System.Data.DataRowVersion.Original, Nothing))

Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_NAME", System.Data.SqlDbType.VarChar, 25, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "NAME", System.Data.DataRowVersion.Original, Nothing))

CType(Me.Ds_fspersonnel1, System.ComponentModel.ISupportInitialize).EndInit()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Dim company_logo_path As String

company_id = "99999999"

Me.Label_Company.Text = "Sample Company Name"

company_logo_path = "complogos/" & company_id & ".jpg"

Me.company_logo.ImageUrl = company_logo_path

Me.DropDownList2.Visible = False

Me.Dropdownlist3.Visible = False

Me.Ds_fspersonnel1.Clear()

Me.SqlDataAdapter1.Fill(Ds_fspersonnel1)

Me.dg_hremployees.Visible = True

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If DropDownList1.SelectedItem.Value = 1 Then

Me.Label1.Text = DropDownList1.SelectedItem.Text

Me.Label1.ForeColor = System.Drawing.Color.Green

Me.DropDownList2.Visible = True

Me.Dropdownlist3.Visible = True

ElseIf DropDownList1.SelectedItem.Value = 2 Then

Me.Label1.Text = DropDownList1.SelectedItem.Text

Me.Label1.ForeColor = System.Drawing.Color.Gray

Me.DropDownList2.Visible = True

Me.Dropdownlist3.Visible = True

ElseIf DropDownList1.SelectedItem.Value = 3 Then

Me.Label1.Text = DropDownList1.SelectedItem.Text

Me.Label1.ForeColor = System.Drawing.Color.Blue

Me.DropDownList2.Visible = True

Me.Dropdownlist3.Visible = True

ElseIf DropDownList1.SelectedItem.Value = 4 Then

Me.Label1.Text = DropDownList1.SelectedItem.Text

Me.Label1.ForeColor = System.Drawing.Color.Red

Me.DropDownList2.Visible = True

Me.Dropdownlist3.Visible = True

Else

Me.DropDownList2.Visible = False

Me.Dropdownlist3.Visible = False

Me.Label1.Text = " "

End If

End Sub





End
Class

 
Ok thats alot of code :)

It looks like it might be because you haven't opened the connection to the database. I use this code to fill a ds and populate a grid.
VB.NET:
[size=2][color=#0000ff]Protected[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Page_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] [/size][size=2][color=#0000ff]Me[/color][/size][size=2].Load

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] conn [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlConnection = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlConnection([/size][size=2][color=#800000]"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Procedures.mdf;Integrated Security=True;User Instance=True"[/color][/size][size=2])
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] ds [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataSet
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] adapter [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlDataAdapter
adapter = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlDataAdapter([/size][size=2][color=#800000]"SELECT * FROM DELIVERIES "[/color][/size][size=2], conn)
adapter.SelectCommand.CommandType = CommandType.Text
ds = [/size][size=2][color=#0000ff]New[/color][/size][size=2] DataSet()
adapter.Fill(ds, [/size][size=2][color=#800000]"Deliveries"[/color][/size][size=2])
GridView1.DataSource = ds.Tables(0).DefaultView
GridView1.DataBind()
adapter.Dispose()
conn.Close()
[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub
[/color][/size]

Try my code, alter it to reflect the table you want to get data from. See if it helps.
 
Apologies for the abundance of code - I just didn't want to leave out something important :) I will incorporate the recommeded changes this afternoon, and will post the results. Thank you very kindly again!!!

dotnetnubie
 
I reviewed my code, and compared them with the example provided. After playing with it for awhile, was FINALLY able get it to work. The main problem was that I was omitting the datagrid.databind() statement. That did the trick! It would have taken me a long time to figure out that by myself. Thank you again, Levy, for your time and effort. I'm very much obliged.

Best regards,
dotnetnubie :)))
 
Back
Top