Dropdownlist

  • Thread starter Frederik Vanderhaeghe
  • Start date
F

Frederik Vanderhaeghe

Hi,

I have a dropdownlist that contains should contain several values from the
database, I fill it up in de page_load() with this code:
If Not Page.IsPostBack Then

InlezenConfig()

Dim strsqlDocType As String = "select distinct type from
TBL_Bestanden_Zoeken"

Dim da As New SqlClient.SqlDataAdapter(strsqlDocType, connectie)

Try

connectie.Open()

da.Fill(ds, "doctype")

ddlDocType.DataSource = ds.Tables("doctype")

ddlDocType.DataBind()

Catch ex As Exception

Finally

connectie.Close()

ddlDocType.Items.Insert(0, "Selecteer een type")

ddlDocType.SelectedIndex = 0

End Try

End If

lblZoeken.Visible = False

dtgZoekResultaten.Visible = True

The code also adds a standard value, 'Selecteer een type' that should be
added at the beginning of the list. The problem now is that when i start my
application only the value 'Selecteer uw type' is in the dropdownlist, but
the other values from the database should also be in it. How can i fix
this???

Fré
 
P

Pipo

Can you add the standard value (Selecteer een type) in the dataset(table)
with Rows.InsertAt maybe and then databind the dropdown?
 
F

Frederik Vanderhaeghe

I already fixed the problem, but i have another :-(

When I choose an item from the dropdownlist, an i press search, the selected
item changes into the first index. How can I resolve this?

Fré
 
G

Guest

Hi,

Have you set the property EnableViewState to true on the drop down list
control? If you have what other code have you got, there must be something
setting it back to 0.

Regards
Wozza
 
F

Frederik Vanderhaeghe

Yes, but that is because the selected item when loaded must be the one at
index 0. But when changed it mus be the chosen one.
 
F

Frederik Vanderhaeghe

It is standard set to true, i didn't change it. All my code is:
Dim connectie As New SqlClient.SqlConnection("Server=" + getsqlserver() +
";Initial Catalog=" + getsqlcatalog() + ";Trusted_Connection=True")

Dim strSQL As String

Dim ds As New DataSet

Dim whereSQL As String = "where "

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

If Not Page.IsPostBack Then

InlezenConfig()

End If

Dim strsqlDocType As String = "select distinct type from
TBL_Bestanden_Zoeken"

Dim da As New SqlClient.SqlDataAdapter(strsqlDocType, connectie)

da.Fill(ds, "doctype")

Dim row As DataRow

row = ds.Tables("doctype").NewRow

row("Type") = "Selecteer een type"

ds.Tables("doctype").Rows.Add(row)

ddlDocType.DataSource = ds.Tables("doctype")

ddlDocType.DataBind()

ddlDocType.SelectedIndex = ds.Tables("doctype").Rows.Count - 1

lblZoeken.Visible = False

dtgZoekResultaten.Visible = True

End Sub

'Knop Velden leegmaken => alle zoekvelden leegmaken of niets geselecteerd

Private Sub btnreset_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnreset.Click

Me.txtdocbedrag.Text = ""

Me.txtdocdatumVan.Text = ""

Me.txtdocdatumTot.Text = ""

Me.txtdocnr.Text = ""

Me.txtklantnaam.Text = ""

Me.txtklantnr.Text = ""

End Sub

Private Sub btnzoeken_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnzoeken.Click

Dim i As New Integer

'documentnummer ingevuld

If Not (txtdocnr.Text = "") Then

whereSQL += "docnr like '" & txtdocnr.Text & "' and "

End If

'klantnummer ingevuld

If Not (txtklantnr.Text = "") Then

whereSQL += "klnr like '" & txtklantnr.Text & "' and "

End If

'klantnaam ingevuld

If Not (txtklantnaam.Text = "") Then

Dim klantnaam As String = Trim(txtklantnaam.Text)

Do While (InStr(klantnaam, " ") > 0)

klantnaam = Microsoft.VisualBasic.Left(klantnaam, InStr(klantnaam,
InStr(klantnaam, " ") - 1)) + _

Microsoft.VisualBasic.Right(klantnaam, InStr(klantnaam, Len(klantnaam) -
InStr(klantnaam, " ")))

Loop

If Microsoft.VisualBasic.Right(klantnaam, 1).Equals("*") Then

If Microsoft.VisualBasic.Left(klantnaam, 1).Equals("*") Then

whereSQL += "klnaam like '%" & klantnaam & "%' or klnaam like '%" &
txtklantnaam.Text & "%' and "

Else

whereSQL += "klnaam like '" & klantnaam & "%' or klnaam like '" &
txtklantnaam.Text & "%' and "

End If

ElseIf Microsoft.VisualBasic.Left(klantnaam, 1).Equals("*") Then

whereSQL += "klnaam like '%" & klantnaam & "' or klnaam like '%" &
txtklantnaam.Text & "' and "

Else

whereSQL += "klnaam like '" & txtklantnaam.Text & "' or klnaam like '" &
klantnaam & "' and "

End If

End If

'document datum ingevuld

If Not (txtdocdatumTot.Text = "") Then

If Not (txtdocdatumVan.Text = "") Then

whereSQL += "convert(datetime,datum,101) between '" & txtdocdatumVan.Text &
"' and '" & CDate(txtdocdatumTot.Text) & "' and "

Else

lblDatum.ForeColor = System.Drawing.Color.Red

End If

ElseIf Not (txtdocdatumVan.Text = "") Then

whereSQL += "convert(datetime,datum,101) = '" & CDate(txtdocdatumVan.Text) &
"' and "

End If

'document bedrag ingevuld

If Not (txtdocbedrag.Text = "") Then

Select Case ddlBedrag.SelectedIndex

Case 0

Case 1

strSQL += "bedrag > '" & Double.Parse(txtdocbedrag.Text) & "' and "

Case 2

strSQL += "bedrag >= '" & Double.Parse(txtdocbedrag.Text) & "' and "

Case 3

strSQL += "bedrag < '" & Double.Parse(txtdocbedrag.Text) & "' and "

Case 4

strSQL += "bedrag <= '" & Double.Parse(txtdocbedrag.Text) & "' and "

Case 5

strSQL += "bedrag = '" & Double.Parse(txtdocbedrag.Text) & "' and "

Case 6

strSQL += "bedrag >= '" & Double.Parse(txtdocbedrag.Text) & "' and bedrag <
'" & Double.Parse(txtBedragTot.Text) & "' and "

End Select

End If

'documenttype ingevuld

i = 0

'While (i < ds.Tables("doctype").Rows.Count)

'If (ddlDocType.SelectedIndex = i) Then

whereSQL += "type like '" & ddlDocType.SelectedValue & "' and "

'i = ds.Tables("doctype").Rows.Count

'End If

'i += 1

'End While

'laatste " and " van de string knippen

whereSQL = Microsoft.VisualBasic.Left(whereSQL, whereSQL.Length - 5)

'strSQL maken

strSQL = "select Id, docnr, klnr, klnaam, datum, bedrag, type from
TBL_Bestanden_Zoeken " & whereSQL & " order by docnr"

If ((txtdocnr.Text = "") And (txtklantnr.Text = "") And (txtklantnaam.Text =
"") And (txtdocdatumVan.Text = "") And (txtdocdatumTot.Text = "") And
(txtdocbedrag.Text = "") And (txtBedragTot.Text = "") And Not
(ddlDocType.SelectedIndex = 0)) Then

lblZoeken.Visible = True

strSQL = ""

dtgZoekResultaten.Visible = False

Else

'dataset opvullen

Dim da2 As New SqlClient.SqlDataAdapter(strSQL, connectie)

da2.Fill(ds, "bestanden")

If (ds.Tables("bestanden").Rows.Count <> 0) Then

dtgZoekResultaten.DataSource = ds.Tables("bestanden").DefaultView

DataBind()

Else

lblZoeken.Text = "Er is niets gevonden"

lblZoeken.Visible = True

dtgZoekResultaten.Visible = False

End If

End If

End Sub

Private Sub dtgZoekResultaten_PageIndexChanged(ByVal source As
System.Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
dtgZoekResultaten.PageIndexChanged

dtgZoekResultaten.CurrentPageIndex = e.NewPageIndex

dtgZoekResultaten.DataBind()

End Sub

End Class
 
P

Pipo

You change the selectedIndex yourself:
ddlDocType.SelectedIndex = ds.Tables("doctype").Rows.Count - 1
 
P

Pipo

Try this:

Dim connectie As New SqlClient.SqlConnection("Server=" + getsqlserver() +
";Initial Catalog=" + getsqlcatalog() + ";Trusted_Connection=True")

Dim strSQL As String
Dim ds As New DataSet
Dim whereSQL As String = "where "

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

If Not Page.IsPostBack Then

InlezenConfig()

Dim strsqlDocType As String = "select distinct type from
TBL_Bestanden_Zoeken"

Dim da As New SqlClient.SqlDataAdapter(strsqlDocType, connectie)

da.Fill(ds, "doctype")

Dim row As DataRow

row = ds.Tables("doctype").NewRow

row("Type") = "Selecteer een type"

ds.Tables("doctype").Rows.Add(row)

ddlDocType.DataSource = ds.Tables("doctype")

ddlDocType.DataBind()

ddlDocType.SelectedIndex = ds.Tables("doctype").Rows.Count - 1

lblZoeken.Visible = False

dtgZoekResultaten.Visible = True

End If

End Sub
 
F

Frederik Vanderhaeghe

Yes, but wher do I have to put it then, so that it doesn't always select the
standard value?
 
F

Frederik Vanderhaeghe

Thank you pipo, it works now!
Pipo said:
Try this:

Dim connectie As New SqlClient.SqlConnection("Server=" + getsqlserver() +
";Initial Catalog=" + getsqlcatalog() + ";Trusted_Connection=True")

Dim strSQL As String
Dim ds As New DataSet
Dim whereSQL As String = "where "

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

If Not Page.IsPostBack Then

InlezenConfig()

Dim strsqlDocType As String = "select distinct type from
TBL_Bestanden_Zoeken"

Dim da As New SqlClient.SqlDataAdapter(strsqlDocType, connectie)

da.Fill(ds, "doctype")

Dim row As DataRow

row = ds.Tables("doctype").NewRow

row("Type") = "Selecteer een type"

ds.Tables("doctype").Rows.Add(row)

ddlDocType.DataSource = ds.Tables("doctype")

ddlDocType.DataBind()

ddlDocType.SelectedIndex = ds.Tables("doctype").Rows.Count - 1

lblZoeken.Visible = False

dtgZoekResultaten.Visible = True

End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top