简体   繁体   English

在C#(azure数据库)的gridview中添加一行

[英]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. 我有一个带有gridview的网页,该网页显示我的Azure云数据库中某个表中的所有行。 This table called "students" and it contains the id (primary key), name, and gpa. 该表称为“学生”,其中包含ID(主键),名称和gpa。

I also have 2 text boxes, one for name and one for gpa. 我也有2个文本框,一个用于名称,一个用于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: 这是我在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 我现在得到的是页面再次加载,但是gridview保持不变

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 .. 您要添加两个参数,但是您的SQL看起来需要三个..

You are missing @Student_Id for a value as a parameter input. 您缺少@Student_Id作为值输入的值。

My guess is also is that your try catch is empty so you never see the error. 我的猜测也是您的try catch为空,因此您永远不会看到错误。

James 詹姆士

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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