updating database from textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdulhadi
    New Member
    • Apr 2010
    • 2

    #1

    updating database from textbox

    hi
    I am trying to update the table records by intering a new values in a textbox
    and am facing problems in the code of dataset
    how could I perform that
    thanx
    Attached Files
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I can't open the file you attached.
    You should post any code that you are having problems with in the thread (because apparently some people can't open the files attached). When you post code please post it within code tags.

    Anyways, you need a TextBox on the page. Then you need to access the Text in the TextBox (using the TextBox.Text property). Once you've got that you should use it to update your database.

    The best way to supply this value (that has been provided by the end user) is to use the SqlCommand.Para meters property (if you're using the SqlCommand object).


    This topic is covered nicely in:
    How to use database in your program Part 1
    How to use database in your program Part 2

    -Frinny

    Comment

    • CroCrew
      Recognized Expert Contributor
      • Jan 2008
      • 564

      #3
      This is what I got from abdulhadi's attached file:

      Code:
      protected void Button1_Click(object sender, EventArgs e)
          {
              SqlConnection conn;
              conn = new SqlConnection();
              conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\matches.mdf;Integrated Security=True;User Instance=True";
      
              conn.Open();
      
              SqlCommand selectcmd;
              selectcmd = new SqlCommand();
              selectcmd.Connection = conn;
              string selecting;
              selecting = "select * from statistics where club=@club";
              selectcmd.Parameters.Add("club", TextBox1.Text);
              selectcmd.CommandText=selecting;
      
      
              SqlCommand upcmd;
              upcmd = new SqlCommand();
              upcmd.Connection = conn;
              string adcmd;
              adcmd = "Update statistics Set goals=goals+2,points=points+3,win=win+1)";
              upcmd.CommandText = adcmd;
      
              SqlDataAdapter da;
              da = new SqlDataAdapter();
              da.SelectCommand = selectcmd;
              
      
              DataSet newrecord;
              newrecord = new DataSet();
              da.Fill(newrecord);
              da.UpdateCommand = upcmd;
              da.Update(newrecord);
              GridView1.DataSource = newrecord;
              GridView1.DataBind();
      CroCrew~

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Ahh, it's line 14 that's causing the problem :)
        You need to look at how to use the SqlParameterCol lection.Add method.


        It should be something like:
        Code:
        sqlCom.Parameters.Add("@club",  SqlDbType.VarChar).Value =  TextBox1.Text;

        Comment

        Working...