Bineesh  Viswanath

Bineesh Viswanath

  • NA
  • 1k
  • 780.6k

SAVE , UPDATE, VIEW, DELETE database using Windows Forms

Jul 1 2013 12:25 PM

Sir, I am now in a ADO.NET Program

There are Two Forms in My Program

1) frmClass and frmDivision

I want your help in the functions of frmDivision.

here is frmClass:-

 





and here is the frmDivision






So these are my forms.

now My Task is :-


1) Save className and divisionName to frmDivision and tbl_Division by btn_Save_Click event

please send me the code t

2) Update the dataGridView1 Data by cell_Double_Click event of dataGridView1 in frmDivision

here are the task I done in this program:-

  className  has been passed to comboBox in  the frmDivision


here is my database Stored procedure to save the data in frmDivision:-


ALTER PROCEDURE  divisionAdd
@divisionName nvarchar(20),
@classId numeric(18,0)
     
AS
    if not exists(select divisionName from tbl_Division where divisionName=@divisionName)
    begin
    insert into tbl_Division(divisionName,classId)
    values(@divisionName,@classId)
    end
    RETURN


this stored procedure is been called in SpDivision.here the spDivsion function to save the data in frmDivision:-


public void divisionAdd(divisionInfo infodivision)
        {
            try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                }
                SqlCommand sqlCmd = new SqlCommand("divisionAdd", sqlCon);
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.Add("@divisionName", SqlDbType.NVarChar).Value = infodivision.divisionName;
                sqlCmd.Parameters.Add("@classId", SqlDbType.Decimal).Value = infodivision.classId;
                int incount = sqlCmd.ExecuteNonQuery();
                if (incount > 0)
                {
                    MessageBox.Show("Saved Successfully");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sqlCon.Close();
            }
          
        }


This function is been called in frmDivision.cs.here is the code:-


        private void btnSave_Click(object sender, EventArgs e)
        {
            divisionSp spdivision = new divisionSp();
            divisionInfo info = new divisionInfo();
            info.classId = decimal.Parse(cmbClassName.SelectedValue.ToString()); // here I geetinan error. see the error next to the function
            info.divisionName = txtDivisionName.Text;
            spdivision.divisionAdd(info);
            divisionGridFill();
        }


So I want your help in this error.

My tbl_Division columns are:- divisionId(pk),divisionName,classId(fk)


The code in  infoDivision class is:-

  class divisionInfo
    {
        public decimal divisionId
        {
            get;
            set;
        }
        public string divisionName
        {
            get;
            set;
        }
        public decimal classId
        {
            get;
            set;
        }
      

    }


So Please help me in the following functions:-


1) save the className and divisionName to the frmDivision and to the tbl_Division

2) Update the datagridView1 data bt cellDoubleClick event


Answers (6)