Re: SqlDataAdapter - can it be (re)used for 2 SELECT statements



Might be best if I give an example. I'm wondering if I can use the one
dataadapter for 2 different calls to the same sproc, and/or how I can use
less code:

// Open a connection to database
SqlConnection objConn = new
SqlConnection(ConfigurationSettings.AppSettings["connJBS"]);
//create dataset to hold all table rows
DataSet objDSJobTypes = new DataSet("JobTypes");

//Web Jobs
//Create the stored procedure command object & add parameter objects for
the stored procedure parameter
SqlCommand objCmdWebJobs = new SqlCommand("spTFL_RequestJob_JobTypes",
objConn);
objCmdWebJobs.CommandType = CommandType.StoredProcedure;
objCmdWebJobs.Parameters.Add("@JobCatID", SqlDbType.Int);
objCmdWebJobs.Parameters["@JobCatID"].Value = 1;
//create our DataAdapter object and use it to fill the dataset object
SqlDataAdapter objDAWebJobs = new SqlDataAdapter(objCmdWebJobs);
objDAWebJobs.Fill(objDSJobTypes,"WebJobs");
//bind to repeater
Repeater1.DataSource=objDSJobTypes.Tables["WebJobs"].DefaultView;
Repeater1.DataBind();

//Web Projects
//Create the stored procedure command object & add parameter objects for
the stored procedure parameter
SqlCommand objCmdWebProjects = new SqlCommand("spTFL_RequestJob_JobTypes",
objConn);
objCmdWebProjects.CommandType = CommandType.StoredProcedure;
objCmdWebProjects.Parameters.Add("@JobCatID", SqlDbType.Int);
objCmdWebProjects.Parameters["@JobCatID"].Value = 2;
//create our DataAdapter object and use it to fill the dataset object
SqlDataAdapter objDAWebProjects = new SqlDataAdapter(objCmdWebProjects);
objDAWebProjects.Fill(objDSJobTypes,"WebProjects");
//bind to repeater
Repeater2.DataSource=objDSJobTypes.Tables["WebProjects"].DefaultView;
Repeater2.DataTextField = "JobTypeDesc";
Repeater2.DataValueField = "JobTypeID";
Repeater2.DataBind();

Any advice greatly appreciated.
Thanks
Matt

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:es3A%23rbfFHA.3904@xxxxxxxxxxxxxxxxxxxxxxx
> Not sure what you are asking about about the parameters.
>
> If you are using an sproc (as you indicated) you'd pass in all the
> parameters needed for both query to a single sproc and the queries can use
> them as needed.
>
> If you want to refer to the tables by name, simply name them after you've
> done the fill based on the index (so use the index the first time, then
> the name).
>
> Karl
>
>
>
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
>
>
> "Matt Jensen" <replytonewsgroups@xxxxxxxxxxxxx> wrote in message
> news:u7idOnZfFHA.3256@xxxxxxxxxxxxxxxxxxxxxxx
>>I guess I can just build up SqlCommand objects (including adding
>>parameters etc.) then just comma list them inside the SqlDataAdapter
>>statement, can I?
>> How about naming the tables?
>> Thanks
>> Cheers
>> Matt
>>
>> "Matt Jensen" <replytonewsgroups@xxxxxxxxxxxxx> wrote in message
>> news:%23qEa3kZfFHA.1204@xxxxxxxxxxxxxxxxxxxxxxx
>>> Nice, thanks :-)
>>> Same for stored procedures with parameters though?
>>> How about naming the tables in the dataset, or does one just refer to
>>> them by table index number?
>>> Cheers
>>> Matt
>>>
>>> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
>>> wrote in message news:Oio5xZZfFHA.572@xxxxxxxxxxxxxxxxxxxxxxx
>>>> Just do it in 1 query.
>>>>
>>>>
>>>> select * from x; select * from y
>>>>
>>>> it'll populate 2 tables in the 1 dataset that you Fill()
>>>>
>>>> Karl
>>>>
>>>> --
>>>> MY ASP.Net tutorials
>>>> http://www.openmymind.net/ - New and Improved (yes, the popup is
>>>> annoying)
>>>> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
>>>> come!)
>>>>
>>>>
>>>> "Matt Jensen" <replytonewsgroups@xxxxxxxxxxxxx> wrote in message
>>>> news:O3j4KVZfFHA.3436@xxxxxxxxxxxxxxxxxxxxxxx
>>>>> Howdy
>>>>> Fairly simple question I think, I presume the answer is no it can't be
>>>>> reused for 2 *SELECT* statements, but just hoping for clarification.
>>>>> Just asking in the interests of trying to minimise code.
>>>>>
>>>>> i.e. if the SqlDataAdapter uses a connection to the one database for a
>>>>> select statement and I want to do a second select statement on exactly
>>>>> the same database, can I reuse the SqlDataAdapter?
>>>>>
>>>>> I ask because I want to put both 'select' results into the one
>>>>> dataset, just thought I'd use the SqlDataAdapter if I could.
>>>>>
>>>>> Thanks
>>>>> Cheers
>>>>> Matt
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>


.



Relevant Pages

  • Re: Retrieving SPROC return value using SPT
    ... You cannot use the parenthesis in calling the stored procedure. ... Your parameter must be declared as OUTPUT inside your stored procedure. ... It still not updated after return from> sproc. ... The "@" seems to be sufficient to tell> SQL>> that it's an output parameter. ...
    (microsoft.public.fox.vfp.queries-sql)
  • Re: problem using sqlDataAdapter wizard
    ... > your SP does not return any result to client at all and DataAdapter cannot ... >> using wizard) in which I use stored procedure to generate the data. ... Here is workaround for this bad bahaviour (invalid object #temptable). ...
    (microsoft.public.data.ado)
  • Re: problem using sqlDataAdapter wizard
    ... > your SP does not return any result to client at all and DataAdapter cannot ... >> using wizard) in which I use stored procedure to generate the data. ... Here is workaround for this bad bahaviour (invalid object #temptable). ...
    (microsoft.public.data.ado)
  • Im so confused
    ... normally from Query Analyzer rather than calling it from any other code so I ... > system stored procedure sp_grantdbaccess checks that there is not an ... > there is not an enclosing transaction. ... Here's my sproc and the ...
    (microsoft.public.sqlserver.security)
  • RE: Database updating issue
    ... I started a transaction on the connection class, passed this to the dataadapter, invoked the Update method on the dataadapter and finally commit the transaction. ... try threading the these calls to the stored procedure. ... To accomplish this task, the inserting of all rows, at the moment, I'm using ...
    (microsoft.public.dotnet.framework)