Display selected field on the datagrid

Hi Guys! I want to display values from the database on to the datagrid. All seems to work when the sql stmt reads as
myCommand.CommandText = "select * from empattendence where adate = #" & txtdate.Text & "#"
but when i select particular fields to display, it gives an error. The following is the sql that i am trying to use
myCommand.CommandText = "select empid,empname,empdesignation,dhours,othours from empattendence where adate = #" & txtdate.Text & "#"
Please let me know what is to be changed in the code. Thanks in advance
Dim con As New System.Data.OleDb.OleDbConnection
        Dim dreader As System.Data.OleDb.OleDbDataReader
        Dim myCommand As New System.Data.OleDb.OleDbCommand
        Dim myPath As String
 
        myPath = Server.MapPath("hayat.mdb")
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath & ";"
        myCommand.CommandText = "select empid,empname,empdesignation,dhours,othours from empattendence where adate = #" & txtdate.Text & "#"
        myCommand.Connection = con
        Try
            con.Open()
            dreader = myCommand.ExecuteReader
 
            DataGrid1.DataSource = dreader
            DataGrid1.DataBind()
            dreader.Close()
        Catch ex As Exception
            lblerror.Text = "There is an error" + ex.ToString
            con.Close()
        End Try

Open in new window

preethamonlineAsked:
Who is Participating?
 
bedanandCommented:
Please check  the bound columns on the data grid. You might be binding the column that the query result doesnot contains that column.

Txs
Bedanand
http://www.dot4pro.com

0
 
amar31282Commented:
what is the error coming in lblerror
0
 
preethamonlineAuthor Commented:
Hi guys, will check out the solution and post in a few minutes. Sorry was away.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
preethamonlineAuthor Commented:
Dear bedanand, you were absolutely right. The date field was bound to the datagrid and hence the error. Actually i had bound the date field to the datagrid as to format it like {0:d}. But when the date field is displayed in the datagrid, it appears as 4/19/2009 12:00:00 AM. How do i make it appear as just 4/19/2009
0
 
bedanandCommented:
Hi,

Are u binding like this.

<asp:BoundField DataField="MyDate" DataFormatString="{0:d}" />
If its like this it should work.
Or you can also bind like this

<asp:BoundField DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />


Regards
Bedanand
http://www.dot4pro.com


0
 
preethamonlineAuthor Commented:
where should i eneter this code
0
 
preethamonlineAuthor Commented:
I get an error message saying that active schema does not support the element <asp:boundfield>
0
 
bedanandCommented:
Put on the columns section of gridview.

0
 
preethamonlineAuthor Commented:
Thanks bedanand, i managed to solve it. Basically i made autogeneratecoloumns = false and then bound the columns which i needed to display to the grid. This gave me more options to make it presentable.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.