注册 登录
编程论坛 ASP.NET技术论坛

[求助]为什么用DataGrid 更新数据时只能更新一定长度的数据啊?

ckl_20147 发布于 2007-05-05 13:18, 521 次点击

我在更新"景点名"字段是可以更新,但在更新"说明"字段时在一定长度内可以更新,但超过一定长度更新就没用,望好人帮忙,谢谢!
.aspx文件

<asp:datagrid id="news" runat="server"
AutoGenerateColumns="False"
OnPageIndexChanged="DataGrid_page" "
OnEditCommand="DataGrid_edit"
OnUpdateCommand="DataGrid_update"
DataKeyField="id">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="id" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="title" HeaderText="景点名"></asp:BoundColumn>
<asp:BoundColumn DataField="show" HeaderText="说明"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑" ItemStyle-Width="55"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>

.aspx.cs文件
public void DataGrid_update(object sender,DataGridCommandEventArgs e)
{

string updateStr = "Update news set title=@title,show=@show where id=@id";
SqlCommand cm = new SqlCommand(updateStr,cn);

cm.Parameters.Add(new SqlParameter("@title",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@show",SqlDbType.Text,16));
cm.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));

string colvalue=((TextBox)e.Item.Cells[1].Controls[0]).Text;
cm.Parameters["@title"].Value=colvalue;

colvalue=((TextBox)e.Item.Cells[2].Controls[0]).Text;
cm.Parameters["@show"].Value=colvalue;


cm.Parameters["@id"].Value=news.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="编辑成功";
news.EditItemIndex=-1;
}
catch(SqlException)
{
Lbl_note.Text="编辑失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
BindGrid();

}

5 回复
#2
川流不息2007-05-05 16:46
cm.Parameters.Add(new SqlParameter("@show",SqlDbType.Text,16));
這一句你不是設定了它的長度嗎?超過他應該不行吧。
#3
ckl_201472007-05-05 17:13
回复:(川流不息)cm.Parameters.Add(new SqlParamet...
哦,谢谢你了啊 我真是晕了
我想再问下为什么我在SQL SERVER 数据库里面也是TEXT类型,长度为16字段确可以存很多字符(至少好几百)呢?
#4
川流不息2007-05-05 17:44
呃,這個數據庫裡面的字段和程序中的字段其實沒有可比性吧。
#5
川流不息2007-05-05 17:45

不過,我也不是很清楚的。

#6
tel19822007-05-07 10:31
可以用ntext字段,这样应该就没有限制了吧。
1