Adding SqlDataSource Control programatically in Asp.net 2.0

You can create the SqlDataSource control in Asp.net 2.0 dynamically. Always remember to add the control to the Page control using the Controls.Add method.
 

 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataSource s = new SqlDataSource();
        s.ID = "mySqlSourceControl";
        Page.Controls.Add(s); 
                
        s.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
        s.UpdateCommand = "UPDATE Person SET Name = @Name WHERE PersonID = 6";
        ControlParameter param = new ControlParameter();
        param.ControlID = "TextBox1";
        param.Name = "Name";
        param.PropertyName = "Text";
        s.UpdateParameters.Add(param);
        s.Update();
        
        s.DataBind(); 
        
           
    }


Print | posted @ Tuesday, July 19, 2005 8:09 PM

Twitter