Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

storedprocedure Expand / Collapse
Author
Message
Posted Saturday, January 25, 2014 7:52 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:10 AM
Points: 13,230, Visits: 12,709
rani_sals (1/25/2014)
Oh okay!
Well now to call this SP for UI, I use this below code.. but what do I have to pass as the tvp object parameter, to enter the selected chkboxlist items.
my UI shows the trainers name so first I'm fetching their ID's and then have to pass the same as tvp object.

string r = CheckBoxListTrainers.Items.ToString();

DataSet1TableAdapters.tbl_Trainer1TableAdapter tt = new DataSet1TableAdapters.tbl_Trainer1TableAdapter();
DataSet1.tbl_Trainer1DataTable dt = tt.GetDataBy(r); // this getDataBy() calls for a SP that takes the id and returns its name from tbl_Trainer
int id = int.Parse(dt.Rows.ToString()); // here I'm getting an error saying input string in not in the correct format


DataSet1TableAdapters.tbl_WorkshopTrainerTableAdapter ta = new DataSet1TableAdapters.tbl_WorkshopTrainerTableAdapter();
ta.sp_InsertWorkshopTrainers(txtTitle.Text, txtTopic.Text, DateTime.Now, txtDuration.Text, DateTime.Now, DateTime.Now,id); // here id is an object tvp that has to passed in the SP


You need to read up a bit on tvp and how they work. You also need to think through what you are doing here. Your parameter is a table, not an int.

Your first error is because you are trying to get the rows collection of your datatable and parse it as an int. That doesn't make sense. There are a number of .net objects that can populate a tvp. From looking at your code, the easiest is probably to use a datatable.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1534721
Posted Saturday, January 25, 2014 8:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:06 AM
Points: 7, Visits: 43
If I try something like this:
How to pass a tvp in C#?

protected void btnSave_Click(object sender, EventArgs e)
{
foreach (ListItem item in CheckBoxListTrainers.Items)
{
if (item.Selected)
{
DataSet1TableAdapters.tbl_Trainer1TableAdapter tt = new DataSet1TableAdapters.tbl_Trainer1TableAdapter();
DataSet1.tbl_Trainer1DataTable dt = tt.GetDataBy(item.Text);
int i = int.Parse(dt.Rows[0][0].ToString()); // i gives me ID's of the selected trainers
}
}

DataSet1TableAdapters.tbl_WorkshopTrainerTableAdapter ta = new DataSet1TableAdapters.tbl_WorkshopTrainerTableAdapter();
ta.sp_InsertWorkshopTrainers(txtTitle.Text, txtTopic.Text, DateTime.Now, txtDuration.Text, DateTime.Now, DateTime.Now,*???*); // What to pass as a TVP for the trainerID's???
}
Post #1534723
Posted Sunday, January 26, 2014 2:54 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:06 AM
Points: 7, Visits: 43
protected void btnSave_Click(object sender, EventArgs e)
{
DataTable dataTable = new DataTable("tvpTID");
dataTable.Columns.Add("TrainerID", typeof(Int32));

foreach (ListItem item in CheckBoxListTrainers.Items)
{
if (item.Selected)
{
DataSet1TableAdapters.tbl_Trainer1TableAdapter tt = new DataSet1TableAdapters.tbl_Trainer1TableAdapter();
DataSet1.tbl_Trainer1DataTable dt = tt.GetDataBy(item.Text);
int i = int.Parse(dt.Rows[0][0].ToString());
dataTable.Rows.Add(i);
}
}

DataSet1TableAdapters.tbl_WorkshopTrainerTableAdapter ta = new DataSet1TableAdapters.tbl_WorkshopTrainerTableAdapter();
ta.sp_InsertWorkshopTrainers(txtTitle.Text, txtTopic.Text, DateTime.Now, txtDuration.Text, DateTime.Now, DateTime.Now, dataTable);
Response.Write("Workshop added successfully");
BindData();
}

THis worked well for me. THanks everyone who tried helping me:)
Post #1534775
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse