Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Thursday, March 29, 2012

pop up windows with img and text from...?

using vb.net, with sql 2000 personal edition.

I'm trying to create a portfolio page, the pages would be divided into years from 2001 ~ 2004 with each page a link of projects that my company has done. when the link is clicked it would open a new window with both image and text.

my question is how should i build this pop up page, with over 90 images i do not want to create individual aspx pop up windows, so if i was to get img and text info where should it be coming from? would sql server or is there was a to put this info in the project page? I haven't a clue, plz help.

ps. if you can point me to an example on the net that would be great..

focusA good place to start may be the following tutorial:
A Robust Image Gallery for ASP.NET
wow thanks NewKid2~

i'm still reading it but its a great example, not only for my project but for my personal use as well, also i love the fact that its in vb.net there should be a page called top 10 everyday examples in vb.net for the noobie in you!

focus
> there should be a page called top 10 everyday examples in vb.net

Well, the same guys have written a bunch of ASP.NET articles, most of them using VB.NET, and all written in an easy-to-understand way.

You can see the entire list of their articles here:
http://aspnet.4guysfromrolla.com

Monday, March 26, 2012

Populate a DropDownList

I need to populate a dropdownlist with items form one SQL Table in SqlDataSource1 but the selected value and the data text need to match the ones that are already into another SQL Table in SqlDataSource2.

Any Sugestions?

You can manually compare the data between the tables in codebehind using the classes found in System.Data.SqlClient & System.Data.Sql. Then you can use the DropDownList.Items.Add(new ListItem()) methods to add new items to your dropdownlist.

Populate an array from a sql query?

I think I did something wrong with this. I am trying to create an array of product id's from a database query.


Dim dReader As SqlDataReader
Dim i As Integer = 0
Dim Products(i) As String
Dim strResults As String

conConnection.Open()
Dim cmdCommand As New SqlCommand("SELECT ProductID FROM Products WHERE CategoryID = '4'", conConnection)

dReader = cmdCommand.ExecuteReader()
While dReader.Read
Products(i) = dReader("Subcategory")
i += 1
strResults += dReader("Subcategory")
End While
Label1.Text = strResults 'just to display if I am getting results

conConnection.Close()

I put that StrResults so I can monitor if anything is getting picked up, but nothing happens at all. What did I do wrong?Doing this freehand so might be a little bit off.


Dim myArrayList as new ArrayList

Dim dReader As SqlDataReader

Dim i As Integer = 0

Dim strResults As String

conConnection.Open()

Dim cmdCommand As New SqlCommand("SELECT ProductID FROM Products WHERE CategoryID = '4'", conConnection)

dReader = cmdCommand.ExecuteReader()

While dReader.Read

myArrayList.Add(dReader("Subcategory"))

End While

conConnection.Close()

Dim x as Integer
For x = 0 to myArrayList.Count - 1
labeli.text += myArrayList(x) & " "
Next


Cool, thanks. I also noticed that I have Subcategory as my Datareader field, but CategoryID in my actual query.
 Dim cmdCommand As New SqlCommand("SELECT ProductID FROM Products WHERE CategoryID = '4'", conConnection)

dReader = cmdCommand.ExecuteReader()

While dReader.Read

Products(i) = dReader("Subcategory")

............

What did I do wrong?

-> You forgot to select "Subcategory" in SQL query?
Oh, sorry, didn't see you found it yourself.
Thanks guys. It feels so good when it finally works! :)

populate array with sql table data vb.net

hello,
I wanted to populate an array with the data from sql table, but not
sure how to go about it.
This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.
Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}
Any suggestions how to go about this one. Your time is greatly
appreciated.
cheers, Sharon.Simple example (i assume you have basic knowledge about ADO.NET)
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
End Sub
Private Const ConnectionString As String =
" server=ServerName;uid=UserName;password=
Password;database=DatebaseName"
Private Function GetDates() As System.Collections.Generic.List(Of DateTime)
Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader
Try
connection.Open()
reader = command.ExecuteReader()
While reader.Read()
result.Add(reader.GetDateTime(0))
End While
Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try
Return result
End Function
End Class
Milosz Skalecki
MCP, MCAD
"Sharon" wrote:

> hello,
> I wanted to populate an array with the data from sql table, but not
> sure how to go about it.
> This is the array iam using at present, but i dont want to provide the
> values. Instead i want to query them from sql table t_holidays.
> Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
> #7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}
> Any suggestions how to go about this one. Your time is greatly
> appreciated.
> cheers, Sharon.
>
Thanks for the Reply, But its giving me an error
System.Collections.Generic.List is not defined, Please let me know
where did it went wrong.
cheers, Sharon.
Milosz Skalecki wrote:
> Simple example (i assume you have basic knowledge about ADO.NET)
> Imports System.Data
> Imports System.Data.SqlClient
> Partial Class _Default
> Inherits System.Web.UI.Page
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
> End Sub
> Private Const ConnectionString As String =
> " server=ServerName;uid=UserName;password=
Password;database=DatebaseName"
> Private Function GetDates() As System.Collections.Generic.List(Of DateTim
e)
> Dim result As New System.Collections.Generic.List(Of DateTime)
> Dim connection As New SqlConnection(ConnectionString)
> Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
> Condition", connection)
> Dim reader As SqlDataReader
> Try
> connection.Open()
> reader = command.ExecuteReader()
> While reader.Read()
> result.Add(reader.GetDateTime(0))
> End While
> Catch ex As Exception
> Throw ex
> Finally
> connection.Dispose()
> End Try
> Return result
> End Function
> End Class
>
> --
> Milosz Skalecki
> MCP, MCAD
>
> "Sharon" wrote:
>
I am using .NET Framework version, is this class available in this
version
Milosz Skalecki wrote:
> Simple example (i assume you have basic knowledge about ADO.NET)
> Imports System.Data
> Imports System.Data.SqlClient
> Partial Class _Default
> Inherits System.Web.UI.Page
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
> End Sub
> Private Const ConnectionString As String =
> " server=ServerName;uid=UserName;password=
Password;database=DatebaseName"
> Private Function GetDates() As System.Collections.Generic.List(Of DateTim
e)
> Dim result As New System.Collections.Generic.List(Of DateTime)
> Dim connection As New SqlConnection(ConnectionString)
> Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
> Condition", connection)
> Dim reader As SqlDataReader
> Try
> connection.Open()
> reader = command.ExecuteReader()
> While reader.Read()
> result.Add(reader.GetDateTime(0))
> End While
> Catch ex As Exception
> Throw ex
> Finally
> connection.Dispose()
> End Try
> Return result
> End Function
> End Class
>
> --
> Milosz Skalecki
> MCP, MCAD
>
> "Sharon" wrote:
>
Howdy,
i automatically assumed you were using framework 2.0. Please use
System.Collections.ArrayList instead of
System.Collections.Generic.List(Of DateTime)
regards
Milosz Skalecki
MCP, MCAD
"Sharon" wrote:

> I am using .NET Framework version, is this class available in this
> version
> Milosz Skalecki wrote:
>

populate array with sql table data vb.net

hello,

I wanted to populate an array with the data from sql table, but not
sure how to go about it.

This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.

Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}

Any suggestions how to go about this one. Your time is greatly
appreciated.

cheers, Sharon.Simple example (i assume you have basic knowledge about ADO.NET)

Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()

End Sub

Private Const ConnectionString As String =
"server=ServerName;uid=UserName;password=Password;d atabase=DatebaseName"

Private Function GetDates() As System.Collections.Generic.List(Of DateTime)

Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader

Try

connection.Open()
reader = command.ExecuteReader()

While reader.Read()
result.Add(reader.GetDateTime(0))
End While

Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try

Return result

End Function

End Class

--
Milosz Skalecki
MCP, MCAD

"Sharon" wrote:

Quote:

Originally Posted by

hello,
>
I wanted to populate an array with the data from sql table, but not
sure how to go about it.
>
This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.
>
Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}
>
Any suggestions how to go about this one. Your time is greatly
appreciated.
>
cheers, Sharon.
>
>


Thanks for the Reply, But its giving me an error

System.Collections.Generic.List is not defined, Please let me know
where did it went wrong.

cheers, Sharon.

Milosz Skalecki wrote:

Quote:

Originally Posted by

Simple example (i assume you have basic knowledge about ADO.NET)
>
Imports System.Data
Imports System.Data.SqlClient
>
Partial Class _Default
Inherits System.Web.UI.Page
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
>
Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
>
End Sub
>
Private Const ConnectionString As String =
"server=ServerName;uid=UserName;password=Password;d atabase=DatebaseName"
>
Private Function GetDates() As System.Collections.Generic.List(Of DateTime)
>
Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader
>
Try
>
connection.Open()
reader = command.ExecuteReader()
>
While reader.Read()
result.Add(reader.GetDateTime(0))
End While
>
Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try
>
Return result
>
End Function
>
End Class
>
>
--
Milosz Skalecki
MCP, MCAD
>
>
"Sharon" wrote:
>

Quote:

Originally Posted by

hello,

I wanted to populate an array with the data from sql table, but not
sure how to go about it.

This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.

Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}

Any suggestions how to go about this one. Your time is greatly
appreciated.

cheers, Sharon.


I am using .NET Framework version, is this class available in this
version
Milosz Skalecki wrote:

Quote:

Originally Posted by

Simple example (i assume you have basic knowledge about ADO.NET)
>
Imports System.Data
Imports System.Data.SqlClient
>
Partial Class _Default
Inherits System.Web.UI.Page
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
>
Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
>
End Sub
>
Private Const ConnectionString As String =
"server=ServerName;uid=UserName;password=Password;d atabase=DatebaseName"
>
Private Function GetDates() As System.Collections.Generic.List(Of DateTime)
>
Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader
>
Try
>
connection.Open()
reader = command.ExecuteReader()
>
While reader.Read()
result.Add(reader.GetDateTime(0))
End While
>
Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try
>
Return result
>
End Function
>
End Class
>
>
--
Milosz Skalecki
MCP, MCAD
>
>
"Sharon" wrote:
>

Quote:

Originally Posted by

hello,

I wanted to populate an array with the data from sql table, but not
sure how to go about it.

This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.

Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}

Any suggestions how to go about this one. Your time is greatly
appreciated.

cheers, Sharon.


Howdy,

i automatically assumed you were using framework 2.0. Please use
System.Collections.ArrayList instead of
System.Collections.Generic.List(Of DateTime)

regards

--
Milosz Skalecki
MCP, MCAD

"Sharon" wrote:

Quote:

Originally Posted by

I am using .NET Framework version, is this class available in this
version
Milosz Skalecki wrote:

Quote:

Originally Posted by

Simple example (i assume you have basic knowledge about ADO.NET)

Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()

End Sub

Private Const ConnectionString As String =
"server=ServerName;uid=UserName;password=Password;d atabase=DatebaseName"

Private Function GetDates() As System.Collections.Generic.List(Of DateTime)

Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader

Try

connection.Open()
reader = command.ExecuteReader()

While reader.Read()
result.Add(reader.GetDateTime(0))
End While

Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try

Return result

End Function

End Class

--
Milosz Skalecki
MCP, MCAD

"Sharon" wrote:

Quote:

Originally Posted by

hello,
>
I wanted to populate an array with the data from sql table, but not
sure how to go about it.
>
This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.
>
Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}
>
Any suggestions how to go about this one. Your time is greatly
appreciated.
>
cheers, Sharon.
>
>


>
>

populate datagrid

Hello,

Here is the part of my code, I need to add a datagrid and populate is from
this sql string. Can you write the rest of the code?

Dim Da As New OdbcDataAdapter
Dim Ds As New DataSet
Dim cmdSelect As New OdbcCommand

cmdSelect = Conn.CreateCommand
cmdSelect.CommandText = "SELECT * FROM myTable"

Thanks,
Jim.Hope this helps;
<code
/* ### In Web.config BEGIN ### */
<configuration
<appSettings>
<add key="IntranetConnStr_Core" value="Data
Source=<DBServerName_Here>;user
id=<UserID_Here>;password=<Password_Here>;initial catalog=<DB_Here>" />
</appSettings
/* ### In Web.config END ### */

/** Calling code BEGIN **/
Dim dvEventStatuss As New DataView
'Note! strSQLToRun = I read the SQL CMD in from a XLM file.
dvEventStatuss = Do_Get.Do_DBConnection("TBL_EventStatuss", strSQLToRun,
ConfigurationSettings.AppSettings(strConnStr), (strWhoCalledMe &
".Do_DBGridInit"), objVisitor)
With uwgOrderEventStatuss
.DataSource = dvEventStatuss
.DataBind()
end with
/** Calling code BEGIN **/

Public Function Do_DBConnection(ByVal strTBLName As String, ByVal strSQL As
String, ByVal strConnStrToUse As String, ByVal strWhoCalledMe As String,
ByVal objVisitor As clsSiteFunctions.clsUser) As DataView
Dim dvDataView As New DataView
Dim dsDataSet As New DataSet
Dim Connection As New SqlConnection

Try
strConnStrToUse = "IntranetConnStr_Core"
strSQL = "Select * FROM <TableName_Here>"
strTBLName = "TBL_TableName"

Connection.ConnectionString =
ConfigurationSettings.AppSettings(strConnStrToUse)
Dim Adapter As New SqlDataAdapter(strSQL, Connection)
Adapter.Fill(dsDataSet, strTBLName)
Connection.Open()
dvDataView.Table = dsDataSet.Tables(strTBLName)
Adapter.Dispose()
Connection.Close()
Connection.Dispose()
Catch

End Try
Do_DBConnection = dvDataView
End Function

</code
"JIM.H." wrote:

> Hello,
> Here is the part of my code, I need to add a datagrid and populate is from
> this sql string. Can you write the rest of the code?
> Dim Da As New OdbcDataAdapter
> Dim Ds As New DataSet
> Dim cmdSelect As New OdbcCommand
> cmdSelect = Conn.CreateCommand
> cmdSelect.CommandText = "SELECT * FROM myTable"
>
> Thanks,
> Jim.

populate datagrid

Hello,
Here is the part of my code, I need to add a datagrid and populate is from
this sql string. Can you write the rest of the code?
Dim Da As New OdbcDataAdapter
Dim Ds As New DataSet
Dim cmdSelect As New OdbcCommand
cmdSelect = Conn.CreateCommand
cmdSelect.CommandText = "SELECT * FROM myTable"
Thanks,
Jim.Hope this helps;
<code>
/* ### In Web.config BEGIN ### */
<configuration>
<appSettings>
<add key="IntranetConnStr_Core" value="Data
Source=<DBServerName_Here>;user
id=<UserID_Here>;password=<Password_Here>;initial catalog=<DB_Here>" />
</appSettings>
/* ### In Web.config END ### */
/** Calling code BEGIN **/
Dim dvEventStatuss As New DataView
'Note! strSQLToRun = I read the SQL CMD in from a XLM file.
dvEventStatuss = Do_Get.Do_DBConnection("TBL_EventStatuss", strSQLToRun,
ConfigurationSettings.AppSettings(strConnStr), (strWhoCalledMe &
".Do_DBGridInit"), objVisitor)
With uwgOrderEventStatuss
.DataSource = dvEventStatuss
.DataBind()
end with
/** Calling code BEGIN **/
Public Function Do_DBConnection(ByVal strTBLName As String, ByVal strSQL As
String, ByVal strConnStrToUse As String, ByVal strWhoCalledMe As String,
ByVal objVisitor As clsSiteFunctions.clsUser) As DataView
Dim dvDataView As New DataView
Dim dsDataSet As New DataSet
Dim Connection As New SqlConnection
Try
strConnStrToUse = "IntranetConnStr_Core"
strSQL = "Select * FROM <TableName_Here>"
strTBLName = "TBL_TableName"
Connection.ConnectionString =
ConfigurationSettings.AppSettings(strConnStrToUse)
Dim Adapter As New SqlDataAdapter(strSQL, Connection)
Adapter.Fill(dsDataSet, strTBLName)
Connection.Open()
dvDataView.Table = dsDataSet.Tables(strTBLName)
Adapter.Dispose()
Connection.Close()
Connection.Dispose()
Catch
End Try
Do_DBConnection = dvDataView
End Function
</code>
"JIM.H." wrote:

> Hello,
> Here is the part of my code, I need to add a datagrid and populate is from
> this sql string. Can you write the rest of the code?
> Dim Da As New OdbcDataAdapter
> Dim Ds As New DataSet
> Dim cmdSelect As New OdbcCommand
> cmdSelect = Conn.CreateCommand
> cmdSelect.CommandText = "SELECT * FROM myTable"
>
> Thanks,
> Jim.
>

Populate Database from Arraylist

I would like to know how to Populate the SQL Database column using the Arraylist

I'm sure its gonna be INSERT...one example i need

INSERT INTO tempPrice(price) VALUES ( "...............")

How should i declare Arraylist in it? I have a list of values inside the arraylist and i just want the entire column to be filled with it.

Thanks.

HTM

Unfortunately there is no direct way to do this. You have to execute each sql query for each row.

Regards

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

Populate drop down list with sql query (contains IF)

I have a drop down list and it is to get it's 'text' and 'value' from a table. The table contains 3 columns:
Title, Name, Id1, Id2
if Id1 is NOT blank then the 'text' value of the drop down list should be:
Name + "(" + Id1 + ")"
else
Name + "(" + Id2 + ")"
I believe the easiest way is to construct the above IF statement is to use SQL query, then pass the parameter to the DataTextField property. However, i seemed to be getting an error. Also any help on the SQL IF statement will be appreciated...
This is what i have so far...(without the IF statement as i'm not sure how to construct the IF statement in SQL)
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack Then
dropDownList.DataSource = ToGetDataTable()
dropDownList.DataValueField = "MyNewColumn"
dropDownList.DataTextField = "Name"
dropDownList.DataBind()
End if
End Sub

Function ToGetDataTable() As System.Data.IDataReader
Dim connectionString As String = "server='(local)';..................."
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT Title, Name + ' ( ' + Id1 + ')' AS MyNewColumn FROM MyTableName"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Do you have sufficient rights on your SQL Server box to create auser-defined function? That seems to be the best way to go aboutthis; you would simply select Title, Name (which is a terrible name fora database field), and MyFunction(Name, Id1, Id2) where MyFunctionwould do the work.
If you don't have this option, you could pull all necessary columns,put them into a DataTable, and then run through it, appending theappropriate values in one particular column.

Populate Dropdown from SQL Server

Hello everyone!
I'm trying to populate a dropdown list from SQL Server but it's not
being populated. I've tried all sorts of suggestions and examples that
I found from Google but I just can not get it to work. I am able to
access the database from other pages and display a list of records, but
I just can't get the data to go into the dd list.
Below is my code:
ASPX
<%@dotnet.itags.org. Page Language="VB" AutoEventWireup="false"
CodeFile="addApplication.aspx.vb" Inherits="addApplication" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlAgency" runat="server"
DataTextField="agName" DataValueField="agID" />
</div>
</form>
</body>
</html>
VB (code behind)
Partial Class addApplication
Inherits System.Web.UI.Page
Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
'Create a connection string
Dim conStr As String =
ConfigurationManager.ConnectionStrings("connString").ConnectionString
' connString is defined in web.config and works on other pages within
the website project
'Open a connection
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(conStr)
objConnection.Open()
'Specify the SQL string
Dim strSQL As String = "SELECT * FROM tblAgency;"
'Create a command object
Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
'Get a datareader
Dim objDataReader As OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConnection)
ddlAgency.DataSource = objDataReader
ddlAgency.DataBind()
'Close the datareader/db connection
objDataReader.Close()
End Sub
End Class
Is there something I'm doing wrong here?
thanks,
JerryWith Me.ddlSendDepartment
.DataSource = tblScreen
.DataTextField = "DepartmentName"
.DataValueField = "DepartmentID"
. DataBind()
End With
"Jerry" <jerryalan@.gmail.com> wrote in message
news:1147987219.226284.39470@.j55g2000cwa.googlegroups.com...
> Hello everyone!
> I'm trying to populate a dropdown list from SQL Server but it's not
> being populated. I've tried all sorts of suggestions and examples that
> I found from Google but I just can not get it to work. I am able to
> access the database from other pages and display a list of records, but
> I just can't get the data to go into the dd list.
> Below is my code:
> ASPX
> <%@. Page Language="VB" AutoEventWireup="false"
> CodeFile="addApplication.aspx.vb" Inherits="addApplication" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
> <title>Untitled Page</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <asp:DropDownList ID="ddlAgency" runat="server"
> DataTextField="agName" DataValueField="agID" />
> </div>
> </form>
> </body>
> </html>
> VB (code behind)
> Partial Class addApplication
> Inherits System.Web.UI.Page
> Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
> 'Create a connection string
> Dim conStr As String =
> ConfigurationManager.ConnectionStrings("connString").ConnectionString
> ' connString is defined in web.config and works on other pages within
> the website project
> 'Open a connection
> Dim objConnection As OleDbConnection
> objConnection = New OleDbConnection(conStr)
> objConnection.Open()
> 'Specify the SQL string
> Dim strSQL As String = "SELECT * FROM tblAgency;"
> 'Create a command object
> Dim objCommand As OleDbCommand
> objCommand = New OleDbCommand(strSQL, objConnection)
> 'Get a datareader
> Dim objDataReader As OleDbDataReader
> objDataReader =
> objCommand.ExecuteReader(CommandBehavior.CloseConnection)
> ddlAgency.DataSource = objDataReader
> ddlAgency.DataBind()
> 'Close the datareader/db connection
> objDataReader.Close()
> End Sub
> End Class
> Is there something I'm doing wrong here?
> thanks,
> --
> Jerry
>
Thanks for the reply Jeff. I tried your suggestion but I wasn't
successfull.
With Me.ddlAgency
.DataSource = objDataReader
.DataTextField = "agName"
.DataValueField = "agID"
.DataBind()
End With
'ddlAgency.DataSource = objDataReader
'ddlAgency.DataBind()
Did I do it wrong?
Jerry
Doh! I caught the mistake. I had the code in the click event handler
and not the page load handler.
Thanks Jeff, your suggestion does work.
Jerry

Populate Dropdown from SQL Server

Hello everyone!

I'm trying to populate a dropdown list from SQL Server but it's not
being populated. I've tried all sorts of suggestions and examples that
I found from Google but I just can not get it to work. I am able to
access the database from other pages and display a list of records, but
I just can't get the data to go into the dd list.

Below is my code:

ASPX
<%@dotnet.itags.org. Page Language="VB" AutoEventWireup="false"
CodeFile="addApplication.aspx.vb" Inherits="addApplication" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlAgency" runat="server"
DataTextField="agName" DataValueField="agID" />
</div>
</form>
</body>
</html
VB (code behind)

Partial Class addApplication
Inherits System.Web.UI.Page

Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
'Create a connection string

Dim conStr As String =
ConfigurationManager.ConnectionStrings("connString").ConnectionString

' connString is defined in web.config and works on other pages within
the website project

'Open a connection
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(conStr)
objConnection.Open()

'Specify the SQL string
Dim strSQL As String = "SELECT * FROM tblAgency;"

'Create a command object
Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

'Get a datareader
Dim objDataReader As OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)

ddlAgency.DataSource = objDataReader
ddlAgency.DataBind()

'Close the datareader/db connection
objDataReader.Close()
End Sub
End Class

Is there something I'm doing wrong here?

thanks,

--
JerryWith Me.ddlSendDepartment

.DataSource = tblScreen

.DataTextField = "DepartmentName"

.DataValueField = "DepartmentID"

.. DataBind()

End With

"Jerry" <jerryalan@.gmail.com> wrote in message
news:1147987219.226284.39470@.j55g2000cwa.googlegro ups.com...
> Hello everyone!
> I'm trying to populate a dropdown list from SQL Server but it's not
> being populated. I've tried all sorts of suggestions and examples that
> I found from Google but I just can not get it to work. I am able to
> access the database from other pages and display a list of records, but
> I just can't get the data to go into the dd list.
> Below is my code:
> ASPX
> <%@. Page Language="VB" AutoEventWireup="false"
> CodeFile="addApplication.aspx.vb" Inherits="addApplication" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
> <title>Untitled Page</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <asp:DropDownList ID="ddlAgency" runat="server"
> DataTextField="agName" DataValueField="agID" />
> </div>
> </form>
> </body>
> </html>
> VB (code behind)
> Partial Class addApplication
> Inherits System.Web.UI.Page
> Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
> 'Create a connection string
> Dim conStr As String =
> ConfigurationManager.ConnectionStrings("connString").ConnectionString
> ' connString is defined in web.config and works on other pages within
> the website project
> 'Open a connection
> Dim objConnection As OleDbConnection
> objConnection = New OleDbConnection(conStr)
> objConnection.Open()
> 'Specify the SQL string
> Dim strSQL As String = "SELECT * FROM tblAgency;"
> 'Create a command object
> Dim objCommand As OleDbCommand
> objCommand = New OleDbCommand(strSQL, objConnection)
> 'Get a datareader
> Dim objDataReader As OleDbDataReader
> objDataReader =
> objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
> ddlAgency.DataSource = objDataReader
> ddlAgency.DataBind()
> 'Close the datareader/db connection
> objDataReader.Close()
> End Sub
> End Class
> Is there something I'm doing wrong here?
> thanks,
> --
> Jerry
Thanks for the reply Jeff. I tried your suggestion but I wasn't
successfull.

With Me.ddlAgency
.DataSource = objDataReader
.DataTextField = "agName"
.DataValueField = "agID"
.DataBind()
End With

'ddlAgency.DataSource = objDataReader
'ddlAgency.DataBind()

Did I do it wrong?

--
Jerry
Doh! I caught the mistake. I had the code in the click event handler
and not the page load handler.

Thanks Jeff, your suggestion does work.

--
Jerry

populate DropDownList from DB

Hi,
Could someone tell me how can be populated DropdownList from Data Base. I
use ASP.NET/C#/MS SQL SERVER.
Thanks!
Viktor
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.721 / Virus Database: 477 - Release Date: 16.7.2004 a.Victor,
You need to get the database data into a dataset, set the DropdownList
properties DataSource to the dataset name and DataMember to the table name
and call DataBind method.
Eliyahu
"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:OMEUyaGdEHA.212@.TK2MSFTNGP12.phx.gbl...
> Hi,
> Could someone tell me how can be populated DropdownList from Data Base. I
> use ASP.NET/C#/MS SQL SERVER.
> Thanks!
> Viktor
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.721 / Virus Database: 477 - Release Date: 16.7.2004 a.
>
Thanks for the reply!
Could you add the other code?
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
DropDownList2.DataMember = "Table";
DataBind (); // or DropDownList2.DataBind ();
"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:%23QdEA%23HdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> Thanks for the reply!
> Could you add the other code?
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
> conn);
>
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
>
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
>
>
Hi, Thanks again for the reply! I wrote that but it doesn't work. Could you
check for an error?
Thanks
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataBind () ;
}
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
see inline
"Viktor Popov" <viketo@.yahoo.com> wrote in message news:O5PJdnIdEHA.3616@.TK2MSFTNGP10.phx.g
bl...
> Hi, Thanks again for the reply! I wrote that but it doesn't work. Could yo
u
> check for an error?
> Thanks
> private void Page_Load(object sender, System.EventArgs e)
> {
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
> conn);
>
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
> DropDownList2.DataMember = "Table";
> DropDownList2.DataBind () ;
>
add the code (with the correct values):
DropDownList2.DataTextField = "<name of column for visible text>";
DropDownList2.DataValueField = "<name of column for internal value>";

> }
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
>
I tryed to do that:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
conn.Open();
da.SelectCommand.ExecuteReader();
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataBind () ;]
}
but it doesn't work.
What could I do?
Thanks!
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
what's the error?
"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
> I tryed to do that:
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer
",
> conn);
> conn.Open();
> da.SelectCommand.ExecuteReader();
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
> DropDownList2.DataMember = "Table";
> DropDownList2.DataTextField = "TypeOffer";
> DropDownList2.DataValueField = "OfferID";
> DropDownList2.DataBind () ;]
> }
> but it doesn't work.
> What could I do?
> Thanks!
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
>
There is no error. When I start the application the DropDownList is empty.
There are no values, no items. My table is full with data
TypeOffer
--
OfferID TypeOffer
1 rent
2 sell
3 change
Do you know what's wrong?
"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:eahN5iJdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> what's the error?
> "Viktor Popov" <viketo@.yahoo.com> wrote in message
> news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
blek.TypeOffer
> ",
>
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
Can set a breakpoint after the line with da.Fill (ds,"Table"); and check if
the dataset actually gets any data?
"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:%231PE14JdEHA.2544@.TK2MSFTNGP10.phx.gbl...
> There is no error. When I start the application the DropDownList is empty.
> There are no values, no items. My table is full with data
> TypeOffer
> --
> OfferID TypeOffer
> 1 rent
> 2 sell
> 3 change
> Do you know what's wrong?
> "Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
> news:eahN5iJdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> blek.TypeOffer
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
>

populate DropDownList from DB

Hi,
Could someone tell me how can be populated DropdownList from Data Base. I
use ASP.NET/C#/MS SQL SERVER.

Thanks!

Viktor

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.721 / Virus Database: 477 - Release Date: 16.7.2004 a.Victor,

You need to get the database data into a dataset, set the DropdownList
properties DataSource to the dataset name and DataMember to the table name
and call DataBind method.

Eliyahu

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:OMEUyaGdEHA.212@.TK2MSFTNGP12.phx.gbl...
> Hi,
> Could someone tell me how can be populated DropdownList from Data Base. I
> use ASP.NET/C#/MS SQL SERVER.
> Thanks!
> Viktor
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.721 / Virus Database: 477 - Release Date: 16.7.2004 a.
Thanks for the reply!

Could you add the other code?

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");

SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);

DataSet ds = new DataSet();

da.Fill (ds,"Table");

DropDownList2.DataSource = ds;

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
DropDownList2.DataMember = "Table";
DataBind (); // or DropDownList2.DataBind ();

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:%23QdEA%23HdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> Thanks for the reply!
> Could you add the other code?
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
> conn);
>
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
>
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
Hi, Thanks again for the reply! I wrote that but it doesn't work. Could you
check for an error?

Thanks

private void Page_Load(object sender, System.EventArgs e)

{

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");

SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);

DataSet ds = new DataSet();

da.Fill (ds,"Table");

DropDownList2.DataSource = ds;

DropDownList2.DataMember = "Table";

DropDownList2.DataBind () ;

}

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
see inline

"Viktor Popov" <viketo@.yahoo.com> wrote in message news:O5PJdnIdEHA.3616@.TK2MSFTNGP10.phx.gbl...
> Hi, Thanks again for the reply! I wrote that but it doesn't work. Could you
> check for an error?
> Thanks
> private void Page_Load(object sender, System.EventArgs e)
> {
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
> conn);
>
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
> DropDownList2.DataMember = "Table";
> DropDownList2.DataBind () ;

add the code (with the correct values):
DropDownList2.DataTextField = "<name of column for visible text>";
DropDownList2.DataValueField = "<name of column for internal value>";

> }
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
I tryed to do that:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
conn.Open();
da.SelectCommand.ExecuteReader();
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataBind () ;]
}

but it doesn't work.
What could I do?

Thanks!

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
what's the error?

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
> I tryed to do that:
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer
",
> conn);
> conn.Open();
> da.SelectCommand.ExecuteReader();
> DataSet ds = new DataSet();
> da.Fill (ds,"Table");
> DropDownList2.DataSource = ds;
> DropDownList2.DataMember = "Table";
> DropDownList2.DataTextField = "TypeOffer";
> DropDownList2.DataValueField = "OfferID";
> DropDownList2.DataBind () ;]
> }
> but it doesn't work.
> What could I do?
> Thanks!
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
There is no error. When I start the application the DropDownList is empty.
There are no values, no items. My table is full with data
TypeOffer
-------
OfferID TypeOffer
1 rent
2 sell
3 change

Do you know what's wrong?
"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:eahN5iJdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> what's the error?
> "Viktor Popov" <viketo@.yahoo.com> wrote in message
> news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
> > I tryed to do that:
> > private void Page_Load(object sender, System.EventArgs e)
> > {
> > // Put user code to initialize the page here
> > SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> > Catalog=Estate; User ID=blek; Password=banderas");
> > SqlDataAdapter da = new SqlDataAdapter ("Select * FROM
blek.TypeOffer
> ",
> > conn);
> > conn.Open();
> > da.SelectCommand.ExecuteReader();
> > DataSet ds = new DataSet();
> > da.Fill (ds,"Table");
> > DropDownList2.DataSource = ds;
> > DropDownList2.DataMember = "Table";
> > DropDownList2.DataTextField = "TypeOffer";
> > DropDownList2.DataValueField = "OfferID";
> > DropDownList2.DataBind () ;]
> > }
> > but it doesn't work.
> > What could I do?
> > Thanks!
> > --
> > Outgoing mail is certified Virus Free.
> > Checked by AVG anti-virus system (http://www.grisoft.com).
> > Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
Can set a breakpoint after the line with da.Fill (ds,"Table"); and check if
the dataset actually gets any data?

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:%231PE14JdEHA.2544@.TK2MSFTNGP10.phx.gbl...
> There is no error. When I start the application the DropDownList is empty.
> There are no values, no items. My table is full with data
> TypeOffer
> -------
> OfferID TypeOffer
> 1 rent
> 2 sell
> 3 change
> Do you know what's wrong?
> "Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
> news:eahN5iJdEHA.2664@.TK2MSFTNGP09.phx.gbl...
> > what's the error?
> > "Viktor Popov" <viketo@.yahoo.com> wrote in message
> > news:OHTu$fJdEHA.1384@.TK2MSFTNGP10.phx.gbl...
> > > I tryed to do that:
> > > private void Page_Load(object sender, System.EventArgs e)
> > > {
> > > // Put user code to initialize the page here
> > > > SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> > > Catalog=Estate; User ID=blek; Password=banderas");
> > > SqlDataAdapter da = new SqlDataAdapter ("Select * FROM
> blek.TypeOffer
> > ",
> > > conn);
> > > conn.Open();
> > > da.SelectCommand.ExecuteReader();
> > > DataSet ds = new DataSet();
> > > da.Fill (ds,"Table");
> > > DropDownList2.DataSource = ds;
> > > DropDownList2.DataMember = "Table";
> > > DropDownList2.DataTextField = "TypeOffer";
> > > DropDownList2.DataValueField = "OfferID";
> > > DropDownList2.DataBind () ;]
> > > }
> > > > but it doesn't work.
> > > What could I do?
> > > > Thanks!
> > > > > --
> > > Outgoing mail is certified Virus Free.
> > > Checked by AVG anti-virus system (http://www.grisoft.com).
> > > Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
> > >
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
It doesn't work...I'm stucked....
My last code is this. When I RUN it there are no errors but the DropDownList
is not populated and the msg.Text=""

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter dad = new SqlDataAdapter ("SELECT blek.TypeOffer.OfferID,
blek.TypeOffer.TypeOffer FROM blek.TypeOffer", conn);

DataTable table=new DataTable();
conn.Open();
dad.Fill(table);
conn.Close();
DropDownList2.DataSource=table;
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataBind ();
msg.Text = table.Rows.Count.ToString();
}

--
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
DropDownList2.DataSource=table;

try: DropDownList2.DataSource=table.DefaultView;

"Viktor Popov" <viketo@.yahoo.com> wrote in message
news:uAGYoqKdEHA.1692@.tk2msftngp13.phx.gbl...
> It doesn't work...I'm stucked....
> My last code is this. When I RUN it there are no errors but the
DropDownList
> is not populated and the msg.Text=""
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
> Catalog=Estate; User ID=blek; Password=banderas");
> SqlDataAdapter dad = new SqlDataAdapter ("SELECT
blek.TypeOffer.OfferID,
> blek.TypeOffer.TypeOffer FROM blek.TypeOffer", conn);
>
> DataTable table=new DataTable();
> conn.Open();
> dad.Fill(table);
> conn.Close();
> DropDownList2.DataSource=table;
> DropDownList2.DataValueField = "OfferID";
> DropDownList2.DataTextField = "TypeOffer";
> DropDownList2.DataBind ();
> msg.Text = table.Rows.Count.ToString();
> }
>
> --
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.

Populate string array with a SQL row

Can someone tell why I get an "Invalid attempt to read when no data is present. " error?

Here is the code snipplet . . .


SqlCommand DBCommand = new SqlCommand("SELECT * FROM Addresses WHERE AddressID='"+ID.ToString()+"'", DBConn );
try
{
DBConn.Open();
dr = DBCommand.ExecuteReader( System.Data.CommandBehavior.CloseConnection ) ;

}
catch
{
values = null ;
return values ;
}
values[0]=dr["AddressStreet1"].ToString() ;
values[1]=dr["AddressStreet2"].ToString() ;
values[2]=dr["City"].ToString() ;
values[3]=dr["StateProvinceID"].ToString() ;
values[4]=dr["ZipPostalCode"].ToString() ;
values[5]=dr["CountryID"].ToString() ;

Thanks in advance!You need to read the first record of the recordset:

dr.read();
It works except the first column is gone. Why is that?
Disregard my last message - it was my error.

populate text box from sql

when my page opens how i can i auto-populate a text box from a value that is passed from a stored proc?On your Form_Load, grab the data from the stored proc, set your textbox equal to the value it returns.

Wednesday, March 21, 2012

Populating a Drop Down List using a SQL Stored Procedure

Hello All
I posted this question yesterday but I do not see it in the list, and I cannot search and locate it.

I am rather new to coding in ASP.NET. However, I have been coding with Classic ASP for many years. Please be patient with me and my seemingly simple questions

I am in need of some help with Populating a Drop Down List is ASp.NET using C#. I want to be able to populate a drop down list with the ID as the value and the Text as the text shown by calling a SQL stored procedure. If possible, can you give me an example and comment what the steps are.

After much searching yesterday, I found a good example using access. But this is not what I needed. However, it did get me started with the concept.

And another question. How would I code to see the value choosen? My web forms contain mostly drop down list for items such as: state names, postal codes, area codes, dates, times, etc... Just about anything that I can use a DDL, I use it. Saves so much in the error checking.

Thank you in advance for your time.
Andrew
SQL DBATheres a few steps involved in getting data to be present on a page.

First for your application you'll need to create a DataReader object to interact with your SQLCommand which is your stored procedure. (research on how to implement a DataReader)

Then once you have data in your DataReader, you can bind it do the DDL like so:

ddl.DataSource = YourDataReaderName
ddl.DataValueField = "FieldNameofYourChoice" from your DataReader
ddl.DataTextFiled = "FiledNameofYourchoice" from your DataReader
ddl.DataBind

Basically that's it
Thank you
I was hoping to get more of a step by step instruction.
I am having alot of trouble making the database connection. I am seeing so many different types and ways of doing the same thing, that it is getting very confusing. Some inside the aspx page and some in the cs page. I would like to use the code behind cs page.

Any sites out there tell how to do that? And give good examples.

I used to use that same signature many years ago.

Thanks
Andrew

Friday, March 16, 2012

Populating a Dropdownlist from a SQL table

Hello guys, I am trying to populate a ddlist on my form from a sql table, but am having trouble coming up with the code.
Here is what i got so far. Im not sure what goes after the read? Thanks you very much.

Sub GetCauseofLoss()

Dim connAs SqlClient.SqlConnection
Dim cmdAs SqlClient.SqlCommand

Dim drAs SqlClient.SqlDataReader

Dim intFieldAsInteger

conn =New SqlClient.SqlConnection

conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")

cmd = conn.CreateCommand

conn.Open()

cmd.CommandText = "SELECT * FROM CauseOfLoss"

dr = cmd.ExecuteReader

While dr.Read

dgddLossCode.SelectedValue &= vbNewLine

For intField = 0To dr.FieldCount - 1

?????

Next

EndWhile

dr.Close()

conn.Close()

EndSub

' Assume myDDL is an established web control
Dim myDS as DataSet
' Populate DataSet however you want.
Dim currRecord as DataRow
myDDL.Items.Add(new ListItem("", "blank")) ' Just addin
for each currRecord in myDS.Tables(0).Rows
myDDL.Items.add(new ListItem(currRecord(0)))
next
The index in parenthesis after Tables can be replaced with a string of a table name you proved.
The index in parenthesis after currRecord can be replaced with a string of a column name you proved or the name of the column from the database.
I haven't used DataReader before, thus my example above with a DataSet. Looking at the documentation for it, you would do something similar (and in this case, probably easier).

While dr.Read()
dgddLossCode.Items.Add(new ListItem(dr.Item(COLUMN_INDEX_OR_NAME_HERE)) ' assuming that is you DropDownList
End While
The above example is assuming you only want 1 column. If you want multiple columns from the same row, you can name each column individually, or you can iterate through them or use a method of the class. You can find those athttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqldatareadermemberstopic.asp
If you do that, use a String variable and then add the String to the DropDownList


check this

The table Cause of Loss has a Loss _Code, and a Loss_Description. I would need to populate the ddlist with both columns. I also didnt mention that this ddlist is part of a datagrid. Thank you very much guys for your help. Does this look OK?

Sub GetCauseofLoss()

Dim conn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim myDS as DataSet
Dim currRecord as DataRow

Dim dr As SqlClient.SqlDataReader
Dim intField As Integer
conn = New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()

cmd.CommandText = "SELECT * FROM CauseOfLoss"

dr = cmd.ExecuteReader
While dr.Read

for each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Code))
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Description))
next

End While
dr.Close()
conn.Close()

End Sub


Each add call on the dropdownlist.item will add a new list item. What you need to do is similar, but more like
Dim strToAdd as String
While dr.Read
strToAdd = ""
for each currRecord in myDS.Tables(0).Rows
strToAdd &= dr.Item(Loss_Code)
strToAdd &= dr.Item(Loss_Description)
dgddLossCode.Items.Add(strToAdd)
next
End While
You may want to space them somehow, but I'm not sure the best method on that. As for the ddl being in a DataGrid, I'm not sure that would change this part of the code. It's still DropDownList, just encapsulated within a DataGrid.

vin1127 wrote:

Sub GetCauseofLoss()

Dim conn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim myDS as DataSet
Dim currRecord as DataRow

Dim dr As SqlClient.SqlDataReader
Dim intField As Integer
conn = New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()

cmd.CommandText = "SELECT * FROM CauseOfLoss"

dr = cmd.ExecuteReader
While dr.Read

for each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Code))
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Description))
next


Hi, your DataSet was not filled and it is empty and uninstantiated, your foreach loop would not run. Try this
protected void dgrd_ItemDataBound(Object sender, DataGridItemEventArgs e)
{
// if in <EditItemTemplate>, use ListItemType.EditItem
if(e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

// open connection here
// DataSet declaration

// retrieve the fields needed only
string strSQL = "SELECT Loss_Code, Loss_Description FROM CauseOfLoss";
SqlDataAdapter daCOL = new SqlDataAdapter(strSQL,conn);

daCOL.Fill(myDS,"COL");
DataColumn dcol = new DataColumn();
dcol.ColumnName = "CompositeCol";
dcol.ColumnType = System.Type.GetType("System.String");
dcol.Expression = "Loss_Code + ' - ' + Loss_Description";
myDS.Tables["COL"].Columns.Add(dcol);
// locate DDL in DataGrid
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlIDInGrid");
ddl.DataSource = myDS.Tables["COL"];
ddl.DataTextField = "CompositeCol";
ddl.DataValueField = "Loss_Code";
ddl.DataBind();
}
}


Hope this helps...

vin1127 wrote:

The table Cause of Loss has a Loss _Code, and a Loss_Description. I would need to populate the ddlist with both columns. I also didnt mention that this ddlist is part of a datagrid. Thank you very much guys for your help. Does this look OK?

Sub GetCauseofLoss()

Dim conn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim myDS as DataSet
Dim currRecord as DataRow

Dim dr As SqlClient.SqlDataReader
Dim intField As Integer
conn = New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()

cmd.CommandText = "SELECT * FROM CauseOfLoss"

dr = cmd.ExecuteReader
While dr.Read

for each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Code))
dgddLossCode.Items.Add(new ListItem(dr.Item(Loss_Description))
next

End While
dr.Close()
conn.Close()

End Sub



Your code should be this.

Sub GetCauseofLoss()

Dim conn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim myDS as DataSet
Dim currRecord as DataRow

Dim dr As SqlClient.SqlDataReader
Dim intField As Integer
conn = New SqlClient.SqlConnection
conn.ConnectionString = Replace(Application("ConnectStage"), "Driver={SQL Server}; ", "")
cmd = conn.CreateCommand
conn.Open()

cmd.CommandText = "SELECT * FROM CauseOfLoss"

dr = cmd.ExecuteReader
While dr.Read

for each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item("Loss_Code"),dr.Item("Loss_Description"))
next

End While
dr.Close()
conn.Close()

End Sub


for each currRecord in myDS.Tables(0).Rows
dgddLossCode.Items.Add(new ListItem(dr.Item("Loss_Code"),dr.Item("Loss_Description"))
next
The above will actually add a new ListItem to your DDL with a Text of the value in Loss_Code and a Value of "Loss_Description". If this is what you want, thent that is correct. If you want them to both be added, you'll need to make them 1 string, then add that string. On a note, if you only supply on parameter to ListItem, it is made both the Text and Value property.

Populating a html table

How to populate a html table using data from sql database..

The simplest answer is the use a gridview to display a dataset selected from the database.

Populating a Textbox

This seems like a simple thing to do, but I cannot figure it out.
How can I populate a textbox with a value retreived from a SQL database so that it is editable?

Thank you!There's no true data binding in ASP; you've got to write code to retrieve the value and more code to update it. The following code will retrieve the value. You'll also need an Update query to update the value.
Good luck
<code>

Dim oConnAsNew Data.SqlClient.SqlConnection(sConnect)

Dim oCmdAsNew Data.SqlClient.SqlCommand

Dim oDSAsNew DataSet

Dim oDAAsNew SqlClient.SqlDataAdapter

Try

With oCmd

.CommandText = "Select Value From Table1"

.Connection = oConn

EndWith

With oDA

.SelectCommand = oCmd

oConn.Open()

.Fill(oDS)

Textbox1.text =ods.Tables(0).Rows(0).Item(0)


EndWith
</code>


What kind of object are you storing your value in? Is it just a single value you are getting back from the database? If so you could use the ExecuteScalar routine to put it into a string and then assign it to a text box. If you have it in a datatable, you just have to assign the proper cell to the text box:

textBox.Text = myDataTable.Rows(rowCounter).Cells("'myField")


As Mr Jkcnack said that if you are retrieving single value use ExecuteScaler method if you are retieving multiple values use dataReader.