简体   繁体   中英

Add a row into gridview in C# (azure database)

I have a webpage with a gridview that displays all rows in a certain table that is in my Azure cloud DB. This table called "students" and it contains the id (primary key), name, and gpa.

I also have 2 text boxes, one for name and one for gpa. The user should type some values to those boxes and click "add" button to add the new record to the table.

The problem is that I can't get it to work.

Here's what I have in the function ButtonAdd_click:

string InsertCommand = @"INSERT INTO [Students] ([Student_Id], [Student_Name], 
                         [GPA]) VALUES (@Student_Id, @Student_Name, @GPA)";
string connString = "<%$ ConnectionStrings:SQLAzureConnection %>";
using (SqlConnection conn = new SqlConnection(connString))
{
    using (SqlCommand comm = new SqlCommand())
    {
        comm.Connection = conn;
        comm.CommandText = InsertCommand;
        comm.Parameters.AddWithValue("@Student_Name", TextBox1.Text);
        comm.Parameters.AddWithValue("@GPA", TextBox2.Text);

        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
        }

**EDIT: Sorry the connection string wasn't right!! What I get now is that the page loads again, but the gridview stays the same

What is that mean? how can I do it right??

Having a look at your code:

You are adding two parameters but your SQL looks like it needs three ..

You are missing @Student_Id for a value as a parameter input.

My guess is also is that your try catch is empty so you never see the error.

James

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM