Tags down


Read multiple rows and multiple columns using SqlDataReader in C#

By : Sumit Garg
Date : September 12 2020, 09:00 PM
I wish this helpful for you You are almost there. You just need an array or collection to store the rows in.
code :
public class MyCard
    public string Name { get; set; }
    public string Number { get; set; }
    public string Expiry { get; set; }
    public string Balance { get; set; }
   //Please note:  This needs updating to match the data type used in your DB table.
   //I have used string to show you a simple example.
SqlDataReader reader = cmd.ExecuteReader();
List<MyCard> MyCardList = new List<MyCard>();
while (reader.Read())
     MyCard mycard = new MyCard();
     mycard.Name = reader[0].ToString();
     mycard.Number = reader[1].ToString();
     mycard.Expiry = reader[2].ToString();
     mycard.Balance = reader[3].ToString();
//Remember to close the reader and dispose of objects correctly.

Share : facebook icon twitter icon

Using SQLDataReader to insert multiple rows in table

By : meme
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , This should be done completely in SQL, using an INSERT statement, something like:
code :
INSERT Club_Member (UserID, Firstname, Lastname, Email....)
SELECT m.UserID, c.FirstName, c.LastName, c.Email1
FROM aspnet_membership m
JOIN [Historical$] c on m.EMail = c.Email1

SqlDatareader Has Rows with Multiple Result Sets

By : Jameel Ahmad
Date : March 29 2020, 07:55 AM
Any of those help Try to execute your query directly from SQL to see what is the result you get. Maybe you could try to re-think the original query and insert there a check for empty result set.
Here is a sample of DataReader with multiple result sets: do you see something different from the code you're using?

How to read multiple resultset from SqlDataReader?

By : Rajesh Yadav
Date : March 29 2020, 07:55 AM
this one helps. Here you have a sample about how to handle multiple result sets with a data reader
code :
static void RetrieveMultipleResults(SqlConnection connection)
    using (connection)
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM dbo.Categories;" +
          "SELECT EmployeeID, LastName FROM dbo.Employees",

        SqlDataReader reader = command.ExecuteReader();

            Console.WriteLine("\t{0}\t{1}", reader.GetName(0),

            while (reader.Read())
                Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0),
        while (reader.NextResult());

SqlDataReader prints multiple rows, how can I split?

By : draw chen
Date : March 29 2020, 07:55 AM
like below fixes the issue I want to retrieve data from a DB using a SqlDataReader. I got it working but my query return more than one row of data and I want to separate it. , Try this
code :
string query = "select title, rating, dor from movie where title like '%"+name+"%';";

using (SqlConnection conn = new SqlConnection(cs))
    SqlCommand cmd = new SqlCommand(query, conn);

    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.HasRows)
        while (reader.Read())
            Response.Write("------------------------");//whatever separator you want to use
        Response.Write("No rows found");


Collapsing multiple rows by unique identifier, multiple values (columns), multiple elements per variable (rows)

By : Joe Clark
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am trying to collapse multiple rows by unique identifier in R, but each column could have multiple variables that I want to keep in separate rows. How do I do that? I've tried the "separate_rows" function, but it says that each of the nested columns must have the same number of elements. Mine don't. , tried something ugly. there must be something prettier.
code :

x= df %>% split(.$identifier) %>% map(function(x)sapply(x, function(y)sort(unique(y),dec=T)))
xx=map_dfr(x, function(x){
    mx=max(sapply(x, length))
    z=lapply(x, function(y){
        if(length(y)>0 & startsWith(y[1],"sample")){
            c(y,rep(y[1], mx-length(y)))
            c(y,rep("", mx-length(y)))
}) %>% as.data.frame

xx$identifier = unlist(lapply(split(nm,nm), function(x)if(length(x)>1)paste0(x,".",1:length(x)) else x ))

Related Posts Related Posts :
  • Why is my commit button not going through its for loop?
  • How can I declare a variable with name 'operator'?
  • Deadlock when calling an async method inside a non async action
  • Visual Studio 2019 closing curly brace not under the opening brace (it's fully left aligned instead)
  • How to access and change the property of a label that I created dynamically
  • Calling a method that returns a random number from inside a loop
  • How to import binary heaps in C#
  • C# Driver MongoDb: How to use "UpdateOneAsync"
  • How to use the data from reading a file
  • How to define local variable in a select LINQ-query
  • Using CultureInfo in order to replace USD symbol with -
  • Is it too early to migrate from .net core 2.2 to .net core 3?
  • Accessing null value leads to application fail / C#
  • Mask first 3 Characters in the Email with "*"
  • Stop method from execution
  • Keep one number after decimal point, not rounding
  • Read a Siemens PLC s7 String in C# with S7netplus
  • How to call a stored procedure in EF Core 3.0 via FromSqlRaw
  • Best workaround for not being able to inherit a sealed class?
  • How do you reference a variable from another class?
  • How to instantiate a class as the interface that it derives from with constrained generic type parameter
  • Is it possible to do a .NET Core DLL containing .NET Core Framework?
  • C# writing JSON to a file without backslashes
  • How to remove special characters from input text box and display in a label?
  • How to make the first or last letter of every word in a string lowercase
  • Transfer variables between scenes
  • Cross apply two list, return same modified object
  • How to stop propagating an asynchronous stream (IAsyncEnumerable)
  • Fatal Error: Public Main() method is required in a public class
  • C# calorie counter
  • How can i make a proper if/else if/else statement?
  • How to force an IAsyncEnumerable to respect a CancellationToken
  • "the term 'add-migration' is not recognized as the name of a cmdlet" visual studio 2019
  • How to deal with optional data in a typed domain model?
  • Reasons to not throw when implementing `IComparable<T>`?
  • How to properly instantiate dynamically derived objects and their members in C#
  • How to assert in selenium webdriver in C#?
  • How to display version number a certain way
  • What is the main PURPOSE of Composition in C#?
  • DropDown List is empty when generating a view from the model
  • Simple question about the name of an expression containing ? :
  • How to fix 'DataTable' does not contain a definition for 'AsEnumerable' Error in Microsoft Botframework v4?
  • Wrong Date value if day or month properties are one digit
  • Is there a way i can draw a transparent WPF application over the desktop at all times?
  • Issue With Foreign Key in mvc?
  • Cannot add custom user data to scaffolded ASP.NET Core project
  • Microsoft DI Container _transientDisposables not disposed (memory leak)
  • Is there any shortcut to add the lambda/func arrow operator => in Visual Stuido 2019?
  • Client certificate with HttpClient in c#
  • Complex IEnumerable Result from LINQ Select
  • Performance Way to check if a byte array contains another byte Array C#
  • Repository pattern and change log implementation
  • DotNet core 3.0 compilation issues in VSCode
  • How to create a random spawner in unity 2d?
  • MVC API not hitting controller
  • Getting data from SQL Server with C#
  • How to compare two arrays of chars and see location?
  • Asynchronous code, shared variables, thread-pool threads and thread safety
  • Upgrading HTK GazeManager to MRTK v2 Pointer (HandRay...)
  • How to split an overridden method into multiple files?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk