vb.net, web page, SQL holding the selected data in an accessible variable

saljas
saljas used Ask the Experts™
on
The following code works, but I did not like because in order to get selected sql data I have to add a gridview control!!
Is there a way to get the returned data in a variable

         <asp:SqlDataSource ID="sourcename" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>">            
         </asp:SqlDataSource>
         <%  
             sourcename.SelectParameters.Add("CustIdIs", Session("CustIdIs"))
             sourcename.SelectCommand = "SELECT Col1, col2 FROM Table WHERE Col0= @CustIdIs  "
          %>
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="sourcename">
             <Columns>
                 <asp:BoundField DataField="Col1" HeaderText="Txt1" SortExpression="ttt" />                
                 <asp:BoundField DataField="Col2" HeaderText="Txt2" SortExpression="ttt" />                
            </Columns>
         </asp:GridView>
<%
            Dim I
            Dim str As String

            For I = 1 To GridView1.Rows.Count - 1
                str = GridView1.Rows(I).Cells(0).Text
                Response.Write(str & "<br>")
           Next
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
How many rows you are getting returned? If you will always get one row, you can use two text-boxes instead. Variable is fine, but you will loose it's contents due to the statelessness of web applications. You will probably have to store it in Session or fetch it again from db.
Ryan ChongSoftware Team Lead
Commented:
but I did not like because in order to get selected sql data I have to add a gridview control!!
Is there a way to get the returned data in a variable
yes, this is really simple, if you just want to read the data, to make things easier, you probably can start by using a data command object with a data reader object, depending on what backend database are you using.
Prakash SamariyaIT Professional
Top Expert 2015
Commented:
You can use SqlDataSource's Select Method in code-behind to get the data from sqldatasource as DataView and then you can play with it!
Example:
 DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

Open in new window

Please check URL for more information: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.select(v=vs.110).aspx
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Commented:
Many thanks for your answers

Nitin Sontakke
Large number of rows

Ryan Chong
I just started using onload event which seems easyer and lest restrictions

Prakash Samariya
I have spend 3 weeks trying that and other things



It is very good idea if there is a command that gets data from sql to a variable directly e.g.
variable = "SELECT Col1, col2 FROM Table WHERE Col0= @CustIdIs  "

or some thing similer
Prakash SamariyaIT Professional
Top Expert 2015
Commented:
It is very good idea if there is a command that gets data from sql to a variable directly e.g.
variable = "SELECT Col1, col2 FROM Table WHERE Col0= @CustIdIs  "
@saljas, There is no direct way to get data from any datasource to so called variable! You have to do some stuff in code-behind!

You can use
var sqlDataSourceView = SqlDataSource1.GetView(String.Empty)

Open in new window


Please check below url for SqlDataSource related all what you need!
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource(v=vs.110).aspx#Examples
Software Team Lead
Commented:
I just started using onload event which seems easyer and lest restrictions
well, onload event tells us when /where you want to execute your scripts, but it didn't mean that it's easier and less restrictions. you need a good approach how you want to extract your data, which some of us already provided that.

let us know if you need further assistance.

Author

Commented:
could not find a solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial