这个错误怎么解决

liycle 2008-07-12 04:13:28
错误 1 无法将类型“string”隐式转换为“System.Web.UI.WebControls.TextBox” E:\network center\admin\AddInformation.aspx 1

为什么出现这个错误
...全文
66 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
liycle 2008-07-13
  • 打赏
  • 举报
回复
问题解决
我把ID给改了 所以出现问题了
liycle 2008-07-13
  • 打赏
  • 举报
回复
它没有提示那行出错了 就是说apsx文件的第一行出错了
数据库设计是这样的
ID int 4
Title varchar 50
Content ntext 16
Gfrom varchar 50
Type varchar 50
Datatime datetime
前台代码:
<%@ Page Language="C#" MasterPageFile="~/admin/MasterPage.master" AutoEventWireup="true" CodeFile="AddInformation.aspx.cs" Inherits="admin_AddInformation" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<center>
<table style="width:600px" cellpadding="0" cellspacing="0">
<tr>
<td align="center" style="width:600px; height:30px" colspan="2">信息管理</td>
</tr>
<tr>
<td align="center" style="width:100px; height:30px">
 <asp:Label ID="Label1" runat="server" Text="标题"></asp:Label></td>
<td align="left" style="width:500px; height:30px">
<asp:TextBox ID="Title" runat="server" Width="250px"></asp:TextBox></td>
</tr>
<tr>
<td align="center" style="width:100px; height:30px">
 <asp:Label ID="Label2" runat="server" Text="类型"></asp:Label></td>
<td align="left" style="width:500px; height:30px">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Type" DataValueField="Type">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:networkConnectionString %>"
SelectCommand="SELECT [Type] FROM [gonggao]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td align="center" style="width:100px; height:30px">
 <asp:Label ID="Label3" runat="server" Text="来源"></asp:Label></td>
<td align="left" style="width:500px; height:30px">
<asp:TextBox ID="Tfrom" runat="server" Width="250px"></asp:TextBox></td>
</tr>
<tr>
<td align="center" style="width:100px; height:30px">
 <asp:Label ID="Label4" runat="server" Text="内容"></asp:Label></td>
<td align="left" style="width:500px; height:30px">
<asp:TextBox ID="content" runat="server" Width="500px" Height="300px"></asp:TextBox></td>
</tr>
<tr>
<td align="center" style="height:30px" colspan="2">
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />  <input id="Reset1" type="reset" value="取消" /></td>
</tr>
</table>
</center>
</asp:Content>

后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class admin_AddInformation : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("server=127.0.0.1;uid=sa;pwd=sa;database=network");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string tid = Title.Text.Trim();
string tim = DateTime.Now.ToString().Trim();
//content.Text;
if (Title.Text == "")
{
Response.Write("<script>alert('标题不能为空!');</script>");
}
else if (DropDownList1.Text == "")
{
Response.Write("<script>alert('请选择信息类型!');</script>");
}
else if (content.Text == "")
{
Response.Write("<script>alert('内容不能为空!');</script>");
}
else
{
SqlCommand cmd = new SqlCommand("insert gonggao (Title,Content,Gfrom,Type,Datatime)"+"values('"+tid+"','"+content.Text+"','"+Tfrom.Text+"','"+DropDownList1.Text+"','"+tim+"')", conn);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('信息添加成功!');</script>");
}
conn.Close();
}
}


说是"错误 1 无法将类型“string”隐式转换为“System.Web.UI.WebControls.TextBox” E:\network center\admin\AddInformation.aspx 1
"

有人说什么插入数据库的时候类型不对,我不知道怎么改
是不是内容这一项出问题了?
帮帮忙谢谢
Adechen 2008-07-13
  • 打赏
  • 举报
回复
把出错误的行写出来,
类型转换错误
liycle 2008-07-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ojlovecd 的回复:]
哪句出错
[/Quote]要怎么修改呢
liycle 2008-07-12
  • 打赏
  • 举报
回复
我也这么觉得
我姓区不姓区 2008-07-12
  • 打赏
  • 举报
回复
看代码没啥问题啊
liycle 2008-07-12
  • 打赏
  • 举报
回复
哪个出错了
liycle 2008-07-12
  • 打赏
  • 举报
回复
难道是插入内容的时候出错了 ?
content在数据库的类型是text
我姓区不姓区 2008-07-12
  • 打赏
  • 举报
回复
liycle 2008-07-12
  • 打赏
  • 举报
回复
说是aspx文件 第一行出错了 没有吧 我斗检查了了N边了
yagebu1983 2008-07-12
  • 打赏
  • 举报
回复
错误很明显了!!!
你插到数据库里的记录的类型要和数据库字段类型相同!!
我姓区不姓区 2008-07-12
  • 打赏
  • 举报
回复
哪句出错
我姓区不姓区 2008-07-12
  • 打赏
  • 举报
回复
估计是你的TextBox后面忘了打.Text了
liycle 2008-07-12
  • 打赏
  • 举报
回复
<%@ Page Language="C#" MasterPageFile="~/admin/MasterPage.master" AutoEventWireup="true" CodeFile="AddInformation.aspx.cs" Inherits="admin_AddInformation" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<center>
<table style="width:600px" cellpadding="0" cellspacing="0">
<tr>
<td align="center" style="width:600px; height:30px" colspan="2">信息管理</td>
</tr>
<tr>
<td align="center" style="width:100px; height:30px">
 <asp:Label ID="Label1" runat="server" Text="标题"></asp:Label></td>
<td align="left" style="width:500px; height:30px">
<asp:TextBox ID="Title" runat="server" Width="250px"></asp:TextBox></td>
</tr>
<tr>
<td align="center" style="width:100px; height:30px">
 <asp:Label ID="Label2" runat="server" Text="类型"></asp:Label></td>
<td align="left" style="width:500px; height:30px">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Type" DataValueField="Type">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:networkConnectionString %>"
SelectCommand="SELECT [Type] FROM [gonggao]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td align="center" style="width:100px; height:30px">
 <asp:Label ID="Label3" runat="server" Text="来源"></asp:Label></td>
<td align="left" style="width:500px; height:30px">
<asp:TextBox ID="Tfrom" runat="server" Width="250px"></asp:TextBox></td>
</tr>
<tr>
<td align="center" style="width:100px; height:30px">
 <asp:Label ID="Label4" runat="server" Text="内容"></asp:Label></td>
<td align="left" style="width:500px; height:30px">
<asp:TextBox ID="content" runat="server" Width="500px" Height="300px"></asp:TextBox></td>
</tr>
<tr>
<td align="center" style="height:30px" colspan="2">
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />  <input id="Reset1" type="reset" value="取消" /></td>
</tr>
</table>
</center>
</asp:Content>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class admin_AddInformation : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("server=127.0.0.1;uid=sa;pwd=sa;database=network");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
if (Title.Text == "")
{
Response.Write("<script>alert('标题不能为空!');</script>");
}
else if (DropDownList1.Text == "")
{
Response.Write("<script>alert('请选择信息类型!');</script>");
}
else if (content.Text == "")
{
Response.Write("<script>alert('内容不能为空!');</script>");
}
else
{
SqlCommand cmd = new SqlCommand("insert gonggao (Title,Content,Gfrom,[Type],Datatime)"+"values('"+Title.Text.Trim()+"','"+content.Text.Trim()+"','"+Tfrom.Text.Trim()+"','"+DropDownList1.Text.Trim()+"','"+DateTime.Now.ToString().Trim()+"')", conn);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('信息添加成功!');</script>");
}
conn.Close();
}
}
我姓区不姓区 2008-07-12
  • 打赏
  • 举报
回复
贴出错代码

110,021

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧