This .net stuff is killing me.What are you trying to do in more detail? What goes in each element of the array? What about multiple tables?
Not that I'm a Datset fan but you have a lot more functionality with your data in a dataset than an array.
This .net stuff is killing me.What are you trying to do in more detail? What goes in each element of the array? What about multiple tables?
Not that I'm a Datset fan but you have a lot more functionality with your data in a dataset than an array.
Dim sqlDapter As SqlDataAdapter
Dim dSet As DataSet
Dim dView As DataView
sqlDapter = New SqlDataAdapter(strSQL, sqlConn)
dSet = New DataSet()
dView = New DataView
sqlDapter.Fill(dSet, "Users")
dView = dSet.Tables("Users").DefaultView
dgUsers.DataSource = dView
dgUsers.DataBind()
The above works fine but I did like to bind the DataGrid to a GridView
instead of a DataView as the above code shows. What I did is deleted
the DataView from the above code & added a GridView i.e. all the
instances of the DataView were replaced with GridView i.e. changed the
variable name 'dView' to 'gView' but I get this error:
Value of type 'System.Data.DataView' cannot be converted to
'System.Web.UI.WebControls.GridView'
pointing to this line
gView = dSet.Tables("Users").DefaultView
How do I populate the GridView with the DataSet?Your DataSet and DataView variables stay the same. You will simply set
the DataSource of the GridView to your existing dView variable.
Using what you started with...
Quote:
Originally Posted by
sqlDapter.Fill(dSet, "Users")
dView = dSet.Tables("Users").DefaultView
Quote:
Originally Posted by
dgUsers.DataSource = dView
dgUsers.DataBind()
gvUsers.DataSource = dView
gvUsers.DataBind()
The DataGrid and GridView both take a DataView as the DataSource.
Brennan Stehling
http://brennan.offwhite.net/blog/
rn5a@.rediffmail.com wrote:
Quote:
Originally Posted by
I was using a DataView to bind records from a DB table to a DataGrid
using the following code:
>
Dim sqlDapter As SqlDataAdapter
Dim dSet As DataSet
Dim dView As DataView
>
sqlDapter = New SqlDataAdapter(strSQL, sqlConn)
>
dSet = New DataSet()
dView = New DataView
>
sqlDapter.Fill(dSet, "Users")
dView = dSet.Tables("Users").DefaultView
>
dgUsers.DataSource = dView
dgUsers.DataBind()
>
The above works fine but I did like to bind the DataGrid to a GridView
instead of a DataView as the above code shows. What I did is deleted
the DataView from the above code & added a GridView i.e. all the
instances of the DataView were replaced with GridView i.e. changed the
variable name 'dView' to 'gView' but I get this error:
>
Value of type 'System.Data.DataView' cannot be converted to
'System.Web.UI.WebControls.GridView'
>
pointing to this line
>
gView = dSet.Tables("Users").DefaultView
>
How do I populate the GridView with the DataSet?
I have several Label controls in my page and a stored procedure that always return 1 row. I want to call the same stored procedure with different parameter. Can anybody who me how to do this?
I have a function that contains:
DataSet objDs =new DataSet();
System.Data.SqlClient.SqlConnection myConnection =new System.Data.SqlClient.SqlConnection(
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);System.Data.SqlClient.SqlDataAdapter cmd;
cmd =new System.Data.SqlClient.SqlDataAdapter("sp_getCounter1", myConnection);cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
cmd.SelectCommand.Parameters.Add(
new SqlParameter("@dotnet.itags.org.param1", mode));
DataTable dtMasterListe =new DataTable("mytable");myConnection.Open();
cmd.Fill(objDs);
With the above code it is easy to e.g bind to datagrid, but I want to achiveve the following Label1.Text = objDs.getstring(etc).
Any clues?
public string FunctionName(){using (SqlConnection cn =new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { SqlCommand cmd =new SqlCommand("StoredProcedureName", cn); cmd.CommandType = CommandType.StoredProcedure;// Add parameters if needed cn.Open();return (string)ExecuteScalar(cmd); }}void Page_Load(){ Label1.Text = FunctionName()}This is what I usually do:
If you already sure the dataset only contains 1 table and 1 row only, then you can use the following to get the data you want:
Label1.Text = ds.Tables[0].Rows[0][0].ToString();
If you know the table name returned in the dataset, you can also pass in the tablename:
Label1.Text = ds.Tables["tablename"].Rows[0][0].ToString();
It will even be better if you can specify the column name:
Label1.Text = ds.Tables["tablename"].Rows[0]["Name"].ToString();
I usually check if the number of tables & rows > 0 then only get the value:
if ((ds.Tables.Count == 0) && (ds.Tables["tablename"].Rows.Count == 0))
Hope this helps. ^_^
Hi,
If you know that your stored procedure will return only one row all the time, then go for theExcecuteScalar method.
Works perfect!
Had to make a minor change:
int ret = (int)cmd.ExecuteScalar();
return ret.ToString();
to make ExecuteScalar to work.
Thanks.
Please tell me what I'm doing wrong.
check that if the dataset is populated when there is no PostBack.
Public Overloads Function GetBookInfo() As DataSet
Dim oPubConnection As SqlConnection
Dim SConnString As String
Dim osqlCommPubs As SqlCommand
Dim osqlCommTitles As SqlCommand
Dim oDataAdapterPubs As SqlDataAdapter
Dim oDataAdapterTitles As SqlDataAdapterTry
SConnString = "Data Source=(local);Initial Catalog=Pubs;" & _
"User ID=sa;Password=;"
oPubConnection = New SqlConnection(SConnString)
oPubConnection.Open()'Create command to retrieve pub info
osqlCommPubs = New SqlCommand
osqlCommPubs.Connection = oPubConnection
osqlCommPubs.CommandText = "Select Pub_ID, Pub_Name from Publishers"'Create Data Adapter for pub info
oDataAdapterPubs = New SqlDataAdapter
oDataAdapterPubs.SelectCommand = osqlCommPubs'Create command to retrieve title info
osqlCommTitles = New SqlCommand
osqlCommTitles.Connection = oPubConnection
osqlCommPubs.CommandText = "select Pub_ID, title, price, ytd_sales from titles"'Create data adapter for title info
oDataAdapterTitles = New SqlDataAdapter
oDataAdapterTitles.SelectCommand = osqlCommTitles'Create and fill a data set
Dim datBookInfo As DataSet = New DataSet
oDataAdapterPubs.Fill(datBookInfo, "Publishers")
oDataAdapterTitles.Fill(datBookInfo, "Titles")
Return datBookInfoCatch ex As Exception
Finally
oPubConnection.Close()End Try
End Function
If(!Page.IsPostBack)
{
LoadData();
}
Also, since this is a windows project and not a web project (assuming your "I've created a vb windows application" is correct) there will NOT be a page.isPostBack...
Thanks,
MajorCats
I hope someone can help me. I have an array defined as a two dimensional array that was populated from a dataset. I want to take that array and populate a dropdownlist. The reason I created a two dimensional array was because in the first dimension I store the value of the dropdownlist and in the second dimension I store the text for the dropdownlist. The value is not just a column from the dataset but a string that is pieced together. Please take a look at the code below and tell me what I'm doing wrong. If someone can please help me. I'm really lost.
Why again are you not binding it straight to the Dataset? You should be able to do this using a Dataset, even if it is a concatenation of certain columns (you may even be able to this at the DB level)
'This is were I populate the array
Dim objArray(,) As ObjectReDim objArray(intRowCnt - 1, intColCnt - 1)
i = 0
For Each dr In mDSet.Tables(0).Rows
With mDSet.Tables.Item(0).Rows(i)
If .Item("mbo_key").ToString.Length > 0 Then
objArray(i, 0) = .Item(3).ToString.Trim & "|" & .Item(4).ToString.Trim
objArray(i, 1) = .Item(1).ToString.Trim & " - " & .Item(2).ToString.Trim & " *"
Else
objArray(i, 0) = .Item("period_key").ToString
objArray(i, 1) = .Item(1).ToString.Trim & " - " & .Item(2).ToString.Trim
End If
End With
i += 1
NextWith ddlDropDownList
.DataSource = objArray
For i = 0 To intMBO
' this is were it's blowing up!!!!!
.DataValueField = CStr(objArray(i, 0))
.DataTextField = CStr(objArray(i, 1))
Next
.DataBind()
End With
As for your problem...you may be able to fix it if instead of using a multidimensial array, use a arrayed class that you define, then you should be able to set the .datavalue/textfield to the class properties (yes use properties). It would look much cleaner too. Very simple class
Public Class Items
Public Property Text...
Public Property Value...
End Class
Hope this helps!
--Michael
I thank you for your help but I'm not sure what you mean. I created 2 Properties like you suggested but I get an error saying I can't convert an array to string when I try to assign it to the .DataValueField of the dropdownlist. Can you please show me an example because I'm really fustrated with this problem.
Also the reason I don't bind it to a dataset is because the value of an option changes depending on if a certain field is Null or not. I wasn't sure how to do this.
With selPeriod
.DataValueField = mclsMBO.marstrValue
.DataTextField = mclsMBO.marstrText
End With
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?