Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am retrieving that value in from load i want to update by click the update button

C#
SqlConnection cn;
     SqlDataAdapter da;
     cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
     da = new SqlDataAdapter("select * from home", cn);
     DataSet ds = new DataSet();
     da.Fill(ds);
TextBox1.Text  = ds.Tables[0].Rows[0][1].ToString();
     TextBox2.Text = ds.Tables[0].Rows[0][2].ToString();
     TextBox3.Text = ds.Tables[0].Rows[1][1].ToString();
     TextBox4.Text = ds.Tables[0].Rows[1][2].ToString();
     TextBox5.Text = ds.Tables[0].Rows[2][1].ToString();
     TextBox6.Text = ds.Tables[0].Rows[2][2].ToString();
     TextBox7.Text = ds.Tables[0].Rows[3][1].ToString();
     TextBox8.Text = ds.Tables[0].Rows[3][2].ToString();
Posted
Updated 2-Sep-13 18:59pm
v2
Comments
abbaspirmoradi 2-Sep-13 3:33am    
you should explain your problem. we can't Guess what you want!
jitendra kachhi 2-Sep-13 3:51am    
i m displaying data in textbox through this code
SqlConnection cn;
SqlDataAdapter da;
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
da = new SqlDataAdapter("select * from home", cn);
DataSet ds = new DataSet();
da.Fill(ds);
TextBox1.Text = ds.Tables[0].Rows[0][1].ToString();
TextBox2.Text = ds.Tables[0].Rows[0][2].ToString();
TextBox3.Text = ds.Tables[0].Rows[1][1].ToString();
TextBox4.Text = ds.Tables[0].Rows[1][2].ToString();
TextBox5.Text = ds.Tables[0].Rows[2][1].ToString();
TextBox6.Text = ds.Tables[0].Rows[2][2].ToString();
TextBox7.Text = ds.Tables[0].Rows[3][1].ToString();
TextBox8.Text = ds.Tables[0].Rows[3][2].ToString();

now i want to update all textbox values after click the button
Thomas ktg 2-Sep-13 3:34am    
If you could give your question little more explained that would be well answered
syed shanu 2-Sep-13 3:38am    
What is ur problem.As for now ur displaying all the data from database to ur textbox in formload can you paste your update button click code here so that we can help you.
jitendra kachhi 2-Sep-13 3:45am    
i m displaying data in textbox through this code
SqlConnection cn;
SqlDataAdapter da;
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
da = new SqlDataAdapter("select * from home", cn);
DataSet ds = new DataSet();
da.Fill(ds);
TextBox1.Text = ds.Tables[0].Rows[0][1].ToString();
TextBox2.Text = ds.Tables[0].Rows[0][2].ToString();
TextBox3.Text = ds.Tables[0].Rows[1][1].ToString();
TextBox4.Text = ds.Tables[0].Rows[1][2].ToString();
TextBox5.Text = ds.Tables[0].Rows[2][1].ToString();
TextBox6.Text = ds.Tables[0].Rows[2][2].ToString();
TextBox7.Text = ds.Tables[0].Rows[3][1].ToString();
TextBox8.Text = ds.Tables[0].Rows[3][2].ToString();

now i want to update all textbox values after click the button

Write the update query to get the updated text from the textbox control
and pass the query to this method provided below. The method returns 1 when the command executes query successfully else it returns 0.

string query = "update tablename set col1=textname1,..... where condition";
C#
        public int ExecuteCommand(string query)
        {
            int RowsAffected = 0;
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            SqlCommand Command = null;
            try
            {
                Command = new SqlCommand(query, cn );
                RowsAffected = Command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "<br>" + query);
            }
            finally
            {
                Command.Dispose();
                Command = null;
                cn.Close();
                cn.Dispose();
            }
            return RowsAffected;
        }
</br>
 
Share this answer
 
v3
Comments
Herbisaurus 2-Sep-13 10:29am    
5+!
Thomas ktg 3-Sep-13 0:44am    
Thank you so much
hi..
this is the sample code for update

C#
protected void Update_Click(object sender, EventArgs e)//TO UPDATE THE EDITED RECORD
       {
 SqlCommand cmd = new SqlCommand();
              con.Open();
 string strQuery = "UPDATE Country_master SET  [CountryCode]='" + txt_countrycode.Text + "', [CountryName]='" + txt_countryname.Text + "',[IsActive]=" + active + " WHERE CountryId=" + txt_countryid.Text + " ";
                cmd = new SqlCommand(strQuery, con);
                cmd.ExecuteNonQuery();

}

hope this helps you
Happy Coding :)
 
Share this answer
 
Comments
Herbisaurus 2-Sep-13 10:29am    
5+!
Post the code that you posted in you question in a button click event..like
C#
protected void Update_Click(object sender, EventArgs e)
       {
        SqlConnection cn;
        SqlDataAdapter da;
        cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        da = new SqlDataAdapter("select * from home", cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        TextBox1.Text = ds.Tables[0].Rows[0][1].ToString();
        TextBox2.Text = ds.Tables[0].Rows[0][2].ToString();
        TextBox3.Text = ds.Tables[0].Rows[1][1].ToString();
        TextBox4.Text = ds.Tables[0].Rows[1][2].ToString();
        TextBox5.Text = ds.Tables[0].Rows[2][1].ToString();
        TextBox6.Text = ds.Tables[0].Rows[2][2].ToString();
        TextBox7.Text = ds.Tables[0].Rows[3][1].ToString();
        TextBox8.Text = ds.Tables[0].Rows[3][2].ToString()
       }
 
Share this answer
 
Comments
jitendra kachhi 2-Sep-13 11:08am    
i am displaying all values in textbox in my asp.page from my database.. now i want to some change on textbox values it should be update on 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