Showing posts with label csharp. Show all posts
Showing posts with label csharp. Show all posts

Tuesday, December 21, 2010

source code site list c#

here is some of c# source code site list .

http://csharp4newbies.com/csharp_programming.html

http://cplus.about.com/od/codelibraryfo2/A_Library_of_Software_written_in_C_with_full_source_code.htm

Thursday, November 11, 2010

datagridview control column row programitically

 Adding datagridview column row programitically And button control .


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                MessageBox.Show((e.RowIndex + 1) + "  Row  " + (e.ColumnIndex + 1) + "  Column button clicked ");
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btn);
            btn.HeaderText = "Click Data";
            btn.Text = "Click Here";
            btn.Name = "btn";
            btn.UseColumnTextForButtonValue = true;

            DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btn1);
            btn1.HeaderText = "Click Data";
            btn1.Text = "Click Here";
            btn1.Name = "btn";
            btn1.UseColumnTextForButtonValue = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {


                string rup = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

                if (rup == "Click Here")
                {
                    MessageBox.Show("this is test");
                }
               
                MessageBox.Show((e.RowIndex + 1) + "  Row  " + (e.ColumnIndex + 1) + "  Column button clicked ");
            }
        }

c# connecting mysql and select ,insert ,delete

The MySQL Connector/Net allows you to create a graphical Windows application that is data-based. When your application runs, it must connect to a MySQL database. To support this connection, you can use the MySqlConnection class that is defined in the MySql.Data.MySqlClient namespace. Before using this class, you can first include that namespace in your file.

How to include :
1. Add Reference
2. Browse
3. Select File 'MySql.Data.dll'
at
C:\Program Files\MySQL\MySQL Connector Net
1.0.3\bin.\NET 1.1
4. You put code
using MySql.Data.MySqlClient;

Create a connection with mysql database :

MySqlConnection conn = new MySqlConnection("Network Address=localhost;" + "Initial Catalog='test';"   +"Persist Security Info=no;" + "User Name='root';" + "Password=''");

After creating a connection string, to apply it and actually establish the connection, you must call the MySqlConnection.Open(). Its syntax is:
public void Open();
 
As you can see, this method doesn't take any argument. 
The MySqlConnection object that calls it is responsible 
to get the connection string ready. If the connection 
fails, the compiler would throw a MySqlException exception. 
If the connection string doesn't contain a data source or 
server, the compiler would throw an  
InvalidOperationException
exception. 
After using a connection and getting the necessary information from it, you should terminate it. To close a connection, you can call the MySqlConnection.Close() method. Its syntax is:

public void Close();


This method is simply called to close the current connection. While you should avoid 
calling the Open() method more than once if a connection is already opened, you can 
call the Close() method more than once.


 Select Data : 

 To read data from a mysql table add following code under a button . 


MySqlConnection conn = new MySqlConnection("Network Address=localhost;" + "Initial Catalog='test';" +"Persist Security Info=no;" +
                                                                "User Name='root';" +
                                                                "Password=''");

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader Reader;
            command.CommandText = "select name from student where id='1'";
            conn.Open();
            Reader = command.ExecuteReader();
            while (Reader.Read())
            {
                string thisrow = "";
                for (int i = 0; i < Reader.FieldCount;i++)
                    thisrow += Reader.GetValue(i).ToString() + ",";
                MessageBox.Show(thisrow);
            }
            conn.Close();







 Insert Data: 



 MySqlConnection conn = new MySqlConnection("Network Address=localhost;" + "Initial Catalog='test';" + "Persist Security Info=no;" +  "User Name='root';" +"Password=''");

            conn.Open();

            MySqlCommand command = conn.CreateCommand();

            command.CommandText = "insert into student(name,address)values('sanjoy','kaunia')";

            MySqlDataReader result = command.ExecuteReader();

            MessageBox.Show("successfully inserted");

            result.Close();

            conn.Close();






Delete Data:



MySqlConnection conn = new MySqlConnection("Network Address=localhost;" + "Initial Catalog='test';" + "Persist Security Info=no;" +
                                                                "User Name='root';" +
                                                                "Password=''");

            conn.Open();

            MySqlCommand command = conn.CreateCommand();

            command.CommandText = "delete from student where id='1'";

            MySqlDataReader result = command.ExecuteReader();

            MessageBox.Show("successfully deleted");

            result.Close();

            conn.Close();
       

Thursday, October 15, 2009

How to pass parameter in Crystal report from c#

To pass parameter programmeticaly create parameter in crystal report follow link below



just need to change following code

here code is used like that


private void button1_Click(object sender, EventArgs e)
        {             ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt");
             ParameterFieldDefinitions crParameterFieldDefinitions ;
            ParameterFieldDefinition crParameterFieldDefinition ;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
             crParameterDiscreteValue.Value = textBox1.Text;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["Customername"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;
             crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
             crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh(); 
         }

  

use like that no need to use cryrtp.load method

private void button1_Click(object sender, EventArgs e)
        {             ReportDocument cryRpt = new ReportDocument();
                         ParameterFieldDefinitions crParameterFieldDefinitions ;
            ParameterFieldDefinition crParameterFieldDefinition ;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
             crParameterDiscreteValue.Value = textBox1.Text;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["Customername"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;
             crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
             crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh(); 
         }

Wednesday, September 30, 2009

How to clear data gridview ?

To clear a data grid view rows . please add flowing code in any event handle

dataGridView1.Rows.Clear();


Monday, August 31, 2009

How tom make right click option in c# winform

To make a right click menu in c# winform application .


First take a new project from visual studio 2008 or any previous version . one winform will create for your project . in that form take contextmenustrip from tools menu in visual studio .
add item in context menu
then from your form properties select contextmenu property your one then run your program
and right click on your program you will see your added item in right click option .

How to view windows calculator from program c#

To view windows calculator from your program

  1. take a new project named - calculatorviewer solution explorer autometically created
  2. take a button in your form .
  3. use namespace System.Runtime.InteropServices;
  4. button click event write the folowwing code
[DllImport("shell32.dll")]
private static extern long ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

[DllImport("user32.dll")]
private static extern int FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

[DllImport("user32.dll")]
private static extern int GetParent(int hWnd);

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

//Gets window attributes
[DllImport("USER32.DLL")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

private const int WM_COMMAND = 0x0112;

private const int WM_CLOSE = 0xF060;

private const int BN_CLICKED = 245;

private const int GWL_ID = -12;

IntPtr hwndChild = IntPtr.Zero;

IntPtr hwnd = IntPtr.Zero;


System.Diagnostics.Process.Start("Calc");

run your project click the button windows calculator will show .

How to pass value one winform to another winform c#


To pass value parent form to child form you need to pass a value from parent form as a parameter
and need a public method in child form as follows

from parent form :

mychildform newchild=new mychildform(myvalue) ;
newchild.showdialog() ;

here i have pass a value from parent to child as parameter . we need to take one arguments when initialized child form .
public string globalvalue ;

public mychildform(string initialValue)
{
InitializeComponent();
ValueFromParent = initialValue;

}

then we need method to recieve the value

public string ValueFromParent
{
set
{
globalvalue= value;
}
}

Now You will get parent value to child form as name of globalvalue string variable .

how to make a form mdi c#

C# winform has multiple document interface facility .

to make a form mdi just go to form properties and make iscontiner =true it will be a multiple documents interface .

to call child form in mdi form write the following code

myform newmyform=new myform() ;

newmyform.mdiparent=this ;

newmyform.show() ;


to call form in child chil form

myform newmyform=new myform() ;
newmyform.mdiparent=this.mdiparent ;
newmyform.show()

Sunday, August 30, 2009

How to close previous form to new form open autometically C#


To close a form which is parent from child autometically follwing are the rules .
say we have two forms name login winform and another is mainform we want to go mainform when click on login button then mainform will open and login form will close autometically.

when login button click event follwong code will be -

private void btnlogin_Click(object sender, EventArgs e)
{
mainform newmainwindow = new mainform();

newmainwindow.Show(this);

this.Hide();
}

and then main form will load follwoing method need to call


protected override void OnClosing(CancelEventArgs e)
{
Owner.Close();
}

It will work .

How to Edit a Connection String C#

Editing Connection Strings Stored in Application Settings

You can modify connection information that is saved in application settings by using the Project Designer.

To edit a connection string stored in application settings

  1. In Solution Explorer, double-click the My Project icon (Visual Basic) or Properties icon (Visual C# or Visual J#) to open the Project Designer.

  2. Select the Settings tab.

  3. Locate the connection you want to edit and select the text in the Value box.

  4. Edit the connection string in the Value box.

    -or-

    Click the ellipses in the Value box to edit your connection with the Connection Properties dialog box. For more information.

You can modify connection information that is saved in code by using the Dataset Designer.

To edit a connection string stored in code

  1. In Solution Explorer, double-click the dataset (.xsd file) with the connection you want to edit.

  2. Select the TableAdapter or query with the connection you want to edit.

  3. In the Properties window expand the DefaultConnection node.

  4. To quickly modify the connection string, edit the ConnectionString property.

Saturday, August 29, 2009

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

SQL Date format is yyyy-mm-dd so so query will be
string mydate="10/04/2009";

datetime dt=convert.todatetime(mydate) ;

insert into mydatetime(mydatetime)values('"+ string.Format("{0:yyyy/MM/dd}",dt) +"')


it will work well .

Thursday, August 27, 2009

how to set combobox value from database c#

To show combo box value from database . to do that i have used a test table student and colum id and naem

id is int data type and name is varchar data type .

after creating sql table and make a suitable conncetion then apply these code to show combobox value from database .




dbconnection dbconn = new dbconnection();

SqlDataReader rdr = null;

DataTable dt = new DataTable();

string sqlquery = "select * from student where status='1'";

try
{
dbconn.connection.Open();

SqlCommand cmd4 = new SqlCommand(sqlquery, dbconn.connection);



rdr = cmd4.ExecuteReader();

dt.Columns.Add("Id");

dt.Columns.Add("name");



while (rdr.Read())
{


int id = (int)rdr["id"];

string name= (string)rdr["name"];

string comboid = Convert.ToString(id);



dt.Rows.Add(id, name);

}


dt.AcceptChanges();



this.comboBox1.DisplayMember = "name";

this.comboBox1.ValueMember = "Id";

this.comboBox1.DataSource = dt;



if (rdr != null)
{
rdr.Close();
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}




For this You need a combobox first take it from tools in your project tool bar .

how to make datagridview cell format in math round


To format a datagridview cell value in different format such as decimal to int or specially round format when colum is money data type in database .

to d0 that following code are neccessary .


for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
decimal each = decimal.Parse( this.dataGridView1.Rows[i].Cells[2].Value.ToString());

decimal decimalcorund = decimal.Round(each, 2);

this.dataGridView1.Rows[i].Cells[2].Value = decimalcorund.ToString();
}


Here for looping affaged by total grid raw counting when every loop is run then i m taking my expected colum value in a decimal format then decimal round and feet with value celles again in datagridview .

how to create event in C# banglahelp


c# এ ইভেন্ট তৈরী করতে প্রজেক্ট প্রোপার্টিজ এর event icon এ ক্লিক করতে হবে - নীচের ইমেজে আইকন দেখানো হলো -

তারপর আপনাকে event এর নাম অনুযায়ী ইভেন্ট box type করতে হবে । করার পর আপনাকে enter key press করতে হবে ।

enter key press করার পর vs আপনার জন্য একটি private method তৈরী করবে যেমন আপনি Textbox keypress করার জন্য একটি envent handeling method লেখতে চান ।


  1. আপনি আপনার প্রোজেক্ট প্রোপার্টিজ মেনু থেকে এভেন্ট এ ক্লিক করুন তারপর
  2. আপনি আপনার method এর নাম লিখুন সুবিধা জনক নিয়ম হচ্ছে প্রথম শব্দ সব সময় বড় হাতের অক্ষর হবে । যেমন আমি লেখতে চায় keypress তখন আমার textbox এর নাম অনুযায়ী হবে - textbox_KeyPress আপনাকে কে এভেন্ট প্রোপার্টজ এ KeyPress Type করতে হবে ।
  3. তাহলে textbox_KeyPress(object sender,eventargs e) নামের একটি method তৈরী হবে । এই ভাবে আপনি common event handing method ছাড়াও আর অনেক মেথড তৈরি করতে পারেন



Important Web Site Link For a Programmer


প্রোগ্রামারদের জন্য বিভিন্ন গুরুত্বপুর্ন ওয়েব তালিকা -

C#


c# প্রোগ্রামাদের জন্য নীচের ওয়েব সেখান বিভিন্ন ধরনের কোড সহায়তা পাবে -

How Enter key will work like tab key

Enter key কে টাব এর মতো কাজ করাতে প্রথমে আমাদের KeyPress event ব্যবহার করতে হবে ।

তারপর keypress event মেথড এর মাঝে নীচের কোড টি লেখতে হবে ।

if (e.KeyChar == (char)13)
{
SendKeys.Send("{tab}");

}

নীচের কোডে বিস্তারিত দেওয়া আছে -

private void txtagentname_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
SendKeys.Send("{tab}");
}


}

এখন আপনার টেক্সবক্স থেকে enter key press করলে এটি টাব কী এর মতো কাজ করবে