We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,752 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Trouble Saving to Database in Visual Studio using C#

I was wondering if anyone could help me. I am working on a program in VS2008 with C# which has a datagridview populated from 2 SQL Express tables and needs to save to only 1 database.

Below is the code I have working so far:

public frmGrid()
    {
        BindingSource bs = Program.dbman.GetData();
        InitializeComponent();
        dataGridView1.DataSource = bs;
        bindingNavigator1.BindingSource = bs;
        //Above code works.  need a binding source for the dgv AND bn to both connect to

        //Creates an event handler for editing a cell (event handle below)
        dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

        //Makes the folowing columns read only
        dataGridView1.Columns["MixName"].ReadOnly = true;
        dataGridView1.Columns["MixId"].ReadOnly = true;

        //Rexizes specific columns
        dataGridView1.Columns["flcId"].Width = 50;
        dataGridView1.Columns["MixId"].Width = 50;
        dataGridView1.Columns["BatchNo"].Width = 50;

        //Adds filter for columns (remeber to add using statement)
        foreach (DataGridViewColumn col in dataGridView1.Columns)
        {
            col.HeaderCell = new
                DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
        }


    }

The code that selects my data for the datagridview is here:

  public BindingSource GetData()
    {
        string strSQL = "SELECT tblMix.MixName, tblCrushDatasheet.flcId, tblCrushDatasheet.MixId, tblCrushDatasheet.BatchNo, tblCrushDatasheet.Sizing, tblCrushDataSheet.KilnPosition, tblCrushDatasheet.tmpTempF, tblCrushDatasheet.FiringTemp, tblCrushDatasheet.FiringProfile, tblCrushDatasheet.FiringConditions, tblCrushDatasheet.FiredColor, tblCrushDatasheet.NoOfCrucibles, tblCrushDatasheet.ScreenedMethd, tblCrushDatasheet.ScreenedDate, tblCrushDatasheet.ScreenedTech, tblCrushDatasheet.SampleVolume, tblCrushDatasheet.SampleWeight, tblCrushDatasheet.LFDFunnelNo, tblCrushDatasheet.LFD1Tech, tblCrushDatasheet.LFD1Date, tblCrushDatasheet.LFD2Tech, tblCrushDatasheet.LDF2Date, tblCrushDatasheet.Plus40Weight, tblCrushDatasheet.PanWeight, tblCrushDatasheet.CrushScreenSet, tblCrushDatasheet.LbfFinal, tblCrushDatasheet.LbfMax, tblCrushDatasheet.PressureProfile, tblCrushDatasheet.CrushCellNo, tblCrushDatasheet.CrushTech, tblCrushDatasheet.CrushScreenTech, tblCrushDatasheet.FiredDate, tblCrushDatasheet.CrushDate, tblCrushDatasheet.DataSheetNo, tblCrushDatasheet.tmpTorF, tblCrushDatasheet.tmpCrushNo, tblCrushDatasheet.Notes, tblCrushDatasheet.Locked, tblCrushDatasheet.LockedBy, tblCrushDatasheet.ModUser, tblCrushDatasheet.ModDate FROM " + "tblMix LEFT JOIN tblCrushDatasheet ON " + "tblMix.MixId = tblCrushDatasheet.MixId";
        try
        {
            SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strConnString);
            SqlCommandBuilder commandbuilder = new SqlCommandBuilder(dataAdapter);

            System.Data.DataTable table = new System.Data.DataTable();
            dataAdapter.Fill(table);
            BindingSource dbBindSource = new BindingSource();
            dbBindSource.DataSource = table;



            return dbBindSource;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return null;
        }
    }

I am trying to configure a Save button on the form that will send all changes made on the datagridview to the database and have not found a solution that either works, or that I understand.

Any help would be greatly appreciated.

PS - I thought this would be the easiest part, but for me it clearly is not.

Thanks!!

1
Contributor
0
Replies
1
View
jrpaarlberg
Newbie Poster
1 post since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0524 seconds using 2.68MB