Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some textboxes and a submit button.
I am able to retrieve data from database and shown them to the textboxes
I want to update this data and then to save them back to the database.
How can I do that?
What code should I use in the click handler of the button?
Posted

As you are saying you have successfully shown the data on textboxes and now you are trying to update same data, ok for that you have need to store any Primary Key value of particular row or shown data and store that value in Viewstate or session, after that on Button_Click you can get the value of all the textboxes and update the database by the stored value in the viewstate or sassion.

you can also store the Primary Key value in the Hidden Field or you can use query string also on the place of viewstate or sassion.
 
Share this answer
 
Retrieve your primary key value in Session varaibles and then write the following code in updatebtn_click

C#
Protected void updatebtn_Click(object sender, EventArgs e)
{

SqlCommand cmd= new SqlCommand("Update TableName Set Column1=@Column1,Column2=@Column2 Where ID=@ID",Conn);
//Conn is SqlConnection object that You have created.
cmd.Parameters.AddWithValue("@Column1",TextBox1.Text);
cmd.Parameters.AddWithValue("@Column2",TextBox2.Text);
cmd.Parameters.AddWithValue("@ID",txtid.Text);

try
{
Conn.open();
cmd.ExecuteNonQuery();
Conn.close();
}
Catch(Exception exp)
{
throw exp;
}
}

Hope, It will help you.
Thanks
 
Share this answer
 
v2
Comments
lovitaxxx 15-Nov-12 5:44am    
How can I retrieve the primary key value in session varaibles?
Sorry for my question but i am new at programming
faisal23 15-Nov-12 5:52am    
You are fetching data successfully from database as you said. So you supply some id or primary key on the basis of which you are able to fetch data right. So at the time of updation use same key to update particular record...
lovitaxxx 15-Nov-12 6:46am    
I retrieve data from the database using this code:
Dim con As New OleDb.OleDbConnection
Dim strCon As String = "Provider=SQLOLEDB; Data Source=; Initial Catalog=; User ID=; Password="

con.ConnectionString = strCon

con.Open()
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter

Sql = "SELECT * FROM Tablename"

da = New OleDb.OleDbDataAdapter(Sql, conn)
da.Fill(ds, "Tablename")
txtid.Value = ds.Tables("Tablename").Rows(inc).Item("2")

And then i navigate through records of te table using navigator buttons first,.. last.
I dont know now how to specify in which record I am in a specific moment(primary key) and then to update that record?
_Raj sinha 16-Nov-12 5:38am    
Are you developing a Windows Application or Web Application? Tell me first
_Raj sinha 16-Nov-12 5:51am    
If you are using windows app and retrieving the Primary key value on textbox then use the above solution that i have updated now. The code is in C#, you can convert it to VB
Hope it works..
Thanks
I think knowing the basic ADO.NET will enable you to do this and all DB operations very very easily. Here is a link that will definitely help you:

A Beginner's Tutorial for Understanding ADO.NET[^]
 
Share this answer
 

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