Showing posts with label guys. Show all posts
Showing posts with label guys. Show all posts

Monday, March 26, 2012

Populate ASP:dropdownlist using OutputParams

Hi Guys,

Anybody know how I can populate a dropdownlist using an output param from SQL?

I have:


...
SqlParameter parameterScores = new SqlParameter("@dotnet.itags.org.Scores", SqlDbType.NVarChar, 100);
parameterScores.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterScores);

...
...

Users userInfo = new Users();
userInfo.Name = (string)parameterName.Value;
userInfo.Scores = (string)parameterScores.Value;

return userInfo;

...
...

Name.Text = userInfo.Name;
scores.DataSource = userInfo.Scores;

Any help will be greatly apprecieated.

Thanksif 'Users' is a collection you should be able to use your code, be sure to call the scores.DataBind() method...

otherwise, just put the values in a collection (Dictionary, HashTable, etc) or array and access them.

// Method 1.) Binding...

Hashtable myHash = new Hashtable();
myHash.Add("key1", "Ohio"); // Add these when you get the info from the db
myHash.Add("key2", "Vermont"); // Add these when you get the info from the db

dd.DataSource = myHash;
dd.DataTextField = "Value";
dd.DataValueField = "Key";
dd.DataBind();

// Method 2.)

Just simply add the values when you've got them.

ie:

dd.Items.Add("Ohio"); // Add these when you get the info from the db
dd.Items.FindByText("Ohio").Value = "myKeyValue";

Let me know if you have problems...

Saturday, March 24, 2012

Populate dropdown

Hy Guys,

I have a datagrid that, in the edititem mode, have a dropdownlist. This
dropdownlist have to be populated with the items of another dropdownlist
that is in the same page.

How can I do it??

Thank'sthis is what you need

http://msdn.microsoft.com/library/d...stomcolumns.asp

--
Regards,

HD

"Andr Almeida Maldonado" <deweb@.bol.com.br> wrote in message
news:ebVFaqi2DHA.2428@.tk2msftngp13.phx.gbl...
> Hy Guys,
> I have a datagrid that, in the edititem mode, have a dropdownlist. This
> dropdownlist have to be populated with the items of another dropdownlist
> that is in the same page.
> How can I do it??
> Thank's

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 DropDownList with a Data Method Code Wizard

Please help... I am sure most of you guys know the answer to this basic question

I am using the Web Matrix ...Getting Started Manual and am to the

Using a Data Method Code Wizard to Populate a DropDownList.

The question is : When asked to Remove the Button1_Click event code...

How is this done? Do I remove the entire code?? And where is the event code?

Sub Button1_Click(sender As Object, e As EventArgs)
DataList1.DataSource = GetOrderDetails(CInt(TextBox1.Text))
DataList1.DataBind()
End SubIn WebMatrix, new events are automatically hooked up to event handlers declaratively. So, after creating a method that handles a Button webcontrol's OnClick event, you'll have something like this in your HTML view:


<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>

In this example, the 'onclick' attribute's value identifies the name of the method that handles this control's Click event.

So, if you want to unhook an method from an event in Web Matrix, the simplest way to do this is to remove the corresponding event's attribute on that control's tag. What you do with the method's code after that is entirely up to you.

Hope this helps!
No..this did not help..but thanks anyway. Could you please go to the following URL and see what I mean.

http://www.asp.net/webmatrix/guidedtour/section4/ddlcodebuilder.aspx

I am 1/3 of the way through the tutorial and I like ASP.Net thus far.

Populating ComboBox From Dataset

Hi Guys,

I need to populate a combobox from a dataset returned from a webservice. I set the datasource property of the control to the web method but got an empty list at run time! What am I doing wrong? Any help on how to achieve this? Looping through a datareader solved the problem but I want to consume a dataset from a web service. I thought this would be a straight stuff but I'm having problems with it.After setting the datasource property you have to bind the data to your controll.

It seems you are missing that bit.
I can handle that with DropDownList control in ASP.Net but not with ComboBox in Windows Application! Could you kindly show how syntactically?
Are you getting an error?
What does it say?

Post your code so that we can see more clearly what has gone wrong
I am not getting any error message! I only get this wierd entry in the ComboBox. "System.Data.DataViewManagerListItemTypeDescriptor"

This is the code that I used.

Private Sub Users_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim wsDemo As New ng_app001_pc_stock.PCInventory
ComboBox1.datasource = wsDemo.getAllOSTypes
End Sub

The webmethod getAllOSTypes works well, at least I tested it with DataGrid control. What could be wrong?