giovedì 31 ottobre 2013

C# - How to Bind ASP TextBox in TextMode = Password

If you have an Asp TextBox in TextMode = Password for example:


<asp:TextBox  id="txt_password" runat="server" TextMode="Password" ></asp:TextBox>

you can't do simply  txt_password.Text = "yourValue" to bind it, but you need to do as follow:

txt_password.Attributes.Add("value", "yourValue");


My Two Cents ...

C# - SqlBulkCopy

SqlBulkCopy lets you efficiently bulk load a SQL Server table with data from another source.
For example if you need to synchronize differente Sql Server Tables or to put data from DataTable (or DataSource) to Sql Server Table in a very efficient way.

Here a Little Example:



public void UpdateTable(SqlConnection con, SqlTransaction tra, DataSet ds, string table)
        {
            string sql = "DELETE FROM " + table;

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = sql;

            if (tra != null)
                com.Transaction = tra;

            com.ExecuteNonQuery();

            SqlCommand Command = new SqlCommand();
            Command.Connection = con;
            if (tra != null)
                Command.Transaction = tra;

            SqlBulkCopy copy = new SqlBulkCopy(con, SqlBulkCopyOptions.Default, tra);
            copy.DestinationTableName = table;
            copy.WriteToServer(ds.Tables[0]);
        }

with this cose, you can copy data from a ds to Sql Server Table. 

My Two Cents ...  

mercoledì 4 settembre 2013

c# - WPF, Binding DataGrid with DataSet

Hi,
the following code shows how you can bind a DataGrid with a DataSet in WPF.



YourWpfDataGrid.ItemsSource = YourDataSet.Tables["YourTable"].AsDataView();



My Two Cents ...

giovedì 8 agosto 2013

C# - Writing Only Number in a TextBox

If you have a Text Box and you want that your users write only numbers, you can use the following code:




private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) &&                                     !char.IsPunctuation(e.KeyChar))
                e.Handled = true;
        }


How you can understand the code is in the "KeyPress" event of the TextBox. 



My Two Cents ...