PDA

View Full Version : DropDownList - SelectedItem ???


reverendleo
06-06-2005, 08:11 PM
Hello again, all!

I have a DropDownList that pulls its items from a table in MS Access:

dim SQL as string
SQL = "SELECT JobName FROM tblSWPPPBookInfo ORDER BY JobName;"
conClsf.Open()
dim cmdClsf as OleDbCommand = new OleDbCommand(SQL,conClsf)
DropDownList1.DataSource = cmdClsf.ExecuteReader(CommandBehavior.CloseConnection)
'DropDownList1.DataTextField = "JobName"
DropDownList1.DataValueField = "JobName"
DropDownList1.DataBind()
DropDownList1.DataSource.Close
cmdClsf.Dispose
conClsf.Close

A button on my form should initiate the following:

Dim strSQL as string = "SELECT * FROM tblSWPPPBookInfo WHERE JobName = '" & DropDownList1.SelectedValue & "';"

My problem lies in that regardless of which item I pick from the DropDownList, it invariably chooses the first item in the list to plug into the above SQL statement. I've also tried

....& DropDownList1.SelectedItem.Value & .....

with the same results. Any ideas? Thanks, as always!

reverendleo
06-06-2005, 09:29 PM
UPDATE:

I've also tried ....SelectedItem.Text .......same result.

I'm thinking it has to do with when the button is clicked, it posts the form back, reseting the drop down list to the first entry. Here's the code for the whole page, if anyone's interested.

Thanks!!

miranda
06-07-2005, 05:19 AM
This is because you haven't looked for ispostback in the page load. So the dropdownlist is rebuilt with the click of the button. So to solve the problem just do this.

Sub Page_Load(sender As Object, e As EventArgs)
If Not(Page.IsPostBack) Then
dim SQL as string = "SELECT JobName FROM tblSWPPPBookInfo ORDER BY JobName;"
conClsf.Open()
dim cmdClsf as OleDbCommand = new OleDbCommand(SQL,conClsf)
DropDownList1.DataSource = cmdClsf.ExecuteReader(CommandBehavior.CloseConnection)
DropDownList1.DataTextField = "JobName"
DropDownList1.DataValueField = "JobName"
DropDownList1.DataBind()
DropDownList1.DataSource.Close
cmdClsf.Dispose
conClsf.Close
End If
cmdDelRec.Attributes.Add("onclick", _
"return confirm('Are you sure you want to delete?');")
Label1.Font.Bold = True
End Sub


The proper way to get the value of this control is
DropDownList1.SelectedItem.Value

reverendleo
06-08-2005, 02:22 PM
Thanks Miranda! On the ball as usual, you are.

I tried to basically copy the last page you assisted me with in writing a new page that will work similarly. I'm having a slight issue, though. My SaveRec command is giving me an "Input String was not in correct format" error.
Here's the code:

Function SJ(ByVal JobName as String, ByVal City as String, ByVal ST as String, ByVal County as String, ByVal DT as Date, _
ByVal PermitNum as String, ByVal ProjManName as String, ByVal Title as String, ByVal Inspector as String, _
ByVal InspQual as String, ByVal TypeofWork as String, ByVal DescriptionofActivity as String, _
ByVal Act1 as String, ByVal Act1StartDate as String, ByVal Act1EndDate as String, _
ByVal Act2 as String, ByVal Act2StartDate as String, ByVal Act2EndDate as String, _
ByVal Act3 as String, ByVal Act3StartDate as String, ByVal Act3EndDate as String, _
ByVal Act4 as String, ByVal Act4StartDate as String, ByVal Act4EndDate as String, _
ByVal Act5 as String, ByVal Act5StartDate as String, ByVal Act5EndDate as String, _
ByVal Act6 as String, ByVal Act6StartDate as String, ByVal Act6EndDate as String, _
ByVal Act7 as String, ByVal Act7StartDate as String, ByVal Act7EndDate as String, _
ByVal Act8 as String, ByVal Act8StartDate as String, ByVal Act8EndDate as String, _
ByVal TotalArea as Single, ByVal AreatobeDisturbed as Single, ByVal AreaOffSite as Single, _
ByVal ExVeg1 as String, ByVal ExVeg1Den as Single, ByVal ExVeg1Cov as Single, _
ByVal ExVeg2 as String, ByVal ExVeg2Den as Single, ByVal ExVeg2Cov as Single, _
ByVal ExVeg3 as String, ByVal ExVeg3Den as Single, ByVal ExVeg3Cov as Single, _
ByVal ExVeg4 as String, ByVal ExVeg4Den as Single, ByVal ExVeg4Cov as Single, _
ByVal ExVeg5 as String, ByVal ExVeg5Den as Single, ByVal ExVeg5Cov as Single, _
ByVal SoilType1 as String, ByVal SoilType1EroFac as Single, ByVal SoilType1UniCla as String, ByVal SoilType1Cov as Single, _
ByVal SoilType2 as String, ByVal SoilType2EroFac as Single, ByVal SoilType2UniCla as String, ByVal SoilType2Cov as Single, _
ByVal SoilType3 as String, ByVal SoilType3EroFac as Single, ByVal SoilType3UniCla as String, ByVal SoilType3Cov as Single, _
ByVal SoilType4 as String, ByVal SoilType4EroFac as Single, ByVal SoilType4UniCla as String, ByVal SoilType4Cov as Single, _
ByVal SoilType5 as String, ByVal SoilType5EroFac as Single, ByVal SoilType5UniCla as String, ByVal SoilType5Cov as Single, _
ByVal PreConERC as Single, ByVal PostConERC as Single, ByVal ExAreasofErosion as String, ByVal RecWaters as String, _
ByVal ExStormSewerSys as String, ByVal NatDrainWays as String, ByVal TempPlantLoc as String, ByVal PostConSWMC as String) as integer
Dim SQL As String = "UPDATE tblSWPPPBookInfo SET JobName = '" & JobName & "', City = '" & City & "', " & _
"State = '" & ST & "', County = '" & County & "', Date = '" & DT & "', " & _
"PermitNum = '" & PermitNum & "', ProjManName = '" & ProjManName & "', Title = '" & Title & "', " & _
"Inspector = '" & Inspector & "', InspQual = '" & InspQual & "', TypeofWork = '" & TypeofWork & "', DescriptionofActivity = '" & DescriptionofActivity & "', " & _
"Act1 = '" & Act1 & "', Act1StartDate = '" & Act1StartDate & "', Act1EndDate = '" & Act1EndDate & "', " & _
"Act2 = '" & Act2 & "', Act2StartDate = '" & Act2StartDate & "', Act2EndDate = '" & Act2EndDate & "', " & _
"Act3 = '" & Act3 & "', Act3StartDate = '" & Act3StartDate & "', Act3EndDate = '" & Act3EndDate & "', " & _
"Act4 = '" & Act4 & "', Act4StartDate = '" & Act4StartDate & "', Act4EndDate = '" & Act4EndDate & "', " & _
"Act5 = '" & Act5 & "', Act5StartDate = '" & Act5StartDate & "', Act5EndDate = '" & Act5EndDate & "', " & _
"Act6 = '" & Act6 & "', Act6StartDate = '" & Act6StartDate & "', Act6EndDate = '" & Act6EndDate & "', " & _
"Act7 = '" & Act7 & "', Act7StartDate = '" & Act7StartDate & "', Act7EndDate = '" & Act7EndDate & "', " & _
"Act8 = '" & Act8 & "', Act8StartDate = '" & Act8StartDate & "', Act8EndDate = '" & Act8EndDate & "', " & _
"TotalArea = " & TotalArea & ", AreatobeDisturbed = " & AreatobeDisturbed & ", AreaOffSite = " & AreaOffSite & ", " & _
"ExVeg1 = '" & ExVeg1 & "', ExVeg1Den = " & ExVeg1Den & ", ExVeg1Cov = " & ExVeg1Cov & ", " & _
"ExVeg2 = '" & ExVeg2 & "', ExVeg2Den = " & ExVeg2Den & ", ExVeg2Cov = " & ExVeg2Cov & ", " & _
"ExVeg3 = '" & ExVeg3 & "', ExVeg3Den = " & ExVeg3Den & ", ExVeg3Cov = " & ExVeg3Cov & ", " & _
"ExVeg4 = '" & ExVeg4 & "', ExVeg4Den = " & ExVeg4Den & ", ExVeg4Cov = " & ExVeg4Cov & ", " & _
"ExVeg5 = '" & ExVeg5 & "', ExVeg5Den = " & ExVeg5Den & ", ExVeg5Cov = " & ExVeg5Cov & ", " & _
"SoilType1 = '" & SoilType1 & "', SoilType1EroFac = " & SoilType1EroFac & ", SoilType1UniCla = '" & SoilType1UniCla & "', SoilType1Cov = " & SoilType1Cov & ", " & _
"SoilType2 = '" & SoilType2 & "', SoilType2EroFac = " & SoilType2EroFac & ", SoilType2UniCla = '" & SoilType2UniCla & "', SoilType2Cov = " & SoilType2Cov & ", " & _
"SoilType3 = '" & SoilType3 & "', SoilType3EroFac = " & SoilType3EroFac & ", SoilType3UniCla = '" & SoilType3UniCla & "', SoilType3Cov = " & SoilType3Cov & ", " & _
"SoilType4 = '" & SoilType4 & "', SoilType4EroFac = " & SoilType4EroFac & ", SoilType4UniCla = '" & SoilType4UniCla & "', SoilType4Cov = " & SoilType4Cov & ", " & _
"SoilType5 = '" & SoilType5 & "', SoilType5EroFac = " & SoilType5EroFac & ", SoilType5UniCla = '" & SoilType5UniCla & "', SoilType5Cov = " & SoilType5Cov & ", " & _
"PreConERC = " & PreConERC & ", PostConERC = " & PostConERC & ", ExAreasofErosion = '" & ExAreasofErosion & "', " & _
"RecWaters = '" & RecWaters & "', ExStormSewerSys = '" & ExStormSewerSys & "', NatDrainWays = '" & NatDrainWays & "', " & _
"TempPlantLoc = '" & TempPlantLoc & "', PostConSWMC = '" & PostConSWMC & "' " & _
"WHERE (JobName = '" & JobName & "');"

Dim dbCommand As IDbCommand = New OleDbCommand(SQL, conClsf)

Dim rowsAffected As Integer = 0
conClsf.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Catch Exc as Exception
Label1.Text = SQL & "<br /><br />"
Label1.Text += Exc.ToString()
Finally
conClsf.Close
End Try

Return rowsAffected
End Function

And the actual sub run by the click:

Sub SaveJob(sender As Object, e As EventArgs)
If SJ(txtJobName.text, txtCity.text, txtState.text, txtCounty.SelectedItem.text, txtDate.text, txtPermitNum.text, txtProjManName.text, _
txtTitle.text, txtInspector.text, txtInspQual.text, txtTypeofWork.text, txtDescriptionofActivity.text, _
txtAct1.text, txtAct1StartDate.text, txtAct1EndDate.text, txtAct2.text, txtAct2StartDate.text, txtAct2EndDate.text, _
txtAct3.text, txtAct3StartDate.text, txtAct3EndDate.text, txtAct4.text, txtAct4StartDate.text, txtAct4EndDate.text, _
txtAct5.text, txtAct5StartDate.text, txtAct5EndDate.text, txtAct6.text, txtAct6StartDate.text, txtAct6EndDate.text, _
txtAct7.text, txtAct7StartDate.text, txtAct7EndDate.text, txtAct8.text, txtAct8StartDate.text, txtAct8EndDate.text, _
txtTotalArea.text, txtAreatobeDisturbed.text, txtAreaOffSite.text, _
txtExVeg1.text, txtExVeg1Den.text, txtExVeg1Cov.text, txtExVeg2.text, txtExVeg2Den.text, txtExVeg2Cov.text, _
txtExVeg3.text, txtExVeg3Den.text, txtExVeg3Cov.text, txtExVeg4.text, txtExVeg4Den.text, txtExVeg4Cov.text, _
txtExVeg5.text, txtExVeg5Den.text, txtExVeg5Cov.text, _
txtSoilType1.text, txtSoilType1EroFac.text, txtSoilType1UniCla.text, txtSoilType1Cov.text, _
txtSoilType2.text, txtSoilType2EroFac.text, txtSoilType2UniCla.text, txtSoilType2Cov.text, _
txtSoilType3.text, txtSoilType3EroFac.text, txtSoilType3UniCla.text, txtSoilType3Cov.text, _
txtSoilType4.text, txtSoilType4EroFac.text, txtSoilType4UniCla.text, txtSoilType4Cov.text, _
txtSoilType5.text, txtSoilType5EroFac.text, txtSoilType5UniCla.text, txtSoilType5Cov.text, _
txtPreConERC.text, txtPostConERC.text, txtExAreasofErosion.text, txtRecWaters.text, txtExStormSewerSys.text, _
txtNatDrainWays.text, txtTempPlantLoc.text, txtPostConSWMC.text) > 0 Then
LookupJob(txtJobName.text)
Label1.Text = txtJobName.text & " has been updated"
Else
Label1.Text = txtJobName.text & " has NOT been updated"
End If
End Sub

There are a ton of fields, and I thought I had it formatted correctly, but obviously I didn't. Can you see the mistake?
Thanks, as always, you are a life-saver.

miranda
06-08-2005, 04:26 PM
I am guessing it is the use of the reserved word date in your table. Try enclosing the column name in square brackets in your select statement like so, [date] Then let me know how that works for you.

Dim SQL As String = "UPDATE tblSWPPPBookInfo SET JobName = '" & JobName & "', City = '" & City & "', " & _
"State = '" & ST & "', County = '" & County & "', [Date] = '" & DT & "', " & _
"PermitNum = '" & PermitNum & "', ProjManName = '" & ProjManName & "', Title = '" & Title & "', " & _
"Inspector = '" & Inspector & "', InspQual = '" & InspQual & "', TypeofWork = '" & TypeofWork & "', DescriptionofActivity = '" & DescriptionofActivity & "', " & _
"Act1 = '" & Act1 & "', Act1StartDate = '" & Act1StartDate & "', Act1EndDate = '" & Act1EndDate & "', " & _
"Act2 = '" & Act2 & "', Act2StartDate = '" & Act2StartDate & "', Act2EndDate = '" & Act2EndDate & "', " & _
"Act3 = '" & Act3 & "', Act3StartDate = '" & Act3StartDate & "', Act3EndDate = '" & Act3EndDate & "', " & _
"Act4 = '" & Act4 & "', Act4StartDate = '" & Act4StartDate & "', Act4EndDate = '" & Act4EndDate & "', " & _
"Act5 = '" & Act5 & "', Act5StartDate = '" & Act5StartDate & "', Act5EndDate = '" & Act5EndDate & "', " & _
"Act6 = '" & Act6 & "', Act6StartDate = '" & Act6StartDate & "', Act6EndDate = '" & Act6EndDate & "', " & _
"Act7 = '" & Act7 & "', Act7StartDate = '" & Act7StartDate & "', Act7EndDate = '" & Act7EndDate & "', " & _
"Act8 = '" & Act8 & "', Act8StartDate = '" & Act8StartDate & "', Act8EndDate = '" & Act8EndDate & "', " & _
"TotalArea = " & TotalArea & ", AreatobeDisturbed = " & AreatobeDisturbed & ", AreaOffSite = " & AreaOffSite & ", " & _
"ExVeg1 = '" & ExVeg1 & "', ExVeg1Den = " & ExVeg1Den & ", ExVeg1Cov = " & ExVeg1Cov & ", " & _
"ExVeg2 = '" & ExVeg2 & "', ExVeg2Den = " & ExVeg2Den & ", ExVeg2Cov = " & ExVeg2Cov & ", " & _
"ExVeg3 = '" & ExVeg3 & "', ExVeg3Den = " & ExVeg3Den & ", ExVeg3Cov = " & ExVeg3Cov & ", " & _
"ExVeg4 = '" & ExVeg4 & "', ExVeg4Den = " & ExVeg4Den & ", ExVeg4Cov = " & ExVeg4Cov & ", " & _
"ExVeg5 = '" & ExVeg5 & "', ExVeg5Den = " & ExVeg5Den & ", ExVeg5Cov = " & ExVeg5Cov & ", " & _
"SoilType1 = '" & SoilType1 & "', SoilType1EroFac = " & SoilType1EroFac & ", SoilType1UniCla = '" & SoilType1UniCla & "', SoilType1Cov = " & SoilType1Cov & ", " & _
"SoilType2 = '" & SoilType2 & "', SoilType2EroFac = " & SoilType2EroFac & ", SoilType2UniCla = '" & SoilType2UniCla & "', SoilType2Cov = " & SoilType2Cov & ", " & _
"SoilType3 = '" & SoilType3 & "', SoilType3EroFac = " & SoilType3EroFac & ", SoilType3UniCla = '" & SoilType3UniCla & "', SoilType3Cov = " & SoilType3Cov & ", " & _
"SoilType4 = '" & SoilType4 & "', SoilType4EroFac = " & SoilType4EroFac & ", SoilType4UniCla = '" & SoilType4UniCla & "', SoilType4Cov = " & SoilType4Cov & ", " & _
"SoilType5 = '" & SoilType5 & "', SoilType5EroFac = " & SoilType5EroFac & ", SoilType5UniCla = '" & SoilType5UniCla & "', SoilType5Cov = " & SoilType5Cov & ", " & _
"PreConERC = " & PreConERC & ", PostConERC = " & PostConERC & ", ExAreasofErosion = '" & ExAreasofErosion & "', " & _
"RecWaters = '" & RecWaters & "', ExStormSewerSys = '" & ExStormSewerSys & "', NatDrainWays = '" & NatDrainWays & "', " & _
"TempPlantLoc = '" & TempPlantLoc & "', PostConSWMC = '" & PostConSWMC & "' " & _
"WHERE (JobName = '" & JobName & "');"

BTW when you are filling in a form and you have a dropdownlist that you want to selct the value of the data in your database you would use
if ((rdrMbrs.Item("County")) is System.DBNull.Value) then
'txtCounty.SelectedIndex = 0 'sets the list to the 1st row
else
'using the GetString() method of the DataReader assuming county is the 4th column in the database
'txtCounty.SelectedIndex = txtCounty.Items.IndexOf(txtCounty.Items.FindByText(rdrMbrs.GetString(3)))
txtCounty.SelectedIndex = txtCounty.Items.IndexOf(txtCounty.Items.FindByText(rdrMbrs.Item("County")))
end if

reverendleo
06-08-2005, 07:08 PM
Added the [] around [Date] - no dice.

Still getting the same "Input String was not in a correct format error" somewhere in the following lines:

Line 626:Sub SaveJob(sender As Object, e As EventArgs)
Line 627: If SJ(txtJobName.text, txtCity.text, txtState.text, txtCounty.SelectedItem.text, txtDate.text, txtPermitNum.text, txtProjManName.text, _

Maybe it's that txtCounty DropDownList - but I'm not sure how to adjust it.

You're the best!!!!!

miranda
06-08-2005, 07:36 PM
txtCounty.SelectedItem.VALUE

reverendleo
06-08-2005, 07:52 PM
The same error still haunts :(

Just so I don't feel TOO stupid, if the field is a number, you don't include single quotes around the values in the SQL statement, right?

I've attached a copy of the database, if there's something in there that might provide a clue. I'm stumped.

You, however, are a hero!

Thanks!

miranda
06-08-2005, 08:54 PM
Did you see this part of the error?
Stack Trace:


[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat) +164
Microsoft.VisualBasic.CompilerServices.SingleType.FromString(String Value, NumberFormatInfo NumberFormat) +84

[InvalidCastException: Cast from string "" to type 'Single' is not valid.]
Microsoft.VisualBasic.CompilerServices.SingleType.FromString(String Value, NumberFormatInfo NumberFormat) +179
Microsoft.VisualBasic.CompilerServices.SingleType.FromString(String Value) +7
ASP.swppp_aspx.SaveJob(Object sender, EventArgs e) in C:\Inetpub\wwwroot\swppp.aspx:541
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +83
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1281



You have multiple cases where you are trying to do this. Each of the following requires a value and cannot be left blank (txtExVeg1Den.text, txtExVeg1Cov.text, txtExVeg2Den.text, txtExVeg2Cov.text, txtExVeg3Den.text, txtExVeg3Cov.text, txtExVeg4Den.text, txtExVeg4Cov.text, txtExVeg5Den.text, txtExVeg5Cov.text, txtSoilType1EroFac.text, txtSoilType1Cov.text, txtSoilType2EroFac.text, txtSoilType2Cov.text, txtSoilType3EroFac.text, txtSoilType3Cov.text, txtSoilType4EroFac.text, txtSoilType4Cov.text, txtSoilType5EroFac.text)

You could change all of the datatypes in the database from single to text, then you will have to alter the insert statement. Or if you leave the database structure as is you could enter a value maybe a 0(zero) into each of the empty number fields.

Without changing the datatypes in the database the correct way to do it without adding a zero is to look at the fields and build your your function call and your SQL UPDATE statement according to which fields are used.

reverendleo
06-08-2005, 09:55 PM
You're right, as usual.

The problem is this ultimately will be going to a Word merge document, with several tables that don't necessarily have to have every field filled. I'll go back and change the number fields to text, and that should fix it! I'll let you know, and thanks again!

reverendleo
06-08-2005, 10:07 PM
Sure Enough!

That fixed it. I'll just have to leave instructions on the form so I don't get wierd dates and weird coefficients. :p

Thanks again and again, I'm sure I'll be back soon with another predicament!

(forgive the spelling of predicament....not sure about that one)

miranda
06-08-2005, 10:12 PM
Oh I have a bit of troubles at times myself. You have just posted questions that I have been able to answer. Right now I have an authorization issue on a site I am working on that is causing me problems. That and trying to get everything to look good cross browser with CSS. I delayed learning asp.Net for a while and stuck with classic asp. Then even did some work in PHP before bothering with asp.net Even though I did a desktop app in vb.net for one of my clients.

BTW do you use an IDE? If so may I ask which one?

reverendleo
06-09-2005, 03:23 PM
I'm using Dreamweaver for the moment. I really like it, and it has a lot of built in items for .asp, ColdFusion, .php, and just about everything else. It even helps automate setting up a testing server for .asp. I sort of got thrown into all this with nothing more than a semester of VB back in college (5 years ago). I've learned a ton just in the last 2 months, and with people like you around, it sure does make the learning curve a lot easier to overcome. Thanks again for all the help! :thumbsup:

B. Drake from the Lone Star State