Thursday, 31 January 2013

concatenate two strings in crystal report


Field exporer---- Formula field------create new


{Employee.employeeFirstName}+" "+{Employee.employeeMiddleName}+" "+{Employee.employeeLastName}

Tuesday, 29 January 2013

move listbox items to another listbox in c#

// all items moves from left listbox to right


int count = listBox1.Items.Count;//assigning items in listbox it into count variable
            if (count != 0)//checking the conditions
            {
                for (int i = 0; i < count; i++)
                {
                    listBox2.Items.Add(listBox1.Items[i]);
                }
            }

            listBox1.Items.Clear();//clear the listbox after transfering records.


//only selected item move to right listbox


if (listBox1.SelectedIndex > -1)
            {
                listBox2.Items.Add(listBox1.SelectedItem);//transfer records from listbox1 to Listbox2 on select records..
                listBox1.Items.Remove(listBox1.SelectedItem);

            }

//only selected item move to left listbox


if (listBox2.SelectedIndex > -1)
            {
                listBox1.Items.Add(listBox2.SelectedItem);
                listBox2.Items.Remove(listBox2.SelectedItem);
            }

// all items moves from right listbox to left


int count = listBox2.Items.Count;
            if (count != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    listBox1.Items.Add(listBox2.Items[i]);
                }
            }
            listBox2.Items.Clear();


display data in listview c#


string SQL = "SELECT departmentName FROM Department";
            source.View = View.Details;

            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand(SQL, con);
            OleDbDataReader r = null;

            con.Open();
            r = cmd.ExecuteReader();
            for (int i = 0; i <= r.FieldCount - 1; i++)
            {
                source.Columns.Add("Column " + (i + 1).ToString(), 200, HorizontalAlignment.Left);
            }
            while (r.Read())
            {
                ListViewItem lvItem = new ListViewItem(r[0].ToString());
                for (int i = 1; i <= r.FieldCount - 1; i++)
                {
                    lvItem.SubItems.Add(r[i].ToString());
                }
                source.Items.Add(lvItem);
            }
            con.Close();

display data in listbox c#


private void displayDataInListBox()
        {
            try
            {
                OleDbConnection connection = new OleDbConnection(connectionString);
                OleDbDataAdapter da = new OleDbDataAdapter();
                connection.Open();
                OleDbDataReader Dr;
                OleDbCommand command1 = new OleDbCommand("SELECT departmentName FROM Department", connection);
                command1.ExecuteNonQuery();
                Dr = command1.ExecuteReader();
                listBox1.Items.Clear();
                while (Dr.Read())
                {

                    listBox1.Items.Add(Dr.GetString(0).ToString());

                }
                Dr.Close();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message);
            }
          
        }

how to create multiple textbox at runtime in c#.net


public void createTextBox()
        {
            TextBox[] txtTextBox = new TextBox[10];

            for (int u = 0; u < txtTextBox.Count(); u++)
            {
                txtTextBox[u] = new TextBox();
            }
            int i = 0;
            foreach (TextBox txt in txtTextBox)
            {
                string name = "TextBoxName" + i.ToString();

                txt.Name = name;
                txt.Text = name;
                txt.Location = new Point(0, 32 + (i * 28));
                txt.Visible = true;
                this.Controls.Add(txt);
                i++;
            }
        }

Thursday, 13 December 2012

insert and retrive image from database in c#

Insert Image In Databse

public bool SaveImage()
        {
            FileStream fs = new FileStream(curFileName, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] rawData = new byte[fs.Length];
            fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
            try
            {
                if (curFileName != null)
                {
                    SqlCommand cmd = new SqlCommand("insert into ImageTable ([srno],[image]) values(@srno,@image)", con);
                    cmd.Parameters.Add("@srno", SqlDbType.NVarChar, 50).Value = textBoxSrNo.Text;
                    SqlParameter prm = new SqlParameter("@image", SqlDbType.Image, rawData.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, rawData);
                    cmd.Parameters.Add(prm);
                    con.Open();
                    cmd.ExecuteNonQuery();
                   
                    MessageBox.Show("Success");
                }
                else
                {
                    MessageBox.Show("Empty File Path");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
                con.Close();
            }
            return true;
        }


retrive image from database

public void DisplayImage()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT image FROM ImageTable WHERE srno='" + textBoxSrNo.Text + "'", con);
                SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sqlDA.Fill(ds, "ImageTable");
                int c = ds.Tables["ImageTable"].Rows.Count;
                if (c > 0)
                {
                    Byte[] byteBLOBData = new Byte[0];
                    byteBLOBData = (Byte[])(ds.Tables["ImageTable"].Rows[c - 1]["image"]);
                    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                    Image img;
                    //pictureBox1.Image = img.GetThumbnailImage(340, 165, null, new IntPtr());
                   
                   img = pictureBox1.Image = Image.FromStream(stmBLOBData);
                   pictureBox1.Image = img.GetThumbnailImage(200, 200, null, new IntPtr());
                    //pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                    pictureBox1.Show();

                    //Byte[] byteBLOBData = new Byte[0];
                    //byteBLOBData = (Byte[])(ds.Tables["ImageTable"].Rows[c - 1]["image"]);
                    //MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                    //pictureBox1.Image = Image.FromStream(stmBLOBData);
                    //pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                    //pictureBox1.Show();
                }

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

        }





Tuesday, 11 December 2012

conditional execution of two classes in c#

Below code is the best solution when, developer wish to execute 2nd class, only if 1st class  get executed successfully

Class 1st


public bool SaveOtherInfo()
        {
            try
            {
                con.Open();
                if (textBoxSrNo.Text == "" || textBoxCARD2.Text == "" || textBoxFirstName.Text == "")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('One or more field are empty..');", true);
                    return false;
                }
                if (FileUpload1.FileName == "")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Select Image');", true);
                    return false;
                }
                else
                {
                    param = " '" + this.textBoxSrNo.Text + "' ," +   
                            " '" + this.comboBoxRelocate.Text + " '";


                    strSQL = @"INSERT  INTO student VALUES (" + param + ")";

                    cmd.Connection = con;
                    cmd.CommandText = strSQL;
                    cmd.CommandType = CommandType.Text;

                    int i = cmd.ExecuteNonQuery();


                    if (i > 0)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Information submitted Successfully');", true);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + ex.Message + "');", true);
                return false;
            }
            finally
            {
                con.Close();
            }
            return true;
        }


Class 2nd

private bool SaveImage()
        {
            try
            {
                //Condition to check if the file uploaded or not
                if (FileUpload1.HasFile)
                {
                    //getting length of uploaded file
                    int length = FileUpload1.PostedFile.ContentLength;

                    //create a byte array to store the binary image data
                    byte[] imgbyte = new byte[length];

                    //store the currently selected file in memeory
                    HttpPostedFile img = FileUpload1.PostedFile;

                    //set the binary data
                    img.InputStream.Read(imgbyte, 0, length);
                    con.Open();

                    SqlCommand cmd = new SqlCommand("INSERT INTO ImageTable VALUES(@srno,@image)", con);
                    cmd.Parameters.Add("@srno", SqlDbType.NVarChar, 50).Value = textBoxSrNo.Text;
                    cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;

                    int count = cmd.ExecuteNonQuery();
                    con.Close();
                    if (count > 0)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Image Inserted Successfully');", true);

                    }
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + ex.Message + "');", true);
                return false;
            }
            finally
            {

            }
            return true;

        }

Button for checking classes execution and save data of both the classes if both classes executes successfully,

In short, If class"SaveOtherInfo" gets executed successfully then only class"SaveImage" get executed otherwise not..


protected void ButtonSave_Click(object sender, EventArgs e)
        {
            bool flag;
            flag = SaveOtherInfo();

            if (flag == true)
            {
                SaveImage();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Can not Insert Data');", true);
            }
        }