Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

A page load problem wit a datagrid in c#

  • 12-07-2006 03:14PM
    #1
    Closed Accounts Posts: 181 ✭✭


    I have a web form that allows users to enter in details and insert it into the database. I have the gridview invisible at the start and when they enter in there details I want to display the gridview.

    The problem is is that after the insert event it does not display the gridview, below is my code
    protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                GridView1.Visible = true;
            }
                    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
           string input = TextBox1.Text.ToString();
    
           Parameter param = new Parameter ("value", TypeCode.String, input);
    
           SqlDataSource1.SelectParameters.Add(param);
                    
           SqlDataSource1.SelectCommand = "INSERT INTO Player (Name) VALUES (@value)";
    
           GridView1.DataBind();
    
              }
    

    I tired adding another grid view and displaying it in the page load but it stopped my sql insert statement working so I deleted it and restorted back to this code! Does anyone have any solutions to my problem?


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    <edit>
    Can you post the HTML code for the GridView?
    <edit again>
    Seems I may have been right the first time try this:
        protected void Button1_Click(object sender, EventArgs e)
        {
           string input = TextBox1.Text.ToString();
    
           Parameter param = new Parameter ("value", TypeCode.String, input);
    
           SqlDataSource1.SelectParameters.Add(param);
                    
           SqlDataSource1.SelectCommand = "INSERT INTO Player (Name) VALUES (@value)";
           
           [color=red]GridView1.DataSource = SqlDataSource1;[/color] // or what ever the C#2.0 code should be
           GridView1.DataBind();
    
              }
    


  • Closed Accounts Posts: 181 ✭✭deadfingers


    I am sorry Evil Phil what do mean by datasource? Since I posted the OP I created a blank datagrid and then when I performed my insert command I tired to bind the data then but still with no results.

    Is it best pratice to use a different data source for all commands (i.e. insert, select, update) ??


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Try my code above first and see if that works. But it mightn't as you setting the SelectCommand property to an Insert statement. I don't use the SqlDataSource controls so I've no idea what this is going to do.


  • Moderators, Science, Health & Environment Moderators Posts: 9,204 Mod ✭✭✭✭mewso


    Parameter param = new Parameter ("value", TypeCode.String, input);


Advertisement