|
-
Apr 18th, 2007, 09:31 AM
#1
Thread Starter
New Member
passing parametor to SQL Store Procedure to generate dataset to populate datagrid
It's been 24 hours, I'm a newbie so please be patient with me. I'm trying to pass a parametor from a textbox to a SQL Store Procedure to generate dataset to populate datagrid on a form in a windows application.
I found an example of someone using what i believe is C++ but I need to do mine in C#, I got to transalate all the code except the last part. this is my code:
private void btnSearch_Click(object sender, System.EventArgs e)
{
SqlConnection thisConn = new SqlConnection();
thisConn.ConnectionString = @"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=HS00749";
SqlCommand thisCommand = new SqlCommand("CustomerPhoneNumber", thisConn);
thisCommand.CommandType = CommandType.StoredProcedure;
thisCommand.Connection = thisConn;
//Create DataAdapter object for update and other operations
SqlDataAdapter thisAdapt = new SqlDataAdapter();
thisAdapt.SelectCommand = thisCommand;
SqlParameter phoneNumber = new SqlParameter("@PhoneNumber", SqlDbType.NVarChar);
phoneNumber.Direction = ParameterDirection.Input;
thisCommand.Parameters.Add(phoneNumber);
phoneNumber.Value = this.txtCustomerPhoneNumber.Text;
thisAdapt.Fill(this.dsCustomerPhoneNumber1,"cpn");
this.dgResults.DataSource = this.dsCustomerPhoneNumber1.Tables;
this.dgResults.DataBindings.
}
The last part of this example i found on somewhere goes like this:
DataGrid1.DataBind()
this line is suppose to go after the last line of my code (this.dsCustomerPhoneNumber1.Tables) but in c# there's no DataBind but DataBindings which only lets me bind to one of my columns in my dataset and it's not a method like that nice DataBind().
Currently the error a i get is:
Complex DataBinding accepts as a data source either an IList or an IListSource
so the binding part of my dataset to my datadrid is what I think is causing this??
any ideas?
Thanks in advance!!
Francisco.
-
Apr 18th, 2007, 09:52 PM
#2
Re: passing parametor to SQL Store Procedure to generate dataset to populate datagrid
You're actually making things more complex for yourself than is necessary. There are various ways to add a parameter to a command and you should always use the simplest one that does what you need. In your case it's this:
C# Code:
thisCommand.Parameters.AddWithValue("@PhoneNumber", this.txtCustomerPhoneNumber.Text);
Now to your questions. The fact that there's no DataBind method has nothing to do with C#. It has to do with the fact that the example you're looking at is for ASP.NET, i.e. WebForms, and you're creating a WinForms app. They are two different DataGrids; similar but not the same. The WinForms DataGrid has no DataBind method.
You're trying to assign the Tables collection of a DataSet to the DataSource of your grid. That's not legal. Yopu either bind the DataTable directly:
Code:
this.dgResults.DataSource = this.dsCustomerPhoneNumber1.Tables["cpn"];
or via the DataSet:
Code:
this.dgResults.DataMember = "cpn";
this.dgResults.DataSource = this.dsCustomerPhoneNumber1;
-
Apr 19th, 2007, 12:38 AM
#3
Thread Starter
New Member
Re: passing parametor to SQL Store Procedure to generate dataset to populate datagrid
jmcilhinney:
thank you very much for responding
after I posted my inquiry I gave it a try with the data table suggestion but what i did was to create a datatable object and i filled it with data from the adapter and then bind that data table to the datagrid and boila!! it worked, which is pretty much one of the options you gave me but the one you gave me looks nicer than mine since I actually had to instantiate a data table to fill instead of using the dataset which was no getting me nowhere.. I will, though, tried your suggestions they're probably more efficient... and again I can't thank you enought for your time and your explanation
I'll try also the way you suggested for me to pass the parameters..the "addwithvalue property looks brand new to me!... i guess I need to learn how to see beyond my limited c# book!!
cheers!
Francisco
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|