How to validate multi list - c#

I have the following code to select the record from the database:
public List<string>[] Select(string Command)
{
string query = Command;
//Create a list to store the result
List<string>[] list = new List<string>[2];
list[0] = new List<string>();
list[1] = new List<string>();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
list[0].Add(dataReader["NIK"] + "");
list[1].Add(dataReader["Password"] + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}
I have 2 column in my table, which is NIK and Password and the table has 2 rows which is 1,1 and 2,1.
How do I validate if the list contain NIK = 2 and Password = 1? How do I know if the select statement is successfully get the record from my table? How do I print the multi list into textbox ?

You should consider using Dictionary<string, string> instead of an array of List<string>s.
You can then print all the records:
foreach (var pair in dictionary)
Console.WriteLine(pair.Key + ", " pair.Value);
The first string in every dictionary pair is a key and the second one is a value.

how do i validate if the list contain NIK = 2 and Password = 1 ?
Go through the list and check. For example, using Enumerable.Any.
how do i know if the select statement is successfully get the record from my table ?
If no exception was thrown.
how do i print the multi list into textbox ?
Construct a string from the values returned from the database (e.g. using StringBuilder) and assign it to TextBox.Text.
BTW, you should really consider enclosing the reader and connection in using block. This way, resources will be deterministically freed even in the case of exception.
Also, consider using type-specific getters to read the data from the reader (such as GetString, GetInt32 etc.).

Related

Debug "ResultView" showing "Enumeration yielded no results"?

I have applied breakpoint and when I click on query "ResultView" its showing data but when I click 2nd time then data is empty and its showing Enumeration yielded no results. It is strange, is there any cache issue or something else? Why it is showing empty when I click after 1 time, its just show data 1st time,
var connectionString = String.Format(#"
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source={0};
Extended Properties=""Excel 12.0 Xml;HDR=YES""
", filePath);
//Creating and opening a data connection to the Excel sheet
using (var conn = new OleDbConnection(connectionString))
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "select * from [الحيازات$]";
using (var rdr = cmd.ExecuteReader())
{
//LINQ query - when executed will create anonymous objects for each row
var query =
(from DbDataRecord row in rdr
select row).Select(x =>
{
//dynamic item = new ExpandoObject();
Dictionary<string, string> item = new Dictionary<string, string>();
for (int i = 0; i < x.FieldCount; i++)
{
string data = "" + rdr.GetName(i) + ":{'id':'" + rdr.GetName(i) + "','code':'" + rdr.GetName(i) + "','title':'" + rdr.GetName(i) + "','type':'text','response";
item.Add(data, x[i] + "}");
}
return item;
});
//Generates JSON from the LINQ query
json = JsonConvert.SerializeObject(query, Formatting.Indented).Replace("'", "\"");// break point here
See similar question. Inspecting with the debugger calls Read(), which will try to go to the next row. Does your table only have 1 row?
The first time you view the data (when you can actually see it, not the second time when you get the Exception), are there multiple rows shown? If not, then you have an issue with your query as it is only returning one row.
If there are multiple rows, there may be an issue with using LINQ on a DataReader object. Full transparency, I haven't used LINQ to loop through a SqlDataReader before. This is how I would do it:
using (var rdr = cmd.ExectuteReader())
{
while(rdr.Read())
{
//do something with the first column
Console.WriteLine(rdr[0]);
}
}
If you want to use LINQ, you could always create a datatable to read from.
using (var rdr = cmd.ExectuteReader())
{
DataTable datatable = new DataTable();
datatable.Load(rdr)
}
...
var results = (from row in datatable...
The first time it works displaying a subset of your data. At the index of last successful row + 1 there might be a breaking record.
Then the enumerator will stop working.
In my specific case, calling the ToList() worked fine. (CsvHelper, CS0726)
https://github.com/JoshClose/CsvHelper/issues/1434

Adding SQL Statement with all tables and column names into a Dictionary

I need to save a sql SELECT Statement, which includes all tables and its columns in a database. The statement works fine and i can get all the names from the tables and columns i need.
The result looks as follows: (this is just psuedo-something)
table_Name Column_name
- CallerIP DT
- CallerIP ID
- CallerIP IP
- queueObject Action
- queueObject Attempt
- queueObject DestinationAddress
- queueObject ID
I thougt, i can save it into a Dictionary, where the tableName is a String, and the Colum_Names is a List of Strings
private Dictionary<string, List<string>> rowAndTables = new Dictionary<string, List<string>>();
this is my code, which should add all the tables and rows into the Dictionary
//Some code above, that doesnt matter here
command = new SqlCommand(sqlSelect, SqlConnector.getConnection());
command.Connection = SqlConnector.getConnection();
reader = command.ExecuteReader();
while (reader.Read()) {
if (tempTableName.Equals(reader.GetString(0)) == false) {
tempTableName = reader.GetString(0);
tempColumn = reader.GetString(1);
Console.WriteLine(tempTableName);
Console.WriteLine(tempColumn);
} else {
tempColumn = reader.GetString(1);
Console.WriteLine(tempColumn);
}
}
This doesnt do anything, besides printing all tables and columns.
The result looks as follows:
//CONSOLE...
CallerIP //Table
DT
ID
IP
queue_object //Table
Action
Attempt
DestinationAddress
ID
So the printing is fine.
Now I am struggeling with adding it into a Dictionary.
Can anyone help ?
Anything I did made no sense, and would just confuse anyone, I guess.
Well, if you want to fill the dictionary
private Dictionary<string, List<string>> rowAndTables =
new Dictionary<string, List<string>>();
you should modify your code slightly:
...
//DONE: wrap IDisposable (command) into using in order to release resources
using (var command = new SqlCommand(sqlSelect, SqlConnector.getConnection())) {
// Redundant, can be dropped
command.Connection = SqlConnector.getConnection();
using (var reader = command.ExecuteReader()) {
//TODO: check if the key and value are correct ones
string key = Convert.ToString(reader[0]);
string value = Convert.ToString(reader[1]);
// Do we have the key (and corresponding list) in the dictionary?
if (rowAndTables.TryGetValue(key, out var list))
// yes - we should add the value to the existing list
list.Add(value);
else
// no - we have to create key and list with value
rowAndTables.Add(key, new List<string>() {value});
}
}

Fetch Mysql data with Coma after each fetch C# Mysql

I am trying to fetch the data from mysql data base Column where say i have multiple rows data for specific column and i need to include coma after each row fetch.
before it was giving the data when i tried to add coma
Current Output after adding code Response.Write(name.Split(','));
System.String[]System.String[]System.String[]System.String[]System.String[]System.String[]System.String[]System.String[]System.String[]System.String[]System.String[]
My DB
Phone_Number School_id
1 SC1
2 SC1
3 SC1
4 SC1
Expected Output
1,2,3,4
My Fetch Query
string constr = ConfigurationManager.ConnectionStrings["Logging"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand MySqlCommand = new MySqlCommand("SELECT Phone_Number FROM Login where SchoolId='" + SessionManager.SchoolId + "'", con))
{
MySqlCommand.CommandType = CommandType.Text;
con.Open();
MySqlDataReader MySqlDataReader = MySqlCommand.ExecuteReader();
while (MySqlDataReader.Read())
{
string name = MySqlDataReader["FatherFullName"].ToString();
Response.Write(name.Split(','));
}
con.Close();
}
}
String.Split method splits your string with special character or string. This is not what you want.
You can add your Phone_Number values to a List<string> and you can use string.Join(string, IEnumerable<String>) method to generate comma separated values.
var list = new List<string>();
while(MySqlDataReader.Read())
{
string name = MySqlDataReader["FatherFullName"].ToString();
if(!string.IsNullOrEmpty(name))
{
list.Add(name);
}
}
Response.Write(string.Join(",", list)); // 1,2,3,4
You should always use parameterized queries by the way. This kind of string concatenations are open for SQL Injection attacks.
Two things more;
Default value CommandType is Text. You don't need to assign it explicitly.
You don't need to close your connection with con.Close(). Since you used using statement, it does that automatically.
String.Split is used to
split a string into substrings based on the strings in an array
You can use String.Join like this:-
//define a list of string
List<string> phoneNumbers = new List<string>();
while (MySqlDataReader.Read())
{
//add all the phone numbers to the list
string phoneNum = MySqlDataReader["FatherFullName"].ToString();
if(!String.IsNullOrEmpty(phoneNum))
phoneNumbers.Add(phoneNum);
}
//finally use Join method to get expected result
Response.Write(String.Join(",",phoneNumbers));
Also, please note your query is open for SQL Injection attack and you should consider using paramaterized query instead.
Next to the split/join, you can also get the result from MySQL directly:
SELECT GROUP_CONCAT(Phone_Number) FROM Login WHERE SchoolId = 'id';
This returns 1 single row with all phone numbers, seperated by comma's.

compare data from an array with numeric ID and insert it into a datagrid

I have a single array with random numbers as shown below
int[] numbers = new int[5] {2,5,3,7};
Also I have a table in my database with id and name in the ID have the same numerical values ​​to my array.
2 , Pedro
5 , Juan
3 , Claudio
7 , Gonzalo
I need to do is compare the numbers in the array with the id of the database, and if these are equal, showrange name associated in a datagrid
What thought is using a For loop on the array so,
for (int i = 0; i <numbers.length; i + +)
and show and thus obtain the names 'select value from table where id =' + numbers[i];
I need too much help, hope you can help me, thanks
Assuming you have valid connection you can use classes like SqlDataAdapter, DataTable and SqlConnection to fulfill a table with data you want. You also may use slightly different query to get results. Look at this code:
string numList = string.Join(",", numbers.Select(i=>i.ToString()).ToList());
// you need to have connection initialized with connection string
SqlDataAdapter a = new SqlDataAdapter("select value from table where id in (" + numList + ")", connection);
DataTable dt = new DataTable();// the result goes here
a.Fill(dt);// actually querying the database
That's it - you now have all your names from database which are corresponding to the list of IDs you have supplied. Now you can show that list in datagridview or datagrid.
So the question can be reduced to the ADO.NET part, how to retrieve the value from the id:
string sql = "SELECT value FROM dbo.table WHERE id = #id";
var pairs = new List<Tuple<int, string>>();
using (var con = new SqlConnection(yourConnectionString))
using (var cmd = new SqlCommand(sql, con))
{
con.Open();
foreach (int i in numbers)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("#id", i);
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
pairs.Add(Tuple.Create(i, dr.GetString(0)));
}
}
}
}
I have used a List<Tuple<int, string>> as collection to store all ID's and according names which you can use as datasource or further processing(i don't know if you only want to show the id's which have a name in your table). You can access the ID + name in this way:
foreach(var pair in pairs)
{
int id = pair.Item1;
string name = pair.Item2;
}
Note that i've used the using-statement to ensure that all unmanaged resources are getting disposed as soon as possible. That will also close the connection.
Always use parameters to prevent sql-injection or conversion issue even if you are currently not vulnerable to it.

How do I extract data from a DataTable?

I have a DataTable that is filled in from an SQL query to a local database, but I don't know how to extract data from it.
Main method (in test program):
static void Main(string[] args)
{
const string connectionString = "server=localhost\\SQLExpress;database=master;integrated Security=SSPI;";
DataTable table = new DataTable("allPrograms");
using (var conn = new SqlConnection(connectionString))
{
Console.WriteLine("connection created successfuly");
string command = "SELECT * FROM Programs";
using (var cmd = new SqlCommand(command, conn))
{
Console.WriteLine("command created successfuly");
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
conn.Open();
Console.WriteLine("connection opened successfuly");
adapt.Fill(table);
conn.Close();
Console.WriteLine("connection closed successfuly");
}
}
Console.Read();
}
The command I used to create the tables in my database:
create table programs
(
progid int primary key identity(1,1),
name nvarchar(255),
description nvarchar(500),
iconFile nvarchar(255),
installScript nvarchar(255)
)
How can I extract data from the DataTable into a form meaningful to use?
The DataTable has a collection .Rows of DataRow elements.
Each DataRow corresponds to one row in your database, and contains a collection of columns.
In order to access a single value, do something like this:
foreach(DataRow row in YourDataTable.Rows)
{
string name = row["name"].ToString();
string description = row["description"].ToString();
string icoFileName = row["iconFile"].ToString();
string installScript = row["installScript"].ToString();
}
You can set the datatable as a datasource to many elements.
For eg
gridView
repeater
datalist
etc etc
If you need to extract data from each row then you can use
table.rows[rowindex][columnindex]
or
if you know the column name
table.rows[rowindex][columnname]
If you need to iterate the table then you can either use a for loop or a foreach loop like
for ( int i = 0; i < table.rows.length; i ++ )
{
string name = table.rows[i]["columnname"].ToString();
}
foreach ( DataRow dr in table.Rows )
{
string name = dr["columnname"].ToString();
}
The simplest way to extract data from a DataTable when you have multiple data types (not just strings) is to use the Field<T> extension method available in the System.Data.DataSetExtensions assembly.
var id = row.Field<int>("ID"); // extract and parse int
var name = row.Field<string>("Name"); // extract string
From MSDN, the Field<T> method:
Provides strongly-typed access to each of the column values in the
DataRow.
This means that when you specify the type it will validate and unbox the object.
For example:
// iterate over the rows of the datatable
foreach (var row in table.AsEnumerable()) // AsEnumerable() returns IEnumerable<DataRow>
{
var id = row.Field<int>("ID"); // int
var name = row.Field<string>("Name"); // string
var orderValue = row.Field<decimal>("OrderValue"); // decimal
var interestRate = row.Field<double>("InterestRate"); // double
var isActive = row.Field<bool>("Active"); // bool
var orderDate = row.Field<DateTime>("OrderDate"); // DateTime
}
It also supports nullable types:
DateTime? date = row.Field<DateTime?>("DateColumn");
This can simplify extracting data from DataTable as it removes the need to explicitly convert or parse the object into the correct types.
Please consider using some code like this:
SqlDataReader reader = command.ExecuteReader();
int numRows = 0;
DataTable dt = new DataTable();
dt.Load(reader);
numRows = dt.Rows.Count;
string attended_type = "";
for (int index = 0; index < numRows; index++)
{
attended_type = dt.Rows[indice2]["columnname"].ToString();
}
reader.Close();
Unless you have a specific reason to do raw ado.net I would have a look at using an ORM (object relational mapper) like nHibernate or LINQ to SQL. That way you can query the database and retrieve objects to work with which are strongly typed and easier to work with IMHO.
var table = Tables[0]; //get first table from Dataset
foreach (DataRow row in table.Rows)
{
foreach (var item in row.ItemArray)
{
console.Write("Value:"+item);
}
}
Please, note that Open and Close the connection is not necessary when using DataAdapter.
So I suggest please update this code and remove the open and close of the connection:
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
conn.Open(); // this line of code is uncessessary
Console.WriteLine("connection opened successfuly");
adapt.Fill(table);
conn.Close(); // this line of code is uncessessary
Console.WriteLine("connection closed successfuly");
Reference Documentation
The code shown in this example does not explicitly open and close the
Connection. The Fill method implicitly opens the Connection that the
DataAdapter is using if it finds that the connection is not already
open. If Fill opened the connection, it also closes the connection
when Fill is finished. This can simplify your code when you deal with
a single operation such as a Fill or an Update. However, if you are
performing multiple operations that require an open connection, you
can improve the performance of your application by explicitly calling
the Open method of the Connection, performing the operations against
the data source, and then calling the Close method of the Connection.
You should try to keep connections to the data source open as briefly
as possible to free resources for use by other client applications.

Categories

Resources