Showing posts with label below. Show all posts
Showing posts with label below. Show all posts

Saturday, March 24, 2012

populate dropdownlist

I have a list which I am trying to populate from my db, I can my items but not my unique id, my code is below

_cmd.CommandText = "SELECT workgroupID, workGroupname FROM dbo.workgroup"
_dr = _cmd.ExecuteReader



While _dr.Read
workgroupSelect.Items.Add(_dr("workgroupname"))
workgroupSelect.DataValueField = _dr("workgroupid")
End While


How do I get my id field into the list so that I know which one was selected??

How do I know which one was selected?

Grateful for any help.

Cheersok, I now have
With workgroupSelect
.DataSource = _dr
.DataTextField = "workgroupname"
.DataValueField = "workgroupid"
.DataBind()
.Items.Insert(0, "<- Select a Work Group ->")
End With

so all I need now is to know is the workgroupId of the one selected??

Cheers
try :

workgroupSelect.SelectedItem.Value
I have tried that,

I have ..

Protected Sub workgroupSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles workgroupSelect.SelectedIndexChanged
Dim strselected As String
strselected = workgroupSelect.SelectedValue.ToString
Label1.Text = strselected
End Sub

but the only I can get the label text to show the value is to set the dropdowncontrol to postback = true
is there a way around this??
is the value actually changing even if I cant see it in the label??

Thanks
The value would change but there is no way to see it refreshed unless the postback is activated. Such is web development :sick:

Populate form fields

How do you populate individual form fields from a database? I have used datasets in the past but this time I need to do individual form fields.

Below is the code I tried and I know that reader only read 1 parameter and not the way I have it setup now to do multiple parameters.

thanks

1Dim sConnStrAs String = ConfigurationManager.ConnectionStrings("accmon").ConnectionString23'************************45Dim MySqlAs String ="SELECT * FROM privacyscan WHERE privacyscanDelete = 0 and url ='" & privacyAddress & "'"67 Dim reader As OleDb.OleDbDataReader89 Dim MyConn As New OleDbConnection(sConnStr)10 Dim Cmd As New OleDbCommand(MySql, MyConn)1112 MyConn.Open()13 reader = Cmd.ExecuteReader()1415 While reader.Read()16 txtOA.Text = reader("oa")17'txtDate.Text = reader("date")18 'txtURL.Text = reader("url")19 'txtReport.Text = reader("report")20 'txtScan11.Text = reader("chkpt_scan_11")21 'txtPass11.Text = reader("chkpt_pass_11")22 'txtScan14.Text = reader("chkpt_scan_14")23 'txtPass14.Text = reader("chkpt_pass_14")24 'txtScan15.Text = reader("chkpt_scan_15")25 'txtPass15.Text = reader("chkpt_pass_15")26 'txtScan22.Text = reader("chkpt_scan_22")27 'txtPass22.Text = reader("chkpt_pass_22")28 'txtScan31.Text = reader("chkpt_scan_31")29 'txtPass31.Text = reader("chkpt_pass_31")30 'txtScan32.Text = reader("chkpt_scan_32")31 'txtPass32.Text = reader("chkpt_pass_32")32 'txtScan41.Text = reader("chkpt_scan_41")33 'txtPass41.Text = reader("chkpt_pass_41")34 'txtScan41a.Text = reader("chkpt_scan_41a")35 'txtPass41a.Text = reader("chkpt_pass_41a")36 'txtScan41b.Text = reader("chkpt_scan_41b")37 'txtPass41b.Text = reader("chkpt_pass_41b")38 'txtScan42.Text = reader("chkpt_scan_42")39 'txtPass42.Text = reader("chkpt_pass_42")40 'txtScan43.Text = reader("chkpt_scan_43")41 'txtPass43.Text = reader("chkpt_pass_43")42 'txtScan44.Text = reader("chkpt_scan_44")43 'txtPass44.Text = reader("chkpt_pass_44")44 'txtScan71.Text = reader("chkpt_scan_71")45 'txtPass71.Text = reader("chkpt_pass_71")46End While47 reader.Close()48 MyConn.Close()

Well, if you uncomment the lines below then your code should be working fine. However, if your query returns more than one row, your controls will only show the last row returned (since you have a while loop).

Another thing you might want to check is the layout of your table. It doesn't seem very normalized ;-)


What is the problem with the current code? However everytime you need to make sure you are returning only one row.

Wednesday, March 21, 2012

Populating a data grid with a stored procudure w/parameters

All,

I'm trying to populate a datagrid with a data adapter that uses a stored
procedure with a parameter. I get the below error when I run my code
(as seen below). Any hints?

If I delete the .value = "Business Acumen" at the end of the add
parameters statement & put the below code on a different row, the error
goes away, but I get back an empty dataset.

Me.cmdUYP.Parameters("@dotnet.itags.org.web_competency_name").Value = "Business Acumen"

Error Msg:
The SqlParameterCollection only accepts non-null SqlParameter type
objects, not Boolean objects.

My Code:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
Imports System.Text

Public Class UYP_Class
Inherits System.Web.UI.Page
Protected WithEvents btnFLMClose As System.Web.UI.WebControls.Button
Dim previousCat As String
Dim previousComp As String
Protected WithEvents linkIntelU As
System.Web.UI.WebControls.HyperLink
Protected WithEvents Image1 As System.Web.UI.WebControls.Image
Protected WithEvents btnClose As System.Web.UI.WebControls.Button
Protected WithEvents lnkIntelLibrary As
System.Web.UI.WebControls.HyperLink
Protected WithEvents lnkBuyOnline As
System.Web.UI.WebControls.HyperLink
Protected WithEvents conUYP As System.Data.SqlClient.SqlConnection
Protected WithEvents lblError As System.Web.UI.WebControls.Label
Protected WithEvents daUYP As System.Data.SqlClient.SqlDataAdapter
Protected WithEvents cmdUYP As System.Data.SqlClient.SqlCommand
Protected WithEvents DsUYP1 As FDO.dsUYP
Protected WithEvents lnkAllCurric As
System.Web.UI.WebControls.HyperLink
Protected WithEvents lnkHelp As System.Web.UI.WebControls.HyperLink
Protected WithEvents dgUYP As System.Web.UI.WebControls.DataGrid
Protected WithEvents btnExportExcel As
System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.conUYP = New System.Data.SqlClient.SqlConnection()
Me.daUYP = New System.Data.SqlClient.SqlDataAdapter()
Me.cmdUYP = New System.Data.SqlClient.SqlCommand()
Me.DsUYP1 = New FDO.dsUYP()
CType(Me.DsUYP1,
System.ComponentModel.ISupportInitialize).BeginIni t()
'
'conUYP
'
Me.conUYP.ConnectionString = "data source=OREA2SQL017;initial
catalog=Fin_Trng_DB;password=abcd$1234;persist se" & _
"curity info=True;user id=FinTrngUserGrp"
'
'daUYP
'
Me.daUYP.SelectCommand = Me.cmdUYP
'
'cmdUYP
'
Me.cmdUYP.CommandText = "dbo.[prc_uyp_curriculum]"
Me.cmdUYP.CommandType = System.Data.CommandType.StoredProcedure
Me.cmdUYP.Connection = Me.conUYP
Me.cmdUYP.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@dotnet.itags.org.web_competency_name",
System.Data.SqlDbType.NVarChar, 50).Value = "Business Acumen")

'
'DsUYP1
'
Me.DsUYP1.DataSetName = "dsUYP"
Me.DsUYP1.Locale = New System.Globalization.CultureInfo("en-US")
Me.DsUYP1.Namespace = "http://www.tempuri.org/dsUYP.xsd"
CType(Me.DsUYP1,
System.ComponentModel.ISupportInitialize).EndInit( )

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
'Put user code to initialize the page here
Try
'Me.cmdUYP.Parameters("web_competency_name").Value =
"Business Acumen"
daUYP.Fill(DsUYP1, "prc_uyp_curriculum")
If Not IsPostBack Then
dgUYP.DataSource =
DsUYP1._dbo_prc_uyp_curriculum.DefaultView()
dgUYP.DataBind()
End If
End Sub
End Class

Machelle Chandler
Intel Corporation
Beginning .NET developer
Thanks in advance for the help!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!Hi Machelle. Try changing this
Me.cmdUYP.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@.web_competency_name",
System.Data.SqlDbType.NVarChar, 50).Value = "Business Acumen")

for
Me.cmdUYP.Parameters.Add("@.web_competency_name",
System.Data.SqlDbType.NVarChar, 50).Value = "Business Acumen";

"Machelle Chandler" <machelle.a.chandler@.intel.com> wrote in message
news:#fF2buClDHA.2232@.TK2MSFTNGP09.phx.gbl...
> All,
> I'm trying to populate a datagrid with a data adapter that uses a stored
> procedure with a parameter. I get the below error when I run my code
> (as seen below). Any hints?
> If I delete the .value = "Business Acumen" at the end of the add
> parameters statement & put the below code on a different row, the error
> goes away, but I get back an empty dataset.
> Me.cmdUYP.Parameters("@.web_competency_name").Value = "Business Acumen"
> Error Msg:
> The SqlParameterCollection only accepts non-null SqlParameter type
> objects, not Boolean objects.
>
> My Code:
> Imports System
> Imports System.Data
> Imports System.Data.SqlClient
> Imports System.Text.RegularExpressions
> Imports System.Text
>
> Public Class UYP_Class
> Inherits System.Web.UI.Page
> Protected WithEvents btnFLMClose As System.Web.UI.WebControls.Button
> Dim previousCat As String
> Dim previousComp As String
> Protected WithEvents linkIntelU As
> System.Web.UI.WebControls.HyperLink
> Protected WithEvents Image1 As System.Web.UI.WebControls.Image
> Protected WithEvents btnClose As System.Web.UI.WebControls.Button
> Protected WithEvents lnkIntelLibrary As
> System.Web.UI.WebControls.HyperLink
> Protected WithEvents lnkBuyOnline As
> System.Web.UI.WebControls.HyperLink
> Protected WithEvents conUYP As System.Data.SqlClient.SqlConnection
> Protected WithEvents lblError As System.Web.UI.WebControls.Label
> Protected WithEvents daUYP As System.Data.SqlClient.SqlDataAdapter
> Protected WithEvents cmdUYP As System.Data.SqlClient.SqlCommand
> Protected WithEvents DsUYP1 As FDO.dsUYP
> Protected WithEvents lnkAllCurric As
> System.Web.UI.WebControls.HyperLink
> Protected WithEvents lnkHelp As System.Web.UI.WebControls.HyperLink
> Protected WithEvents dgUYP As System.Web.UI.WebControls.DataGrid
> Protected WithEvents btnExportExcel As
> System.Web.UI.WebControls.Button
> #Region " Web Form Designer Generated Code "
> 'This call is required by the Web Form Designer.
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
> Me.conUYP = New System.Data.SqlClient.SqlConnection()
> Me.daUYP = New System.Data.SqlClient.SqlDataAdapter()
> Me.cmdUYP = New System.Data.SqlClient.SqlCommand()
> Me.DsUYP1 = New FDO.dsUYP()
> CType(Me.DsUYP1,
> System.ComponentModel.ISupportInitialize).BeginIni t()
> '
> 'conUYP
> '
> Me.conUYP.ConnectionString = "data source=OREA2SQL017;initial
> catalog=Fin_Trng_DB;password=abcd$1234;persist se" & _
> "curity info=True;user id=FinTrngUserGrp"
> '
> 'daUYP
> '
> Me.daUYP.SelectCommand = Me.cmdUYP
> '
> 'cmdUYP
> '
> Me.cmdUYP.CommandText = "dbo.[prc_uyp_curriculum]"
> Me.cmdUYP.CommandType = System.Data.CommandType.StoredProcedure
> Me.cmdUYP.Connection = Me.conUYP
> Me.cmdUYP.Parameters.Add(New
> System.Data.SqlClient.SqlParameter("@.web_competency_name",
> System.Data.SqlDbType.NVarChar, 50).Value = "Business Acumen")
> '
> 'DsUYP1
> '
> Me.DsUYP1.DataSetName = "dsUYP"
> Me.DsUYP1.Locale = New System.Globalization.CultureInfo("en-US")
> Me.DsUYP1.Namespace = "http://www.tempuri.org/dsUYP.xsd"
> CType(Me.DsUYP1,
> System.ComponentModel.ISupportInitialize).EndInit( )
> 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
> 'Put user code to initialize the page here
> Try
> 'Me.cmdUYP.Parameters("web_competency_name").Value =
> "Business Acumen"
> daUYP.Fill(DsUYP1, "prc_uyp_curriculum")
> If Not IsPostBack Then
> dgUYP.DataSource =
> DsUYP1._dbo_prc_uyp_curriculum.DefaultView()
> dgUYP.DataBind()
> End If
> End Sub
> End Class
> Machelle Chandler
> Intel Corporation
> Beginning .NET developer
> Thanks in advance for the help!
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!

Populating a datagrid bound list view by a query

Hi,
My question today is below is my code which binds a listview to a databound column in my datagrid, but at the moment the list items are hard coded in. What I actually want is for the listitems to be populated from a lookup style query so possible from a data reader bringing back the ID which will be stored in the bound column but so the user just sees the Text associated with the value. Hope that makes sense.

<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FK_StockModelID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropModel" Runat="server">
<asp:ListItem Value="1" text="1"></asp:ListItem>
<asp:ListItem Value="2" text="2"></asp:ListItem>
<asp:ListItem Value="3" text="3"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="DropModel" text="*" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>

I think I can pretty much suss this out if I can work out how to access the dropdown list from tthe codebehind. So if anyone knows that as a starting point, please enlighten me.

PS sorry about the smilies in the code the ASP syntax just happent to be the same code.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp
Ok I found this code at the above link but Im struggling to use it, can anyone explain it line by line or tell me how to make it reference my template item shown above.
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim Cu As String = DRV("ShipVia")
Dim DDL As DropDownList = CType(e.Item.Cells(4).Controls(1), DropDownList)()
Dim SQL As String = _
"SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
Dim DS As New DataSet
Dim item As ListItem
DA.Fill(DS, "Shippers")
DDL.DataSource = DS.Tables("Shippers").DefaultView
DDL.DataTextField = "CompanyName"
DDL.DataValueField = "ShipperID"
DDL.DataBind()
item = DDL.Items.FindByValue(CurrentShip)
If Not item Is Nothing Then item.Selected = True
End If
End Sub
Yes, you'd do it in the ItemDataBound event. After checking for the ItemType, declare a variable of type Combobox. Set it to e.Item.Cells(x).FindControl(your combobox id), and set the value of that combobox to the value you want from the dataset. You can use e.Item.ItemIndex to get the row number in the repeater/grid/dataset.
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim cboModel As DropDownList
Dim cboModel As DropDownList = e.Item.Cells(FindControl(DropModel))
'Also I have put DopModel as the ID of the Dropdown list in the item template but it doesnt seem to be recognised? Name DropModel is not declared

End If
End Sub
Fixed

Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")

Now to try the next bit...
Nearly there, I am now just getting an error at run time on the line underlined saying cannot convert dbnull to string?
Private Sub dgNewCarriageBuildSheets_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgNewCarriageBuildSheets.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim lstItem As ListItem
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentModel As String = DRV("FK_StockModelID")
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")
Main.objCarriageBuildSheet.GetStockModels() 'Populates a dataset
cboModel.DataSource = Main.objCarriageBuildSheet.dds_StockModels.dt_StockModels
cboModel.DataTextField = "Model"
cboModel.DataValueField = "FK_StockModelID"
cboModel.DataBind()
lstItem = cboModel.Items.FindByValue(CurrentModel)
If Not lstItem Is Nothing Then lstItem.Selected = True
End If
End Sub
DataBinder.Eval(e.Item.DataItem, "FK_StockModelID")
Like this ?
If e.Item.ItemType = ListItemType.EditItem Then

Dim lstItem As ListItem
Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim CurrentModel As String = DataBinder.Eval(e.Item.DataItem, "FK_StockModelID") 'DRV("FK_StockModelID")
Dim cboModel As DropDownList = e.Item.Cells(11).FindControl("DropModel")

Main.objCarriageBuildSheet.GetStockModels() 'Populates a dataset
cboModel.DataSource = Main.objCarriageBuildSheet.dds_StockModels.dt_StockModels
cboModel.DataTextField = "Model"
cboModel.DataValueField = "FK_StockModelID"
cboModel.DataBind()
lstItem = cboModel.Items.FindByValue(CurrentModel)

If Not lstItem Is Nothing Then lstItem.Selected = True

End If

I still get same error
Hmm... maybe before you call that line, you should check whether e.Item.DataItem evaluates to null... place a breakpoint there and have a look at it.
It evaluates to a system.data.datarowview object
Hi Mendhak, I have finally solved the problem a different way.

My HTML code looks like this now, the main difference is I have set the datasource to a function in my code behind page.
<asp:TemplateColumn HeaderText="Model">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.FK_StockModelID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=DropModel Runat="server" DataValueField="ModelID" DataTextField="BrandModel" DataSource="<%# PopulateList %>" >
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" Runat="server" text="*" ControlToValidate="DropModel"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateColumn>

The function simply returns a previously filled dataset.
Public Function PopulateList() As DataSet

Return Main.objCarriageBuildSheet.dds_StockModels
End Function

this seems to be working nicely.