C#.net 这样写的话 为什么插入的数据是空??要怎样改呢??----------------------------
protected void Button1_Click(object sender, EventArgs e)
{ SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from main";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds,"main");
this.GridView1.DataSource = ds;
this.GridView1.DataBind(); foreach(GridViewRow row in GridView1.Rows)
{
//string userID = row.Cells[0].Text;
string m_name = row.Cells[2].Text;
if (m_name != null)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
con.Open();
string cmdtext = "insert into main (m_name) values('" + m_name + "' )";
SqlCommand cmd1 = new SqlCommand(cmdtext, con);
cmd1.ExecuteNonQuery();
} }
bind();
}
protected void Button1_Click(object sender, EventArgs e)
{ SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from main";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds,"main");
this.GridView1.DataSource = ds;
this.GridView1.DataBind(); foreach(GridViewRow row in GridView1.Rows)
{
//string userID = row.Cells[0].Text;
string m_name = row.Cells[2].Text;
if (m_name != null)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
con.Open();
string cmdtext = "insert into main (m_name) values('" + m_name + "' )";
SqlCommand cmd1 = new SqlCommand(cmdtext, con);
cmd1.ExecuteNonQuery();
} }
bind();
}
解决方案 »
- 请教如何设置dataGridView控件某一列的数据类型格式
- DataGridView 新加行的默认值的设定
- 多线程发送IP数据包
- 怎么把两个DataTable里的数据揉合在一个DataTable里,并同时并把原来两个表里的数据在新表里做一个随机排序?
- 请问如何将两个。NET 项目合并为一个
- 帮帮我啊,毕设不会!!求大神指点
- http://expert.csdn.net/Expert/topic/1873/1873786.xml?temp=.671673
- 求助!!请大家帮帮偶!谢谢!
- 一个string型里面是"BFEBFBFF32030F27"这样的16进制数据,怎样把它转成10进制
- WPF,恼人的抗锯齿
- 哪位能给个c#俄罗斯方块的详细思路和源代码
- c#(winform)删除用combobox选择显示在datagridview中的表某一行的问题
你跟一下断点,看看m_name取出来的值是不是空的
第一、string m_name = row.Cells[2].Text; 这里看看m_name值是多少
第二、string cmdtext = "insert into main (m_name) values('" + m_name + "' )"; 查看cmdtext此刻是什么,这条语句直接在库里执行是怎样的。
第一 m_name值为: null
第二 值为: ""
你第一次没conn.Open();
打开就行了
真的不知道你怎么查的出数据不过不知道你为什么要这样去实现...
conn.Open();
也是不行
我要把打开的多条数据 插入到一个表里面
那你的row.cell[2]是指的那个啊?我一般不会这么写,我会写成row.cells["列名"]
你换成模版列,用label代替,在查找label就好用了。
按你的写法写成“写成row.cells["列名"]”的话会出现这种报错
------------------------------------错误 2 参数“1”: 无法从“string”转换为“int” F:\tool\我的文档\delphi\project\c#\OAoffice\mainupdate1.aspx.cs 68 39 F:\...\OAoffice\
这种不太难的问题需要自己解决 ...
看看连接字符串对不对啊...查出来的数据对不对啊
有没有执行sql啊...把每一步都仔细检查一下..
应该是this.GridView1.DataSource = ds.Table[0];之类的吧你的是BS?
-------------------
http://community.kingdee.com/images/www_mykingdee_com/leoch/c2.jpg
http://community.kingdee.com/images/www_mykingdee_com/leoch/c1.jpg
SQL语句
--------------
create table main
(
m_id int identity(1000,1) primary key,
m_name varchar(50))
insert into main values('衬衫')用以下方法也是只插入空值
--------------------------------- protected void Button1_Click(object sender, EventArgs e)
{ SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * from main";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds,"main");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
//string name = "";
foreach (GridViewRow gr in GridView1.Rows)
{
if (gr.RowType == DataControlRowType.DataRow) {
TextBox m_name = (TextBox)gr.FindControl("m_name");
//if (m_name != null)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
con.Open();
string cmdtext = "insert into main (m_name) values('" + m_name + "')";
SqlCommand cmd1 = new SqlCommand(cmdtext, con);
cmd1.ExecuteNonQuery();
}
}
} bind();
}
在使用SqlDataAdapter对象的时候,它会
打开数据源,它是DataSet和数据源的一个桥梁,
是一个桥接器!
string cmdtext = "insert into main (m_name) values('" + m_name.Text + "')"; //应该是Text属性值吧
SqlCommand cmd1 = new SqlCommand(cmdtext, con); //习惯上应该是先设置好命令参数,在打开数据源
con.Open(); cmd1.ExecuteNonQuery();
con.Close(); //应该要关闭数据源吧
foreach(GridViewRow row in GridView1.Rows)
{
//string userID = row.Cells[0].Text;
string m_name = row.Cells[2].Text;
if (m_name != null)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["office"].ConnectionString);
con.Open();
string cmdtext = "insert into main (m_name) values('" + m_name + "' )";
SqlCommand cmd1 = new SqlCommand(cmdtext, con);
cmd1.ExecuteNonQuery();
}
this.GridView1.DataSource = ds.Tables ["main"];
试试吧。