Click here to Skip to main content
15,888,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Anyone please guide.....

windows Application (C#)
Topic: TextBox DataBinding

Question: How to write back value (or Update) to DataBase (through DataBinding) from TextBox ???
Posted
Updated 31-Oct-13 0:15am
v5
Comments
[no name] 30-Oct-13 7:57am    
What you want to do a update or an insert in database using textbox's value?
DoingWork 30-Oct-13 11:31am    
Yes and i want to do it through DataBinging.
CodeBlack 30-Oct-13 8:02am    
what is your question and what you have tried ?
DoingWork 30-Oct-13 11:33am    
I want to do a update or an insert in database using textbox's value.
This insertion or updating should be through DataBinding.
DoingWork 30-Oct-13 11:41am    
I'm trying to update database but textBox's new value not updated in database.

Here is my Piece of code.

// Update button click event
private void btn_Update_Click(object sender, EventArgs e)
{
adptr.UpdateCommand = new SqlCommandBuilder(adptr).GetUpdateCommand();
adptr.Update(dataSet);
}

I wrote this code to solve your problem. It is really simple.
I wrote to add new rows to Access Database as I currently don't have Sql Server installed on my machine.
So just replace OleDb with Sql in this code.
Change your connection String.
And, replace the insert command with your Update Command and it will work.

protected void btn1_Click(object sender, EventArgs e)
    {
        Label1.Visible = false; //Label1 is the label to display exception
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%\\UpdatefromTB.accdb;Persist Security Info=False;");
        string OleDbStatement = string.Empty;
        OleDbStatement = "insert into UpdatefromTB (TBValue) values (\"" + TextBox1.Text + "\")";

        try
        {
            connection.Open();

            OleDbCommand cmd = new OleDbCommand(OleDbStatement, connection);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            Label1.Visible = true;
            Label1.Text = msg;
        }
            
        finally
        {
            connection.Close();
        }
    }


Mark this as Answer please because it is.

Thanks.
 
Share this answer
 
Comments
DoingWork 31-Oct-13 15:10pm    
Thanks for reply.
I know this approach but I want to do it through data binding approach. I've shared my piece of code. There is no error but data not updated in database.

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