如何将特定单元格中的gridview文本插入数据库 [英] How to insert gridview text in a particular cell into database

查看:105
本文介绍了如何将特定单元格中的gridview文本插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将第二行的第二列中的内容插入到我的数据库表中,但它会插入一个空的空格。

请帮我这个代码。

I want to insert what is in the second column of the second row into my database table but it inserts an empty space rather.
Please help me with this code.

protected void btnSave_Click(object sender, EventArgs e)
{
    String conString = ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString();
    SqlConnection con = new SqlConnection(conString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    con.Open();
            
  // string name = GridView1.Rows[0].Cells[0].Text;
    string name = (string)GridView1.Rows[2].Cells[2].Text.ToString();           

    cmd.CommandText = "insert into Course_Registration(Optn,Code1) values('" + name + "','" + GridView1.Rows[2].Cells[1].Text.ToString() + "')";
    int ok = cmd.ExecuteNonQuery();
    this.TextBox1.Text = name;
}

推荐答案

如下所示...

Do like below...
Label lblCode = (Label)GridView1.Rows[2].FindControl("Label1");
string code = lblCode.Text;

cmd.CommandText = "insert into Course_Registration(Optn, Code1) values('" + name + "','" + code + "')";



始终考虑使用参数化查询而不是内联查询。



放置调试器,看看标签的值是多少。


这篇关于如何将特定单元格中的gridview文本插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆