SQLDataReader Find Value Of Each Row?
Nov 10, 2010
I used to use datasets instead of sqldatareaders and I used to be able to do something like this
If dataset.tables(0).Rows(0)(1).ToString()) = "N" Then
lbl.Text = dataset.tables(0).Rows(0)(2).ToString())
Else
'Do Nothing
End If
This obviously doesn't work with sqldatareaders.I have code to see if the SQLDatareader has any rows but was wondering if there was a way to get the value of each rowI'm guessing this is possible and i've had a look around but can't seem to find anything
Dim conn As SqlConnection = New SqlConnection("server='h'; user id='w'; password='w'; database='w'; pooling='false'")
conn.Open()
Dim query As New SqlCommand("DECLARE @investor varchar(10), @sql varchar(1000) Select @investor = 69836 select @sql = 'SELECT * FROM OPENQUERY(db,''SELECT * FROM table WHERE investor = ''''' + @investor + ''''''')' EXEC(@sql)", conn)
Dim oDR As SqlDataReader = query.ExecuteReader()
If oDR.HasRows or dataset.tables(0).Rows(0)(1).ToString()) = "N" Then
lbl.Text = dataset.tables(0).Rows(0)(2).ToString())
Else
'Do Nothing
End If
That is the code I have at the moment which obviously doesn't work
View 2 Replies
Similar Messages:
Sep 7, 2010
In normal way of write the code as follows in DAL.
DataSet ds=db.executedataset();
IEnumerable<datarow>= ds.tables[0].asEnumerable();
For better performance can use datareader in Linq instead of DataSet
View 11 Replies
Mar 20, 2011
why the following code doesn't work?
internal static string[] GetToolsForRole(string selectedRole) {
string[] tempStr;
ArrayList myAL = new ArrayList();
[code]...
View 2 Replies
Mar 29, 2011
how to get the number of rows from sqldatareader
View 2 Replies
Oct 25, 2010
I have a SQL Server 2008 database and I am working on it in the backend. I am working on asp.net/C#
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//how do I read strings here????
}
I know that the reader has values. My SQL command is to select just 1 column from a table. The column contains strings ONLY. I want to read the strings (rows) in the reader one by one. How do I do this?
View 5 Replies
Feb 26, 2011
How can you load a List<> object from a Datareader?I tried to do it but for some reason it is not working. Below is the code that I tried to use:
using System;
using System.Data;
using System.Configuration;
[code]...
View 1 Replies
Jul 20, 2010
This my code SHoul the sqldatareader even be null when i debug (becuase response write not working)
[Code]....
View 4 Replies
Sep 19, 2010
hile i have been working with application perfomance tuning part i found an article for doing the same from Microsoft. it suggests me to reduce the round trip between application and database by using Sqlbatch statements. i got two ways to achieve it 1. SqlDataReader.NextResult() - we can use single connection to execute sql batch statements2. MARS (Multiple Active Result Sets) - we can use single connection to open multiple readers and execute the batch statements
View 2 Replies
Mar 12, 2011
i need to Get Two rows details in variables using SQLdatareader and using while loop in it.Can i do so,
my query and while loop are getting fired when i debug and see my while loop...but i dnt know how to get details and show or store it, when i will get 2 records on execution of query.
MY CODE IS BELOW...
[Code]....
View 4 Replies
Apr 15, 2010
When I query for only one record/row, sqldatareader is giving correct result but when i query for multiple rows, its giving error on the client side. below is my code.
[code]....
View 3 Replies
Aug 19, 2010
I am trying to run a stored proc from this function. When I run it thorugh the debugger I can see that myReader.HasRows = true and myReader.FieldCount =14.
But the control does not go in the loop while (myReader.Read()) where I am trying to read Version field returned by the stored proc.
[Code]....
View 3 Replies
May 13, 2010
i try to use userControl to display SqlDataReader data.
in the main page
public SqlDataReader Data2;
...
<uc1:WebUserControl ID="WebUserControl1" RData1="<%=Data2 %>" runat="server" />
and in the userControl
Repeater1.DataSource = RData1;
Repeater1.DataBind();........
But i keep getting this error
Cannot create an object of type 'System.Data.SqlClient.SqlDataReader' from its string representation '<%=Data2 %>' for the 'RData1' property.
View 1 Replies
Jan 16, 2011
I have a very strange issue, I'm using System.Data.SqlClient. to get data from a SQL Server though a stored procedure. When I test the Application at the Development and Stagging machines it works fine but when I deploy the application on the Production Server I randomly getting an SqlDataReader IndexOutOfRangeException with different column names!.The error appears in 2 requests in each 1000 request (approximative).
public static List<CountryInfo> GetAllCountries(){
List<CountryInfo> Items = new List<CountryInfo>();
try{
using (rdr = SqlHelper.ExecuteReader(Globals.ConnectionString, "unv_spGetAllCountries"))
[code]...
View 2 Replies
Apr 4, 2011
I'm using Json.NET and I'm trying to create a JSON string representing a row from a database table to return in an HTTP response. I'm not sure how to do this while I'm reading from the database.The problem is marked by the obnoxious comments /******** ********/
// connect to DB
theSqlConnection.Open(); // open the connection
SqlDataReader reader = sqlCommand.ExecuteReader();
[code]...
View 1 Replies
Sep 24, 2010
I'm trying to convert some code from VB to C# on a site i'm working on and can't seem to get the arraylist to function properly.
Here is what we currently have in the codebehind:
[Code]....
Which we then call from the page using:
[Code]....
I want to convert this to C# as the rest of the code on the new site is in C# and I'm having problems with the ArrayList. I've got the other parts of the code all converted fine it's just the GetContent Function that isn't working right. Here's what I have:
[Code]....
I got an error on the "objSDR.Item" part of the code saying "SqlDataReader doesn't have definition for Item" and so if I changed this to the code above. Only problem now is I can't call this from the page using the code below as there is no "Item" in the GetContent Function any more
[Code]....
[Code]....
View 8 Replies
May 2, 2010
In SQL Server 2008 bit is like boolean (true/false), but I tried to read the bit value (0 or 1) and got error. How can I read it?
[Code]....
View 1 Replies
Apr 18, 2010
how to read data from the sqlDataReader .
sqlDataReader sdr = sql_comm.executeDataReader();
if(sdr.hasRows)
{
while(sdr.read)
i=0
[code]...
View 3 Replies
Aug 25, 2010
I know it is recommended to use a using statement with a sqldatareader, but I was wondering if it is necessary when you are setting the datasource of a control to a datasource directly. in some places in my code I do this.
using (SqlDataReader reader = getReader())
{
while (reader.Read())
{
// do stuff with data
}
}
when binding to a control I do this.
ddlCustomer.DataSource = getReader(); // get Reader returns a sqldataReader
ddlCustomer.DataBind();
In the second case, do I need to use a using statement. Do i have to first declare an SqlDataReader in a using statement, then set the DataSource to that object. Seems like more clutter in the code, so i was hoping binding to the SqlDataReader disploses of the SqlDataReader.
View 3 Replies
Feb 18, 2010
Why SqlDataReader Class object is not created by "new" Operator.
Is it not possible to create the object like below
SqlDataReader dr=new SqlDataReader();
dr=cmd.ExecuteReader();
Why the constructor of the SqlDataReader class is internal?
View 2 Replies
Oct 3, 2010
I have a vb class that reads a single row of data from an SQL view. I want to add a property to it that will expose the value of a new bit field. But whatever I do my class always returns the value "false".What am I doing wrong?
Public ReadOnly Property IsPublished() As Boolean
Get
Return valPublished
End Get
End Property
Here's the relevant line from my datareader: Me.valPublished = Convert.ToBoolean(theObjectReader("Authorised"))Authorised is my bit field. I have tried it with and without the "ConvertToBoolean" statement.When I response write the value of the property for rows where this field is true, it always shows false.
View 4 Replies
Apr 13, 2010
While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.
If the above claim is true, then why is the following method able to retrieve a value from output parameter before the reader is closed:
public int Something()
{
using (SqlConnection con = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("some_procedure", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteReader();
return (int)cmd.Parameters["@ID"].Value;
}
}
You can reset the CommandText property and reuse the SqlCommand object. However, you must close the SqlDataReader before you can execute a new or previous command.
Why must you close sqldatareader before you execute new command?
View 2 Replies
Sep 20, 2010
I still have plenty to learn about SQL... I'm pretty sure what I want to do is possible, but I'm getting lost in the syntax.
I have two tables:
TABLE A: [ID] [Name]
TABLE B: [ID] [TableA.ID] [intValue] [intTypeID]
There are 3 pieces of information I want:
Total Sum(TableB.intValue)
Sum(TableB.intValue) WHERE intTypeID = 1
Sum(TableB.intValue) WHERE intTypeID = 2
Getting one of those pieces is simple: SELECT a.ID,a.Name,SUM(b.intValue) WHERE a.ID = x GROUP BY a.ID,a.Name
I assume I need some kind of JOIN, but I can't seem to get it..
View 2 Replies
Feb 23, 2010
I am trying to create a search page, this allows the admin to search through the entries in the database by certain criteria, such as province (like state but diff country)
Here is the code I have so far. The problem is that I am not getting any errors. But I am also not getting any results. The page just posts back and returns to the blank search page. The GridView does not display any results.
here is my code as it stands:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class administration_Search
Inherits System.Web.UI.Page
Protected Sub ProvinceButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProvinceButton.Click
'get the search string from text box
Dim Search As String
Search = ("%" & ProvinceButton.Text & "%")
'if the string has value then continue with search
If Len(Trim(Search)) > 0 Then
Dim con As String = WebConfigurationManager.ConnectionStrings("fubar").ToString()
'build sql string
Dim s As String
s = ("SELECT id, surname, name FROM orders WHERE province like @strSearch")
'declar connection
Dim c As SqlConnection = New SqlConnection(con)
'add command
Dim x As New SqlCommand(s, c)
'add parameter
x.Parameters.AddWithValue("@strSearch", strSearch)
c.Open()
Dim r As SqlDataReader = x.ExecuteReader
GV.DataSource = r
GV.DataBind()
c.Close()
Else
Province.Text = ("Please enter search terms")
End If
End Sub
End Class
On the .aspx page I have a textbox (id: province) and a button (id:submit)
View 4 Replies
Jun 17, 2010
[Code]....
My question is how do i use SqlDataReader to read the last row added to a table getting the contents of a column and storing it as a c# varaible
View 2 Replies
Jun 28, 2010
how to convert sqldatareader to List
View 2 Replies