Console.WriteLine conversion to string - winforms

In Csharp its normal to write to the console like below, i am translating an old console app to a gui based application and now i wonder if this can be turned simply in strings; so i can do a quick rename, of console.Writeline
var p = new { FirstName = "Bill", LastName = "Gates" };
Console.WriteLine("Fristname {0} Lastname {1}", p.FirstName, p.LastName);
I want something like that, i'll like to keep using that format, but turn it to a normal string
so you get
tbTextBox1.text = tbTextBox1.text + ("Fristname {0} Lastname {1}", p.FirstName, p.LastName)
Is there a simple string conversion function for that ?
I know i could write it like below but if there is a simple conversion i would prefer that
tbTextbox1.text= "Firstname " + p.Firstname + " Lastname " + p.lastname;

You can try this
tbTextBox1.text += String.Format("Fristname {0} Lastname {1}", p.FirstName, p.LastName)

You're looking for String.Format(), which does exactly that.

Related

How to Write SOQL for Distinct of combination of Level_1__c,Level_2__c and Level_3__c

I would like to know if there is possibility of writing a SOQL similar like we do in SQL
like Distinct of Level_1__c,Level_2__c and Level_3__c
Currently SOQL is
select Case__c, Level_1__c, Level_2__c,Level_3__c FROM ERT_Case_Type__c
How to rewrite to include Distinct of Level_1__c,Level_2__c and Level_3__c
Thanks & Regards,
Carolyn
I think for your use case, you will need Apex. Something like the following:
Map<String, Set<String>> distinctLevels = new Map<String, Set<String>>{
'Level_1__c' => new Set<String>(),
'Level_2__c' => new Set<String>(),
'Level_3__c' => new Set<String>()
};
Set<String> levelApiNames = new Set<String>{'Level_1__c', 'Level_2__c', 'Level_3__c'};
for(ERT_Case_Type__c caseType : [SELECT Case__c, Level_1__c, Level_2__c, Level_3__c FROM ERT_Case_Type__c]){
for(String level : levelApiNames){
distinctLevels.get(level).add(caseType.get(level));
}
}
for(String level : levelApiNames){
System.debug('Number of distinct ' + level + ' values: ' + String.valueOf(distinctLevels.get(level).size());
//System.debug('List of distinct ' + level + ' values: ' + distinctLevels.get(level).toString());
//System.debug(distinctLevels.get(level).size()); //as integer
}
Loop over query results
Loop over each field API name you want to track
Add the values for each field to the set inside the map. The set will only have unique values
Do something with the map values for each field API name
This is not tested, but the high-level outline should work for you.

Is IsNullable always trune and IsAutoincrement always false for Npgsql?

I have a program written in C# using VS2012 that automatically builds wrapper classes for database tables. I am trying to update it to make sure that null values are handled intelligently (a new concept for my company). Most of our work is done using PostgreSQL, and we usually use ODBC. I want my program to be able to recognize nullable and autoincrement columns. The DataColumn class includes IsNullable and IsAutoincrement properties. I created a little table with samples of each type of column. Using ODBC, all columns were found to be nullable and not autoincremented. I thought that was because ODBC doesn't implement everything, so I tried it with the latest version of Npgsql. I was surprised to see that Npgsql also reported everything nullable and not autoincrementing. Is there something I need to do to have those properties be set?
Here's my table definition:
CREATE TABLE nullable_test
(
key_field bigserial NOT NULL,
non_nullable_integer integer NOT NULL,
nullable_integer integer
)
WITH (
OIDS=FALSE
);
And here's my test program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Odbc;
using Npgsql;
namespace NullableTest
{
class Program
{
static void Main(string[] args)
{
try
{
NpgsqlConnection npgConn = new NpgsqlConnection();
NpgsqlConnectionStringBuilder connStringBuilder = new NpgsqlConnectionStringBuilder();
connStringBuilder.Host = "localhost";
connStringBuilder.Database = "Stripco";
connStringBuilder.Username = "caps";
connStringBuilder.Password = "asdlkjqp";
npgConn.ConnectionString = connStringBuilder.ToString();
npgConn.Open();
Console.WriteLine("Database open using Npgsql");
DataSet npgsqlDataSet = new DataSet();
NpgsqlDataAdapter npgsqlAdapter = new NpgsqlDataAdapter("select * from nullable_test", npgConn);
npgsqlAdapter.Fill(npgsqlDataSet);
Console.WriteLine("Data set is filled.");
DataTable table = npgsqlDataSet.Tables[0];
DataColumn keyColumn = npgsqlDataSet.Tables[0].Columns["key_field"];
DataColumn nonNullableColumn = npgsqlDataSet.Tables[0].Columns["non_nullable_integer"];
DataColumn nullableColumn = npgsqlDataSet.Tables[0].Columns["nullable_integer"];
Console.WriteLine("Key column is " + (keyColumn.AutoIncrement ? "" : " not ") + " autoincrementing");
Console.WriteLine("Key column is " + (keyColumn.AllowDBNull ? "" : " not ") + " allowing nulls.");
Console.WriteLine("Non-nullable column is " + (nonNullableColumn.AutoIncrement ? "" : " not ") + " autoincrementing");
Console.WriteLine("Non-nullable column is " + (nonNullableColumn.AllowDBNull ? "" : " not ") + " allowing nulls.");
Console.WriteLine("Nullable column is " + (nullableColumn.AutoIncrement ? "" : " not ") + " autoincrementing");
Console.WriteLine("Nullable column is " + (nullableColumn.AllowDBNull ? "" : " not ") + " allowing nulls.");
npgConn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Failed to open database: " + ex.Message);
}
Console.WriteLine("Press any key");
Console.ReadKey();
}
}
}
You have to understand the difference between getting metadata information on a resultset (what you seem to be doing) vs. getting info on a table column - the two aren't the same.
When you get metadata for a table (via NpgsqlConnection.GetSchemaTable(), Npgsql goes and finds all the information it can, including null ability and auto-increment. However, when getting information about a resultset, Npgsql has almost no info provided by PostgreSQL and cannot know whether it's nullable or autoincrement.
So to get all the info, use NpgsqlConnection.GetSchemaTable().
I don't know a lot about datatables, but my guess is since the source is the result of a query, the query itself does not reveal if the column from source is nullable or not. For example, if you had inserted joins, literals or incorporated views, it would become increasingly more difficult to determine where each column actually came from. I don't think the result set itself knows or cares if a column is nullable or not.
Pre version 10 instances of PostgreSQL don't have a native identity type, to the best of my understanding. They accomplish the same thing, in my opinion, but they sort of sew the pieces together the way we did back in the day. As such, I don't know that you can definitely determine that about a column. That said, if you make reasonable assumptions, you can probably get close.
For both of your needs, I'd just go ahead and use the informatio_schema.columns table. Something like this would definitely address the nullable aspect, and it will get you 90% there for the identity:
select
is_nullable = 'YES',
column_default like 'nextval%'
from information_schema.columns
where
table_schema = :SCHEMA and
table_name = :TABLE_NAME and
column_name = :COLUMN
And the C# implementation would look something like this:
private bool IsSerial(string Schema, string Table, string Column)
{
bool result = false;
NpgsqlCommand cmd = new NpgsqlCommand(Resources.Sql, Connection);
cmd.Parameters.AddWithValue("SCHEMA", Schema);
cmd.Parameters.AddWithValue("TABLE_NAME", Table);
cmd.Parameters.AddWithValue("COLUMN", Column);
NpgsqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
result = reader.GetBoolean(1);
}
reader.Close();
return result;
}
And you could change that GetBoolean(1) to a GetBoolean(0) for the nullable piece.
My C# method looks little rough around the edges, but hopefully it's enough to get you there.

How to set two FilterString condtion on code Reports Winforms Devexpress?

am using devexpress winforms reports, I can able to set one filterString condition in code, this code is working successful
report.FilterString = "CompanyId =" + compid;
and
DetailReport.FilterString = "CompanyId=" + compid;
But now I need to set for two conditions I tried this code but it didn't filter display all
values.
report.FilterString = "[CompanyId = " + compid +"] AND [ InvoiceStatus =" + status + "]";
Help me how to solve this ?
The simplest filter syntax looks like this: "[FieldName] = Value".
Thus, change your code as follows:
report.FilterString = string.Format("[CompanyId] = {0} AND [InvoiceStatus] = {1}", compid, status);
Thanks to all. Finally solved by this code and working fine for two filters using correct string formats.
report.FilterString = "InvoiceStatus = '" + status + "' AND CompanyId = " + compid;

Searching in database from a form returning empty input

I want to do search in my database using some fields filled by a form
but if some fields are left empty i don't want to include them
which kind of query can help me in achieving this??
Currently i am using a query like:
Select * from DATABASE where COLUMN='form_input';
but as my form will return empty it will try and select rows which have null entries but rather i want to this time see a result of all rows in database i.e i want to invalidate the filter by COLUMN='form_input'
As we do not know your server side scripting language -
The psheuducode should be -
if(request['form_input']!=null)
Select * from DATABASE where COLUMN='form_input';
else
Select * from DATABASE;
Also If there are many fields for form_input then we can design
our code something like -
String wherequery = "";
if(request['form_input1']!=null)
{
wherequery = wherequery + " COLUMN='form_input1' ";
}
if(request['form_input2']!=null)
{
wherequery = wherequery + " And "
wherequery = wherequery + " COLUMN='form_input2' ";
}
if(request['form_input3']!=null)
{
wherequery = wherequery + " And "
wherequery = wherequery + " COLUMN='form_input3' ";
}
....
And so on
....
String selectQuery = "";
if(wherequery == "")
{
selectQuery = "Select * from TABLE";
}
else
{
selectQuery = "Select * from TABLE where" + wherequery ;
}
execute (selectQuery);
Please note we are using pseudo code here. We can take the form inputs and concatenate query for each input which is not null.
If we find the concatenated string as blank string, we will select the full table.
Otherwise
we will select with the where clause query.
Hope, this help you out.

How to use named parameters inside LIKE operator pattern in Adobe Air

I wanted to use a named parameter placeholder inside the LIKE operator pattern so that the argument string is properly escaped.
Here's my modified code where I am using the at-param placeholder:
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE '%#search%';";
stmt.parameters["#search"] = "argument string";
stmt.execute();
Doing so yields an SQLError with the following details
message: Error #3315: SQL Error.
details: '#search' parameter name(s) found in parameters property but not in the SQL specified
As suggested by Mike Petty, I tried:
stmt.text = 'SELECT * FROM comments WHERE title LIKE "#%search%";';
Which yields to the same SQL Error details.
Documentation has this:
expr ::= (column-name | expr) LIKE pattern
pattern ::= '[ string | % | _ ]'
My suspicion is that it is skipped due to the qoutes, any ideas on how to make it work?
Found a solution for this, basically instead of doing it like this:
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE '%#search%';";
stmt.parameters["#search"] = "argument string";
stmt.execute();
You have to put a placeholder for the entire LIKE operator pattern and bind the pattern as a parameter.
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE #search;";
stmt.parameters["#search"] = "%" + userInput + "%";
stmt.execute();
Remove your :string from your string.
Like works just fine as:
SELECT * FROM comments WHERE comments.title LIKE '%string%';
It's hard to tell from your question, but is your current statement either throwing a SQLError, just doesn't compile, or just doesn't return any results?
If I had to guess I'd say you have a few issues here:
You shouldn't have to qualify the column in the where clause since you only have 1 table to select from
The parameter string is technically a reserved word by AIR / Actionscript (although the case is different) - still a bit confusing.
Even though the documentation and code allow you to use either the colon, or at-symbol, my preference is the # since it's a bit easier to see ;)
Don't forget to add an itemClass definition of the Class you expect for rows - this helps to avoid anonymous objects.
This should be fairly straight forward:
var stmt:SQLStatement = new SQLStatement();
stmt.itemClass = Comment;
stmt.text = 'SELECT * FROM comments WHERE title LIKE "#%search%";';
stmt.parameters['#search'] = userInput.text;
stmt.addEventListener(SQLEvent.RESULT, onResults);
stmt.execute();

Resources