I have two tables, list and songs. The songs table contains columns such as
'songnumber', 'name', 'path' and 'filename'.
The list table contains columns 'listnumber' (identity) and 'A', 'B', 'C',
etc... that contain songnumbers.
I have a form with a databound combo box where we select the list. Once the
list is selected, I'd like to get the 'name' of the song that is referenced
in the 'A' column of the list and make it be the label of a button.
That's where I get stuck... I have a couple of questions from my various
attempts at getting un-stuck...
How do I reference the songnumbers in the list table? I was trying this:
audioDataSet.songs.nameColumn.ToString();
...but that just returned the name of the column, not the data.
If I ever do get the songnumbers, I have made a stored procedure to get the
info for each song. Do I need to create a SqlCommand object in order to run
the SP, or is there one that already exists from the tableadapter? What is
the best way to call the SP?
Thanks,
-dog
.
Linda Liu [MSFT] - 10 Sep 2007 09:05 GMT
Hi,
> How do I reference the songnumbers in the list table?
If you bind the ComboBox to the list DataTable by setting the DataSource
property of the ComboBox, you could get the value of the songnumber as
follows:
DataRowView drv = comboBox1.SelectedItem as DataRowView;
int songNumber = drv["A"];
> What is the best way to call the SP?
We usually use a SqlCommand object to execute a Stored Procedure. We can
configure a SqlCommand object at either design time or run time.
To configure a SqlCommand object at design time, add a SqlCommand objec e.g
to a form and set the CommandType property to StoredProcedure and the
CommandText property to the name of the Stored Procedure you want.
If you want to do this at run time, you could write code similar to the
following sample:
SqlConnection con = new SqlConnection("data source =.\\sqlexpress; initial
catalog = TestDataBase;Integrated Security=true");
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandText = "MySP";
comm.CommandType = CommandType.StoredProcedure;
con.Open();
comm.ExecuteNonQuery();
con.Close();
Of course, if there's a resultset returned from the Stored Procedure, you
could create a SqlDataAdapter object and set its SelectCommand property to
the SqlCommand object and then call the Fill method of the SqlDataAdapter
object to fill the returned resultset to a DataTable.
In addtion, if the row count of the song table is not very large, I
recommend you to fill all the data from the song table to a DataTable. As
soon as you get the songnumber of the ComboBox selected item, you could
find the corresponding data row in the song DataTable and get the 'name' of
the song, which is more efficient. The following is a sample:
DataRowView drv = comboBox1.SelectedItem as DataRowView;
int songNumber = drv["A"];
DataRow dr = ds.Tables[0].Rows.Find(songNumber);
string songName = dr["name"];
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.