繁体   English   中英

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

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

我有一个带有gridview的网页,该网页显示我的Azure云数据库中某个表中的所有行。 该表称为“学生”,其中包含ID(主键),名称和gpa。

我也有2个文本框,一个用于名称,一个用于gpa。 用户应在这些框中键入一些值,然后单击“添加”按钮以将新记录添加到表中。

问题是我无法正常工作。

这是我在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();
        }

**编辑:很抱歉,连接字符串不正确! 我现在得到的是页面再次加载,但是gridview保持不变

那什么意识? 我该怎么办呢?

看一下你的代码:

您要添加两个参数,但是您的SQL看起来需要三个..

您缺少@Student_Id作为值输入的值。

我的猜测也是您的try catch为空,因此您永远不会看到错误。

詹姆士

暂无
暂无

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

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