Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A page load problem wit a datagrid in c#

Options
  • 12-07-2006 3: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 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 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: 8,950 Mod ✭✭✭✭mewso


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


Advertisement