Solved

Issue with dropdown box and submit - ASP.NET

Posted on 2004-03-24
25
1,212 Views
Last Modified: 2013-11-25
Hello,

this should be easy, but for some reason I have a problem. I am using VB.NET and ASP.NET

I have a table in SQL DB where all info that user enters are stored. Each record includes a date info.
I want to show info from the table for a specific date.

As a part of testing process I created a page that shows all info from the SQL table, and that works fine. For better look I placed all info into a HTML table. So, my insert into a table and reading info from a table work fine.

I created a page with a dropdown list and connected it to a date column of the SQL table. This part works fine - I can see all dates available in my dropdown.

My plan is:

When a user choose a specific date, and clicks submit button, all info/record from the table for that specific date should be shown on this page. (the page with dropdown list will be replaced with a page that consists the info)

I can not get this part to work.

Do I need to use some of JavaScript code for this functionality, or I can do everything in VB.NET?

Do you have some code examples?

Thank you for your help.

Srdjan
0
Comment
Question by:JoshFink
  • 12
  • 12
25 Comments
 
LVL 6

Expert Comment

by:sforcier
Comment Utility
If you can already display the data, just use the code you already have and modify the query to include "... WHERE DateField = '" & DropDownValue & "'"

Otherwise, we'll need to know what specifically you mean by "can not get this part to work."
0
 
LVL 31

Accepted Solution

by:
alorentz earned 110 total points
Comment Utility
Private Sub dd1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dd1.SelectedIndexChanged
 
dim SQL = "SELECT * FROM yourtbale where YourDateField = '" & dd1.SelectedIndex.Value & "'"
'yada, yada, yada

End Sub
0
 

Author Comment

by:JoshFink
Comment Utility
Thank you both for your promt responses.

Alorentz,

I used your code as follows:

Private Sub ddl_SelectDate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddl_SelectDate.SelectedIndexChanged
            
            
Dim strSQL1 as String = "SELECT EquipmentType,ReportDate,Person,EmployeeNo,FormCompleted,Location,CompletedBy,Description FROM ReportTest where ReportDate = '" & ddl_SelectDate.SelectedIndex.Value & "';"
            
Dim strConnection1 As String = "user id=sa;password=konik;" & _
            "database=DamageReport;server=localhost;" & _
            "Connect Timeout=30"
       
        Dim objDataSet1 As New DataSet()
        Dim objconnection1 As New SqlConnection(strConnection1)
       
        'create a new DataAdapter
       
        Dim objDataAdapter1 As New SqlDataAdapter(strSQL1, objConnection1)
       
        'fill the dataset with data from DataAdapter object
       
        objDataAdapter1.Fill(objDataSet1, "ReportTest")
       
        'create a DataView object fro the ReportTest table in the DataSet
       
        Dim objDataView1 As New DataView(objDataSet1.Tables("ReportTest"))
       
        'assign the DataView object to the DataGrid control
       
        dgrDamageReport.DataSource = objDataView1
        dgrDamageReport.DataBind()
       
       
        End Sub

Associated with this code are:

             <input type="button" value="Submit Date" runat="server" onclick="ddl_SelectDate"/>
      
      <asp:DataGrid ID="dgrDamageReport" Runat="server" />

The error I got is:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30506: Handles clause requires a WithEvents variable.

Source Error:
Line 106:        
Line 107:        
Line 108:            Private Sub ddl_SelectDate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddl_SelectDate.SelectedIndexChanged
Line 109:            
Line 110:            
 
What causes the error?

BTW, as you can see I called subroutine from onclick event in submit button. Is this the right way?
Also, on the same page in Sub_Load I also have a connection to the same SQL DB table DamageReport which is used to get date info to populate the dropdown box. Is it O.K. to have both connections on the same page? Do I need to close the first one at the end of the Sub_Load so everything would work properly?


I am new to ASP.NET, so I still do not understand some of basic but very imortant details/rules and relations.

Thanks

Srdjan
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
Need to have this at top of class:

Protected WithEvents ddl_SelectDate As System.Web.UI.WebControls.DropDownList
0
 

Author Comment

by:JoshFink
Comment Utility
Alorentz,

my code from above was not in the class.

I put code it in a class such as below, and I got error:

Compiler Error Message: BC30260: 'ddl_SelectDate' is already declared as 'Protected Dim WithEvents ddl_SelectDate As System.Web.UI.WebControls.DropDownList' in this class.

Source Error:

Line 109:        Protected WithEvents ddl_SelectDate As System.Web.UI.WebControls.DropDownList
Line 110:        
Line 111:            Private Sub ddl_SelectDate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSelectDate.SelectedIndexChanged
Line 112:            
Line 113:            
 
My code is:

public Class SelectDate
       
        Protected WithEvents ddl_SelectDate As System.Web.UI.WebControls.DropDownList
         
      Private Sub ddl_SelectDate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSelectDate.SelectedIndexChanged
            
            
      Dim strSQL1 as String = "SELECT EquipmentType,ReportDate,Person,EmployeeNo,FormCompleted,Location,CompletedBy,Description FROM ReportTest where ReportDate = '"   & ddlSelectDate.SelectedIndex.Value & "';"
            
      Dim strConnection1 As String = "user id=sa;password=konik;" & _
            "database=DamageReport;server=localhost;" & _
            "Connect Timeout=30"
       
        Dim objDataSet1 As New DataSet()
        Dim objconnection1 As New SqlConnection(strConnection1)
       
        'create a new DataAdapter
       
        Dim objDataAdapter1 As New SqlDataAdapter(strSQL1, objConnection1)
       
        'fill the dataset with data from DataAdapter object
       
        objDataAdapter1.Fill(objDataSet1, "ReportTest")
       
        'create a DataView object fro the ReportTest table in the DataSet
       
        Dim objDataView1 As New DataView(objDataSet1.Tables("ReportTest"))
       
        'assign the DataView object to the DataGrid control
       
        dgrDamageReport.DataSource = objDataView1
        dgrDamageReport.DataBind()
       
       
        End Sub
       
        End Class

Thanks
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
Not in a class?  Are you using CODEBEHIND?  If so...everything is a class!

Every object on a page creates something like this: (codebehind)

Protected WithEvents ddl_SelectDate As System.Web.UI.WebControls.DropDownList
0
 

Author Comment

by:JoshFink
Comment Utility
I am not using a code behind. I simply coded everything on a same page. Can it work this way or I must (or is better) do it using code behind?

I just read about code behind, and will try to implement it that way.

Stand By.

Thanks
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
Code behind is better, that's the way .Net was built...

Regardless, forget about the class then.  How bout just putting this line:
Protected WithEvents ddl_SelectDate As System.Web.UI.WebControls.DropDownList

Somewhere above the Sub Routine in a script block....no classes?
0
 

Author Comment

by:JoshFink
Comment Utility
Alorentz,

I added the above line as you suggested, but I still get an error:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30260: 'ddlSelectDate' is already declared as 'Protected Dim ddlSelectDate As System.Web.UI.WebControls.DropDownList' in this class.

Source Error:
Line 106:        
Line 107:        
Line 108:        Protected WithEvents ddlSelectDate As System.Web.UI.WebControls.DropDownList
Line 109:        
Line 110:            Private Sub ddl_SelectDate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSelectDate.SelectedIndexChanged
 
I read about proper declaration of Handles reference and checked the names of objects and events, just to make sure they match.

Maybe I am missing something.

Thank you for your help.

Srdjan

0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
Whereever this line is:

'Protected Dim ddlSelectDate As System.Web.UI.WebControls.DropDownList'

Change to this line:

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

Read the error....it's say it's already Dimmed somewhere.  The Dim needs to be changed to WithEvents...no Dim.

P.S.  This is why inline coding for .Net SUCKS!



0
 

Author Comment

by:JoshFink
Comment Utility
Alorentz,

Problem is that the line in question does not exist in my code, so I can not change from Dim to WithEvents:

'Protected Dim ddlSelectDate As System.Web.UI.WebControls.DropDownList' - this does not exist

It looks that ddlSelectDate is somehow Dimmed somewhere but I am not sure where and what causing it. That's why I checked declaration of Handles reference to make sure what objects and elements are named ddlSelectDate.

My dropdown list has id=ddlSelectDate, so I used this ddlSelectDate as an object in WithEvents line such as

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

Maybe this is the problem?

- Help on Microsoft Development Environment 2003 gives following declaration for Handles:

"Parts
proceduredeclaration Handles event

The Sub procedure declaration for the procedure that will handle the event.
event
The name of the event being handled. This event must be raised by either the base class for the current class, or by an object declared using the WithEvents keyword.
Remarks
Use the Handles keyword at the end of a procedure declaration to cause it to handle events raised by an object variable declared using the WithEvents keyword. The Handles keyword can also be used in a derived class to handle events from a base class.

 Public Class ContainerClass
   WithEvents Obj As New Class1()  ' Module or class level declaration.

   Public Class Class1
       Public Event Ev_Event()      ' Declare an event.

   Sub EventHandler() Handles Obj.Ev_Event   - I used this declaration. As I understnad my Obj should be my dropdown list id, and my Ev_Event is SelectedIndexChanged. I also have a submit button that has onclick = ddl_SelectDate (calls the subroutine declared below)

So in my case the two main lines are:

   Protected WithEvents ddlSelectDate As System.Web.UI.WebControls.DropDownList
         
   Private Sub ddl_SelectDate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSelectDate.SelectedIndexChanged

This produces the error mentioned in my previous post. Do you see what could be wrong?

Thanks





0
 

Author Comment

by:JoshFink
Comment Utility
Alorentz,

I did my simple application using code behind. I do not have the wierd error from above.

Now, back to my original question/problem.

So, page loads O.K., but when I choose an appropriate date from dropdown menu and click submit, nothing happens and in the left bottom corner it says Script Error!.

Can you help me to troubleshoot my code?

I think the problem is between my SQL code in .vb file and dropdown menu in my .aspx file?

If you are willing to help me, I would post my code from both .aspx and .vb files.

Even you experts do not care much about points (you help primary for your own fun and satisfaction), I would increase no of pts.

Thank you in advance.

Srdjan
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 31

Expert Comment

by:alorentz
Comment Utility
Sounds like the error is in Javascript...any server side errors would not show page at all, just stack trace.

Post if you like..
0
 

Author Comment

by:JoshFink
Comment Utility
The code for  SelectDate1.aspx:

<%@ Page Inherits="MyCodeBehind" Src="SelectDate1.vb" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Text"%>

some HTML code for CSS and other stufff

<FORM ACTION="SelectDate1.aspx" METHOD="post" NAME="frmInput" runat="server">

      <asp:dropdownlist id="ddlSelect" runat="server" DataTextField="ReportDate" />
      </asp:dropdownlist>
      
      <input type="button" value="Submit Date" runat="server" onclick="ddl_SelectDate"/>
      
      <asp:DataGrid ID="dgrDamageReport" Runat="server" />
      
      
      </form>

            </body>
            
            </html>
            
            <script language="vb" runat="server">
            
            Sub Page_Load ()
            
      'This procedure will write all available dates to <dropdown> box for user selection      
            
            Dim strSQL as String = "SELECT ReportDate FROM ReportTest;"
            
            Dim strConnection As String= "user id=sa;password=konik;" & _
            "database=DamageReport;server=localhost;" & _
            "Connect Timeout=30"
       
        Dim objDataSet As New DataSet()
        Dim objconnection As New SqlConnection(strConnection)
       
        'create a new DataAdapter
       
        Dim objDataAdapter As New SqlDataAdapter(strSQL, objConnection)
       
        'fill the dataset with data from DataAdapter object
       
        objDataAdapter.Fill(objDataSet, "ReportTest")
       
        'create a DataView object fro the ReportTest table in the DataSet
       
        Dim objDataView As New DataView(objDataSet.Tables("ReportTest"))
       
        'assign the DataView object to the DataGrid control
       
        ddlSelect.DataSource = objDataView
            ddlSelect.DataBind()
        ddlSelect.Items.Insert(0, new ListItem("None", 0))
            
            End Sub
   
            </script>


The Code for SelectDate1.vb file:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class MyCodeBehind : Inherits Page

      'This procedure pull all info for selected date
            
            Public dgrDamageReport As DataGrid
       
        Protected WithEvents ddlSelect As System.Web.UI.WebControls.Dropdownlist
         
                Public Sub ddl_SelectDate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSelect.SelectedIndexChanged
            
            
      'SQL to pull all info under selected date
      
                Dim fdate as String = ddlSelect.SelectedItem.Value
            
                  Dim strSQL1 as String = "SELECT EquipmentType,ReportDate,Person,EmployeeNo,FormCompleted,Location,CompletedBy,Description FROM ReportTest where ReportDate = '" & fdate & "';"
                  
                  Dim strConnection1 As String = "user id=sa;password=kroger;" & _
                  "database=DamageReport;server=localhost;" & _
                  "Connect Timeout=30"
              
                  Dim objDataSet1 As New DataSet()
                  Dim objconnection1 As New SqlConnection(strConnection1)
              
            'create a new DataAdapter
              
                  Dim objDataAdapter1 As New SqlDataAdapter(strSQL1, objConnection1)
              
            'fill the dataset with data from DataAdapter object
              
                  objDataAdapter1.Fill(objDataSet1, "ReportTest")
              
            'create a DataView object for the ReportTest table in the DataSet
              
                  Dim objDataView1 As New DataView(objDataSet1.Tables("ReportTest"))
              
            'assign the DataView object to the DataGrid control
              
                  dgrDamageReport.DataSource = objDataView1
                  dgrDamageReport.DataBind()
       
       
        End Sub
End Class


I hope you will be able to notice my mistake.

Thanks
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
If you're using code behind then you don't need any of this in the page:

<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Text"%>

It already imports them in the CodeBehind!!!
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
And this line:

Public Class MyCodeBehind : Inherits Page


Should be:

Public Class MyCodeBehind
0
 

Author Comment

by:JoshFink
Comment Utility
I know that CodeBehind file should import those 3 lines from above, but if I remove them, I get following error:

Compiler Error Message: BC30002: Type 'DataSet' is not defined.

Source Error:

Line 56:             "Connect Timeout=30"
Line 57:        
Line 58:         Dim objDataSet As New DataSet()
Line 59:         Dim objconnection As New SqlConnection(strConnection)
Line 60:        

If I delete : Inherits Page from Public Class line, I get following error:

Parser Error
Parser Error Message: 'MyCodeBehind' is not a valid base class because it does not extend class 'System.Web.UI.Page'.

Source Error:


Line 1:  <%@ Page Inherits="MyCodeBehind" Src="SelectDate1.vb" %>
Line 2:  <%@ import Namespace="System.Data" %>
Line 3:  <%@ import Namespace="System.Data.SqlClient" %>

Wierd, ha?

Any suggestions?
 

 
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
No, not weird, your code is all messed up...

You're mixing INLINE and CODEBEHIND.  Stick with all code behind.  You do not need onclick event anymore, the codebehind handles that:

<input type="button" value="Submit Date" runat="server" onclick="ddl_SelectDate"/>   '<-----------WRONG

Should be:
<input type="button" value="Submit Date" runat="server"/>

There shouldn't be any Sub in the INLINE.  And you CODEBEHIND should start like this:


Example...

HTML:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" Inherits="face"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>default</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
            <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:DropDownList id="ddlSelect" runat="server"></asp:DropDownList>
            </form>
      </body>
</HTML>

CODEBEHIND:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class face
    Inherits System.Web.UI.Page
    Protected WithEvents ddlSelect As System.Web.UI.WebControls.DropDownList


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    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
        Dim i
        For i = 0 To 1000
            dd.Items.Add(i)
        Next
    End Sub


    Private Sub ddlSelect_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSelect.SelectedIndexChanged

    End Sub
End Class

'''''''''''''''''''''''''''''''''''''''''''''''''''''''
You'd be better to just start with a new page, what you have is all screwed up!



0
 

Author Comment

by:JoshFink
Comment Utility
Alorentz,

I will start with a new page as you recommended.

I have a Wrox - "Beginning ASP.NET 1.0 with VB.NET" book that I use to learn basic stuff for ASP.NET. This book has a lot of short and simple examples, which are very helpful to learn basic stuff.

Can you recommend me some other book that has more complicated and advanced examples and goes deeper into ASP.NET?

Also, can you recommend me a good book for SQL 2000 DB ?

Thanks
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
I don't buy books...all the information is online, and tons of code samples.  Just have to look around...
0
 

Author Comment

by:JoshFink
Comment Utility
Alorentz,

I started with a new page as you recommended. I put my HTML lines in .aspx file, and the rest of code in code behind file.

When trying to access the .aspx page by browser I get the folowing error:

Parser Error Message: Could not load type 'Tutorial.Global'.

Source Error:
Line 1:  <%@ Application Codebehind="Global.asax.vb" Inherits="Tutorial.Global" %>

I checked my Tutorial folder and there are 3 Global files: Global.asax, Global.asax.resx, Global.asax.vb, so I am not sure why it could not load type 'Tutorial.Global'.

Maybe I am missing something between code behind and aspx files.

BTW, This is the first line of aspx file:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="SelectDate3.aspx.vb" Inherits="Tutorial.SelectDate3"%>

Thanks

Srdjan


 
0
 

Author Comment

by:JoshFink
Comment Utility
I just noticed that I get the same error when opening any of the files from Tutorial folder

Interesting too, if I open files from an other folder, for all of them I get

Parser Error Message: The XML declaration is unexpected. Line 2, position 3.

Source Error:


Line 1:  <!--@##session ##web.config##-->
Line 2:  <?xml version="1.0" encoding="utf-8" ?>
Line 3:  <configuration>
Line 4:      
 
Few hours ago I removed and installed back IIS and .Framework 1.1. Maybe something loaded wrong.
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
Sorry dude, you're all messed up...I don't know where to begin.

Visual Studio.Net basically builds the page for you with drag drop...no errors...so you're doing something wrong.  Find a .Net tutorial an read that.
0
 

Author Comment

by:JoshFink
Comment Utility
Alorentz,

I finally found some time to get back to ASP.NET.
I had to re instal VS and ASP.NET 1.1 and to tweak options for creation of web aplication using VS.

Your first/original answer worked fine.

Thanks
0
 
LVL 31

Expert Comment

by:alorentz
Comment Utility
Thought so...good luck!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
T-SQL Debugging - Temp Object Content 2 48
CSS Question.. 3 72
Messagebox for a Web Site Application 4 59
dialer.exe 4 51
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now