Get number data from ms access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbdb
    New Member
    • Feb 2009
    • 25

    Get number data from ms access

    hi guys,

    i tried to get some data from my access db. i used repeater to get the data, but i got a problem when i tried to get the data with number type in ms access.
    it shown an error.

    is there another way to get the data ?

    thank you.
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Can you post some sample code? You need to cast before you can "copy" from repeater... also need to check for null values ...

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Sample code would really help us understand what you're talking about.

      (You can't use a Repeater to retrieve data from a database...but you can use it to display data Retrieved from the database...)

      -Frinny

      PS

      When posting code snippets please remember to use code tags

      Comment

      • dbdb
        New Member
        • Feb 2009
        • 25

        #4
        here is the code.

        Code:
        Sub Page_Load (Source As Object, E as EventArgs)
                Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("surat.mdb") & ";"
                Dim MySQL As String = "SELECT no, nosurat, tglsurat, kategori, tujuan, perihal, deskripsi FROM surat"
        	Dim MyConn as New OleDBConnection (strConn)
        	Dim Cmd as New OleDBCommand (MySQL, MyConn)
        	MyConn.Open ()
                rptSurat.DataSource = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
                rptSurat.DataBind()
        End Sub
        and for the repeater,

        Code:
        <asp:Repeater ID="rptSurat" Runat="Server">
        	<HeaderTemplate>
        		<table border=1 cellpadding=5 cellspacing=0 width=50%>
        	</HeaderTemplate>
        
        	<ItemTemplate>
        
        		<tr>		
        			<td width=30%><b><font face=Tahoma size=2>No </font></b></td> 
        				<td><font face=Tahoma size=2><%#Container.DataItem("no")%></td></tr>						
        		<tr>
        			<td width=30%><b><font face=Tahoma size=2>Tanggal Surat </td> 
        				<td><font face=Tahoma size=2><%#Container.DataItem("tglSurat")%></td></tr>	
        		<tr>
        			<td width=30%><b><font face=Tahoma size=2>Kategori </td> 
        				<td><font face=Tahoma size=2><%#Container.DataItem("kategori")%></td></tr>
        		<tr>
        			<td width=30%><b><font face=Tahoma size=2>Tujuan </td> 
        				<td><font face=Tahoma size=2><%#Container.DataItem("tujuan")%></td></tr>
        		<tr>
        			<td width=30%><b><font face=Tahoma size=2>Perihal </td> 
        				<td><font face=Tahoma size=2><%#Container.DataItem("perihal")%></td></tr>
                <tr>
        			<td width=30%><b><font face=Tahoma size=2>Deskripsi Isi </td> 
        				<td><font face=Tahoma size=2><%#Container.DataItem("deskripsi")%></td></tr>				
        				
        			</ItemTemplate>
        		
        	<SeparatorTemplate>
        		<tr height=25><td></td></tr>
        	</SeparatorTemplate>
        	
        	<FooterTemplate>
        		</table>
        	</FooterTemplate>
        
        </asp:Repeater>
        no : number type in access.
        i couldn't get the data, it shown an error.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Ok, next thing we need to know is what the error message says :)

          Comment

          • dbdb
            New Member
            • Feb 2009
            • 25

            #6
            the error msg is like this :

            Code:
            Exception Details: System.IndexOutOfRangeException: no
            
            Source Error: 
            
            
            Line 49: 		<tr>		
            Line 50: 			<td width=30%><b><font face=Tahoma size=2>Letter No </font></b></td> 
            Line 51: 				<td><font face=Tahoma size=2><%#Container.DataItem("no")%></td></tr>						
            Line 52: 		<tr>
            Line 53: 			<td width=30%><b><font face=Tahoma size=2>Date </td> 
             
            
            Source File: D:\ASPwebsite\srtView.aspx    Line: 51 
            
            Stack Trace: 
            
            
            [IndexOutOfRangeException: no]
               Microsoft.VisualBasic.CompilerServices.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags) +202
               Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure) +150
               Microsoft.VisualBasic.CompilerServices.NewLateBinding.InternalLateIndexGet(Object Instance, Object[] Arguments, String[] ArgumentNames, Boolean ReportErrors, ResolutionFailure& Failure) +166
               Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateIndexGet(Object Instance, Object[] Arguments, String[] ArgumentNames) +22
               ASP.srtview_aspx.__DataBind__control4(Object sender, EventArgs e) in D:\ASPwebsite\srtView.aspx:51
               System.Web.UI.Control.OnDataBinding(EventArgs e) +99
               System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +92
               System.Web.UI.Control.DataBind() +15
               System.Web.UI.Control.DataBindChildren() +211
               System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
               System.Web.UI.Control.DataBind() +15
               System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +124
               System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +443
               System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +51
               System.Web.UI.WebControls.Repeater.DataBind() +75
               ASP.srtview_aspx.Page_Load(Object Source, EventArgs E) in D:\ASPwebsite\srtView.aspx:14
               System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
               System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
               System.Web.UI.Control.OnLoad(EventArgs e) +99
               System.Web.UI.Control.LoadRecursive() +50
               System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
            error in line 51

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I see your problem.

              On line 7 of your VB code:
              Originally posted by dbdb
              Code:
              Sub Page_Load (Source As Object, E as EventArgs)
                      Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("surat.mdb") & ";"
                      Dim MySQL As String = "SELECT no, nosurat, tglsurat, kategori, tujuan, perihal, deskripsi FROM surat"
              	Dim MyConn as New OleDBConnection (strConn)
              	Dim Cmd as New OleDBCommand (MySQL, MyConn)
              	MyConn.Open ()
                      rptSurat.DataSource = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
                      rptSurat.DataBind()
              End Sub
              You are assigning the DataSource of the repeater to the return value of the ExecuteReader() method.

              The ExecuteReader method does not return a Table or any other item that can properly be used as a data source. It returns an OleDbDataReader Object. You would have to use this object (read from the data stored within it) to create a data source that could properly be used with the Repeater.

              You could do that, but I'm going to recommend using the OleDataAdpater class to fill a DataSet. Once the OleDataAdapter' s Fill method is used to fill a DataSet, the DataSet will contain DataTables which you can use as a data source for your repeater. In your case it's likely that only 1 table will be in the DataSet....so this should be rather straightforward .

              Look into how use DataAdapters and DataSets....and if you're really curious, look up how to use a DataReader properly.

              Comment

              • shiznit770
                New Member
                • Apr 2009
                • 10

                #8
                The error indicates that the repeater is attempting to read an index that is not present on the datasource.

                Make your select statement in a query so that you can see the data it returns. Double check the spelling of your column names and the corresponding indexes used by the data repeater.

                Comment

                Working...