Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
public partial class frmBranchDetails : Form
    {
        SqlConnection con;
        SqlDataAdapter da1;
        DataSet ds1;
        SqlCommandBuilder cmb;


        public static int id1,hr1;
        int rno = 0;

        frmdgvBranchDetails frm1 = new frmdgvBranchDetails();
        

        public frmBranchDetails()
        {
            InitializeComponent();
        }
         
        private void frmBranchDetails_Load(object sender, EventArgs e)
        {
            id1 = frmdgvBranchDetails.id;
            hr1 = frmdgvBranchDetails.hr;
            con = new SqlConnection("user id=sa;password=arun;database=Branch Module");                                  
            show_data();
        }
        
        private void show_data()
        {
            
            da1 = new SqlDataAdapter("Select SNo,BranchName,BranchCode,Address1,Address2,Street,City,State,Country,ZipCode,Phone1,Phone2,Fax,EmailId,Website,Description from BranchDetails where SNo='" + id1 + "'", con);
            da1.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            ds1 = new DataSet();
            da1.Fill(ds1, "BranchDetails");


            if (ds1.Tables[0].Rows.Count > 0)
            {
                txtBranchName.Text = ds1.Tables[0].Rows[rno][1].ToString();
                txtBranchCode.Text = ds1.Tables[0].Rows[rno][2].ToString();
                txtAddress1.Text = ds1.Tables[0].Rows[rno][3].ToString();
                txtAddress2.Text = ds1.Tables[0].Rows[rno][4].ToString();
                txtStreet.Text = ds1.Tables[0].Rows[rno][5].ToString();
                txtCity.Text = ds1.Tables[0].Rows[rno][6].ToString();
                txtState.Text = ds1.Tables[0].Rows[rno][7].ToString();
                cmbCountry.Text = ds1.Tables[0].Rows[rno][8].ToString();
                txtZipCode.Text = ds1.Tables[0].Rows[rno][9].ToString();
                txtPhone1.Text = ds1.Tables[0].Rows[rno][10].ToString();
                txtPhone2.Text = ds1.Tables[0].Rows[rno][11].ToString();
                txtFax.Text = ds1.Tables[0].Rows[rno][12].ToString();
                txtEmail.Text = ds1.Tables[0].Rows[rno][13].ToString();
                txtWebsite.Text = ds1.Tables[0].Rows[rno][14].ToString();
                txtDescription.Text = ds1.Tables[0].Rows[rno][15].ToString();
            }
        }
        public void Save_Changes()
        {
            cmb = new SqlCommandBuilder(da1);
            da1.Update(ds1, "BranchDetails");
        }

        private void show()
        {

        }
        
        private void btnFirst_Click(object sender, EventArgs e)
        {
            rno=0;
            id1=1;//(record 1)
            show_data();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            
            rno=0;
            id1=id1-1;//previous record
            show_data();

        }

        private void btnLast_Click(object sender, EventArgs e)
        {
           rno = 0;
           id1 = 5;//Last record
           show_data();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            rno = 0;
            id1 = id1 + 1;//Next record
            show_data();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            id1 = ds1.Tables[0].Rows.Count + 1;
            txtBranchName.Text = "";
            txtBranchCode.Text = "";
            txtAddress1.Text = "";
            txtAddress2.Text = "";
            txtStreet.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            cmbCountry.Text = "";
            txtZipCode.Text = "";
            txtPhone1.Text = "";
            txtPhone2.Text = "";
            txtFax.Text = "";
            txtEmail.Text = "";
            txtWebsite.Text = "";
            txtDescription.Text = "";
            Save_Changes();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {            
            rno = 0;
            ds1.Tables[0].Rows[rno].Delete();
            MessageBox.Show("Record Deleted");
            Save_Changes();
            btnNext.PerformClick();
            
        }        
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (id1 == Convert.ToInt32(ds1.Tables[0].Rows.Count + 1))
            {
                DataRow dv = ds1.Tables[0].NewRow();
                dv[1] = txtBranchName.Text;
                dv[2] = txtBranchCode.Text;
                dv[3] = txtAddress1.Text;
                dv[4] = txtAddress2.Text;
                dv[5] = txtStreet.Text;
                dv[6] = txtCity.Text;
                dv[7] = txtState.Text;
                dv[8] = cmbCountry.Text;
                dv[9] = txtZipCode.Text;
                dv[10] = txtPhone1.Text;
                dv[11] = txtPhone2.Text;
                dv[12] = txtFax.Text;
                dv[13] = txtEmail.Text;
                dv[14] = txtWebsite.Text;
                dv[15] = txtDescription.Text;

                ds1.Tables[0].Rows.Add(dv);
                Save_Changes();
                MessageBox.Show("Record(s) Added");
            }
            else
            {
                id1 = id1;
                ds1.Tables[0].Rows[rno][1] = txtBranchName.Text;
                ds1.Tables[0].Rows[rno][2] = txtBranchCode.Text;
                ds1.Tables[0].Rows[rno][3] = txtAddress1.Text;
                ds1.Tables[0].Rows[rno][4] = txtAddress2.Text;
                ds1.Tables[0].Rows[rno][5] = txtStreet.Text;
                ds1.Tables[0].Rows[rno][6] = txtCity.Text;
                ds1.Tables[0].Rows[rno][7] = txtState.Text;
                ds1.Tables[0].Rows[rno][8] = cmbCountry.Text;
                ds1.Tables[0].Rows[rno][9] = txtZipCode.Text;
                ds1.Tables[0].Rows[rno][10] = txtPhone1.Text;
                ds1.Tables[0].Rows[rno][11] = txtPhone2.Text;
                ds1.Tables[0].Rows[rno][12] = txtFax.Text;
                ds1.Tables[0].Rows[rno][13] = txtEmail.Text;
                ds1.Tables[0].Rows[rno][14] = txtWebsite.Text;
                ds1.Tables[0].Rows[rno][15] = txtDescription.Text;

                Save_Changes();
                MessageBox.Show("Record(s) updated");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {           
            txtBranchName.Text = "";
            txtBranchCode.Text = "";
            txtAddress1.Text = "";
            txtAddress2.Text = "";
            txtStreet.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            cmbCountry.Text = "";
            txtZipCode.Text = "";
            txtPhone1.Text = "";
            txtPhone2.Text = "";
            txtFax.Text = "";
            txtEmail.Text = "";
            txtWebsite.Text = "";
            txtDescription.Text = "";
        }
    }
}
Posted
Updated 18-Nov-10 18:49pm
v2
Comments
[no name] 19-Nov-10 0:50am    
Ask a question don't just dump code. And if you do post code, format it correctly

1 solution

con = new SqlConnection("user id=sa;password=arun;database=Branch Module");

Never, ever use the SA account for data access.
da1 = new SqlDataAdapter("Select SNo,BranchName,BranchCode,Address1,Address2,Street,City,State,Country,ZipCode,Phone1,Phone2,Fax,EmailId,Website,Description from BranchDetails where SNo='" + id1 + "'", con);


Never use string concatenation to form a query. Use parameterized queries, or better yet, stored procedures.

And learn how to use DataBinding
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900