Tag Archives: postaday

Reading and Writing Json objects in C#


JSON objects are fast to read. From Python to C# JSON objects are widely used for the database as well as data transaction purposes, some of them are simple, some others are complicated.

So let begins with a simple C# Projects

JSON STRINGS in C#
SERIALIZATION OF JSON STRINGS in C#

Todo

  1. Build a class for constructing JSON string
  2. Write to JSON text file
  3. Read from the file back to JSON class

How to Property binding in C#


I love the data binding features rather than anything else in my visual studio. I am going to demonstrate binding feature in c#.

We have an MDI form called ‘Master’ and a Label on it and another child form in My C# Project. The child simply shows ‘company’ with ‘codes’.

Task: Want to display Company name on Master when the user double-click the company.

How to, do this, we can achieve this with global variables, maybe, but need to refresh the label every time. But the binding feature offers the better solution for this.

  1. We need to create the property to access label control  Text in master as follows
private string foo="My Company";
 public string Company
 { 
 
 set{

foo=value;
 
 }
 get { return foo; }
 }

Now place the label data binding code just below the Initialize section of the ‘Master’.

InitializeComponent();
lbl_company.DataBindings.Add("Text", this, "Company");

The code will work every time when the label Text changes. Actually, we need to change the value of the label when company property altered, which require a two-way binding. This is possible with the INotifyPropertyChanged interface.

Property Binding with Control -c#

How to data bind controls in C#

Our Master class need to be modified as follows Continue reading

How to Split strings using separator symbols in C#


We had some interesting string as follows which need to split and take the desired portion. C#.Net provides special string function called Split, which helps you broke complex strings into fragments.

Here are our Stings look like

121 | Whole Sale Rate

131 | Retail Rate

We begin with initialization of string and then build separator array, then call the split method. I guess you are familiar with the string array if you don’t just look at the 3rd line.

string  st=”121 | Whole Sale Rate”;

Here  “|” is the separator

string[] separator= { “|” };

string []selRateStr = null;

Let’s split the string with Split, which is the function available with string object also with ToString() too.

selRateStr =st.Split(separator,StringSplitOptions.None);

the first element in the selRateStr will be 121, which can be accessed as selRateStr [0];

Here is the authentic link from MSDN which may help you to learn Split function in detail.

 

 

Change DataGridView column style at runtime in C#


How to change DataGridview column style at runtime in Visual Studio C#.Net?

Simply get the column from the Gridview with the help of subclass of GridView. The code will look like

DataGridViewComboBoxColumn products = new DataGridViewComboBoxColumn();
products = (DataGridViewComboBoxColumn)dataGridView1.Columns[ITEM.Index];
products.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
products.FlatStyle = FlatStyle.Flat;

All changes made to the object products will change the dataGridView1.Columns[ITEM.Index] since the object point to the dataGridveiw column ITEM.

* It resembles pointers in C, where a value changes in a pointer also reflect the variable.

Ado.Net connection,adapter,dataset and dataview explained


I wrote this post for those who have no idea about C# database connection.

When starting with Visual Studio language like C#/VB.Net, you need to understand the concept of adapter, dataset and data view.

SQL connection

SQL connection class helps you to build the connection string.Visual studio can build connection string for you or  can create your in App config [Solution Explorer]

<add name=”MACCon” connectionString=”Data Source=(LocalDB)\MSSQLlocalDB;AttachDbFilename=e:\Developerm\c# Projects\MAcc_Prime\MAcc\MACCDATA.mdf;Integrated Security=True” />

Use SQL connection object to configure the connection as follows

con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[“MACCon”].ConnectionString;
con.Open();

Now you’re ready to use database

Data Adapter

The data adapter is the bridge between database tables with the application. The Adapter class helps you to the configugure table. The Adapter connects to the database throw the connection object.

geTradp = new SqlDataAdapter(“select dr,cr from accounttransactions where entry=” + mcode + ” and acid=” + acid + ” and tdate>='” + sdate + “‘ and tdate<='” + edate + “‘”, Common.con);

Here “conn” is the SQL database connection object

Dataset and Data Views

DataSet is the local version of your database table, you can work with the dataset and its data even if the connection has closed. Data Set accompanying with command builder also simplifies operation like adding new data, updating /deleting data etc

Dataset can also be used as data source for controls like data grid view.

Data View is a special object which offers table view so that you can fetch columns, rows, filter, extract rows with Find, FindRow methods loops through recordsets with the help subclasses of data view like Data RowView.

Let’s begin with .Net programming

 

 

 

Add column names to listview using dataSet in C#


When we need to populate various reports, usually we also need to supply customized column names too. We can easily grab column name from the database using objects like DataSet in C#

In our example, we use SQL database objects as follows.

AccountReportAdapter = new SqlDataAdapter(sql, con);
AccountReportDS = new DataSet();
AccountReportAdapter.Fill(AccountReportDS);

Get started with a foreach statement and store the names into a List collection object.

List<string> hds = new List<string>();

hds.Clear();
foreach (DataColumn c in Common.AccountReportDS.Tables[0].Columns)
{
hds.Add(c.ColumnName.ToString());
}

The hds list simply store the column name, just like an array does. Later we can use the list for supplying heads for ListView  as follows

public void ReportHeader(List<string> ar)
{
ColumnHeader headers = new ColumnHeader();
listView1.View = View.Details;
headers.Text = ” “;
headers.Width = 0;
listView1.Columns.Add(headers);
foreach (var htext in ar)
{
headers = new ColumnHeader
{
Text = htext.ToString(),
Width = htext.ToString().Length * 20
};
listView1.Columns.Add(headers);
}
}

if (hds != null) ReportHeader(hds);

 

Accessing controls in a panel -C#


Accessing controls using collection is pretty simple in C#. It comes handy when you want to perform group tasks.

I have a panel which contains many text boxes and Labels. Some of the controls used to format the result. Others name start with txt_ and lbl_. Our task is to clear the Text property with “”/null.

2018-01-05_220714

We can iterate through panel controls as follows

foreach(Control c in panel_rateSettings.Controls )
{
if ( c.Name.Contains(“txt_”) || c.Name.Contains(“lbl_”))
{
c.Text=null;
}
}

Maybe you can find another use of the control collection.

Remove OEM Recovery Drive letter in Windows 10


Branded PC like HP/Lenovo used to set recovery partition[Windows 10/7], probably with drive letter D/E/F etc. Sometimes there are applications which always work with specific drives. In such cases when you try to change OEM partition, the effort may fail.

 OEM partition is designed for system recovery or factory restore. It allows users to easily and quickly restore the system to the original state when system failure or system crash occurs

I personally suggest removing the drive letter from the OEM part, this becomes another troublesome job indeed. With DiskPart command you can remove drive letter of volumes.

2018-01-05_204602

Enter cmd command(Window + R) and Run button

Type diskpart on your command prompt

Type List Volume

Choose the OEM voliume with select Volume [volume#]

Execute Remove letter [Drive Letter]

If you are too lucky the OEM drive will disappear from My computer/My Pc. and the drive letter is available for use.