Showing posts with label run. Show all posts
Showing posts with label run. Show all posts

Saturday, March 24, 2012

Populate Drop Down from SQL

Welp, here is a simple one. I'm trying to teach myself asp.net and run into problems here and there...

If I'm not doing this the most efficiant way let me know!

I have a SQL table that contains a list of names, this list may change from time to time, and I plan to have another form to update this list when I get a bit more educated and start fine tuning(:->)

Anyhow with that in mind...I want a drop down list to display items from this SQL database, keeping in mind that I want to post value to another sql table...Once again I may be doing this totally the wrong way, but hey I'm learning!

So what I've managed to do is create a connect, and read the data into a datatable. I read it into the datatable, then bound it to a datagrid so I could see that I actually did it... and it worked!! yippie!!
so now I've removed the data table, and am attempting to use the same type connection to itterate(??) through a for each loop to populate the items in my dropdown list from the data table...

ok blah blah blah huh..here is the code I have, and I'm stuck at how to itterate and display the data, keeping in mind that I want this dropdown to post the text in the dropdown. I may have lots of extras I don't need in there also..hey I'm a newbie at this!
Thanks
Josh

------code--------
Private Sub DropDownManagers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownManagers.SelectedIndexChanged

Dim ConnStr As String
Dim SQL As String
Dim MySqlConn As New SqlConnection(ConnStr)
Dim FinDataSet As New DataSet
Dim FinDataTable As DataTable
Dim FinDataRow As DataRow
Dim FinDataColumn As DataColumn
Dim MySqlAdapter As New SqlDataAdapter(SQL, ConnStr)
Dim MySqlCB As New SqlCommandBuilder(MySqlAdapter)
ConnStr = "server=(local);database=ProjDms; " & _
"Trusted_Connection=yes"
SQL = "SELECT * FROM FinManagers"
FinDataSet.ReadXmlSchema(Server.MapPath("Managers.xsd"))

'Fill the dataset from SQL
MySqlAdapter.Fill(FinDataSet)

For Each FinDataRow In FinDataTable.Rows
'HERE IS MY PROBLEM AS FAR AS I KNOW
Next
End SubYou don't have to iterate. You can databind the dropdownlist to the data directly, provided you tell it which fields are the text and value fields...
I guess I have some more reading to do huh ?? :->


For Each FinDataRow In FinDataTable.Rows
ddlname.Items.Add(new listItem(FinDataRow("FieldName").ToString,FinDataRow("FieldName").ToString) )
Next

or
not iterating


ddlname.DataSource=FinDataTable
ddlname.DataTextField=FinDataTable.Columns("fieldName").ToString
ddlname.DataValueField=FinDataTable.Columns("fieldName").ToString
ddlname.DataBind

Got it working.

I didn't use a direct databind to the sql to populate as I couldn't figure it out...or at least it was easier to figure out how to do it binding to a dataset, as the samples in the book I bought only show binding other types of things to data sets rather than directly to sql, and as I'm a newb, the information about binding the dropdown to the dataset provided in the previous post made the task much easier.

Thanks for the help!!!!!
Problem number 1 solved :-
Josh

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!

Friday, March 16, 2012

Populating DIV tag from javascript via ASP.Net & ClientScript methods

I'm trying to run some clientside javascript on page load for one of my
ASP.Net pages. I am using the ClientScript.RegisterClient...() methods
to register an external javascript file within ASP.Net. When I put my
code on a ASP button and click it it works just find, runs my
javascript which popus up an alert and then changes the div text.
However, when I put this on a Page_Load event the alert comes up ok but
the changing of the div text does not occur. I have a feeling this has
something to do with not all the page elements being rendered or
avaiable in some way to the javascript. I've tried placing the code in
LoadComplete, and Init, with the same results. Any suggestions on how I
can get my javascript code to be triggered on page load from ASP and be
able to access page elements like div tags? I don't have my code in
front of me now but could come up with example if needed. Any advice is
appreciated.

David Mathew
mathewda@dotnet.itags.org.gmail.comusing masterpages? if so, it changes the client ID

Dave Mathew wrote:

Quote:

Originally Posted by

I'm trying to run some clientside javascript on page load for one of my
ASP.Net pages. I am using the ClientScript.RegisterClient...() methods
to register an external javascript file within ASP.Net. When I put my
code on a ASP button and click it it works just find, runs my
javascript which popus up an alert and then changes the div text.
However, when I put this on a Page_Load event the alert comes up ok but
the changing of the div text does not occur. I have a feeling this has
something to do with not all the page elements being rendered or
avaiable in some way to the javascript. I've tried placing the code in
LoadComplete, and Init, with the same results. Any suggestions on how I
can get my javascript code to be triggered on page load from ASP and be
able to access page elements like div tags? I don't have my code in
front of me now but could come up with example if needed. Any advice is
appreciated.
>
David Mathew
mathewda@.gmail.com


I think what you are running into is the javascript code being executed
before the elements of your page have been created in the page DOM.
Try to stick your javascript into the onload attribute of your body tag
and see if it will work.

If this does work, I think what you will have to do is register your
javascript with the onload event either by adding it as an attribute of
the body tag or via external script.

http://www.quirksmode.org/js/events_advanced.html
The above article is similar to what you could use to register your
javascript with the load event except of course the event you want to
register for is load (onload in IE) and I believe you want to use
window as your element.

I wrote a javascript register function that handles cross browser
registration a while ago but don't have it with me. Let me know if you
want it and I'll post it. I believe there are also similar examples
out there somewhere on the web.

Clint.

Dave Mathew wrote:

Quote:

Originally Posted by

I'm trying to run some clientside javascript on page load for one of my
ASP.Net pages. I am using the ClientScript.RegisterClient...() methods
to register an external javascript file within ASP.Net. When I put my
code on a ASP button and click it it works just find, runs my
javascript which popus up an alert and then changes the div text.
However, when I put this on a Page_Load event the alert comes up ok but
the changing of the div text does not occur. I have a feeling this has
something to do with not all the page elements being rendered or
avaiable in some way to the javascript. I've tried placing the code in
LoadComplete, and Init, with the same results. Any suggestions on how I
can get my javascript code to be triggered on page load from ASP and be
able to access page elements like div tags? I don't have my code in
front of me now but could come up with example if needed. Any advice is
appreciated.
>
David Mathew
mathewda@.gmail.com