从客户端提供的动态数据向现有数据表添加数据 [英] Add data to existing datatable from client supplied dynamic data

查看:73
本文介绍了从客户端提供的动态数据向现有数据表添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将文本框中的数据添加到名为单击按钮时注册"的数据表中.我尝试将数据添加到的列是名称".但是,当我单击按钮时,出现错误,提示我在textbox1中输入的文本(无论输入到文本框中的任何列)都不存在.我想在代码中保持相同的逻辑.任何帮助将不胜感激.

Hi I have been trying to add data from a textbox into a datatable called Registration on button click. The column I am trying to add the data to is "Name". However, when I click button, it gives me error saying the text I entered in textbox1 (whatever I entered into the textbox) column does not exist. I want to maintain the same logic in the code. Any help would be appreciated.

protected void Button1_Click(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection();
       con.ConnectionString = "MY CONNECTION STRING GOES HERE";
       con.Open();
       SqlCommand cmd = new SqlCommand();
       cmd.CommandText = "insert into Registration (Name) values (" +    Convert.ToString(TextBox2.Text) + ")";
       cmd.CommandType = CommandType.Text;
       cmd.Connection = con;
       //cmd.ExecuteNonQuery();
       DataSet ds = new DataSet();
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       da.Fill(ds);
       con.Close();
   }

推荐答案

如果要附加字符串数据类型,则必须使用Single quotes.
You have to use Single quotes if you are appending a string datatype.
cmd.CommandText = "insert into Registration (Name) values (''" +Convert.ToString(TextBox2.Text)+ "'')";


这篇关于从客户端提供的动态数据向现有数据表添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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