3 Replies - 2819 Views - Last Post: 05 January 2012 - 12:31 PM Rate Topic: -----

#1 linkpedia  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 31-July 09

Problem using dataadapter methods in c#...

Posted 05 January 2012 - 04:16 AM

public partial class DataTrialForm : Form

    {

        private String connectionString = null;

        private SqlConnection sqlConnection = null;

        private SqlDataAdapter sqlDataAdapter = null;

        private SqlCommandBuilder sqlCommandBuilder = null;

        private DataTable dataTable = null;

        private BindingSource bindingSource = null;

        private String selectQueryString = null;



        public DataTrialForm()

        {

            InitializeComponent();

        }



        private void DataTraiForm_Load(object sender, EventArgs e)

        {

            

            sqlConnection = new SqlConnection("data source=SEZ-WS-137\\SQLEXPRESS2008;initial catalog=AttendanceData;user id=sa;password=pass");

            selectQueryString = "select top 20 * from Attendance  order by punchtime asc";



            sqlConnection.Open();



            sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);

            sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);



            dataTable = new DataTable();

            sqlDataAdapter.Fill(dataTable);

            bindingSource = new BindingSource();

            bindingSource.DataSource = dataTable;



            dataGridViewTrial.DataSource = bindingSource;

            

            // to hide Identity column

            dataGridViewTrial.Columns[0].Visible = false;

        }



        private void addUpadateButton_Click(object sender, EventArgs e)

        {

            try

            {

                sqlDataAdapter.Update(dataTable);

            }

            catch (Exception exceptionObj)

            {

                MessageBox.Show(exceptionObj.Message.ToString());

            }

        }



        private void deleteButton_Click(object sender, EventArgs e)

        {

            try

            {

               dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);

               sqlDataAdapter.Update(dataTable);

            }

            catch (Exception exceptionObj)

            {

               MessageBox.Show(exceptionObj.Message.ToString());

            }

        }

    }



I found this code on net & I have the following problem:
In my c# project I have a database class which handles all the insert, update , delete stuff... I have a form adminpanel where the user types the desired username in the textbox and the click the search button the button uses the database class to search the data.. I have function there in the database class which returns all the the DATATABLE filled with the results of the user query... Then I bind the DATATABLE to my forms datagridview & all works well... But I want that when the user updates or adds any data in the datagrid view & when the user press the update button on the form the data should be updated directly in the database... I took help from the above code but I dont want the whole code to be pasted again on my form. My database class return the datatable... I used the binding source to the datatable and attached the binding source at the datagridview datasource... I added this line in my update button code. da.update(dataTable) but its not working... I want to know anyway how to pass this datatable to my database class so tht my code works well... I have attached the following stuff of my project so that you can understand it more clearly...

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Problem using dataadapter methods in c#...

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Problem using dataadapter methods in c#...

Posted 05 January 2012 - 06:31 AM

We have a tutorial to do what you want.

http://www.dreaminco...iew-using-c%23/
Was This Post Helpful? 0
  • +
  • -

#3 linkpedia  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 31-July 09

Re: Problem using dataadapter methods in c#...

Posted 05 January 2012 - 11:04 AM

I have seen the tutorial but it didn't solve my problem. I don't want the whole sql connection code in a button. Rather than I will prefer to pass my updated datatable to my database class which in turn will interact with the database to reflect the changes. How to achieve it?
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Problem using dataadapter methods in c#...

Posted 05 January 2012 - 12:31 PM

There is nothing stopping you from using your database class. The tutorial shows you how to insert/update/delete from the DataGridView. You don't have to copy and paste the code. Review it, and put it into your own code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1