【发布时间】:2013-11-29 05:12:17
【问题描述】:
错误:
将 nvarchar 值“select TopicID from Topic where TopicName='Data Structure'”转换为数据类型 int 时转换失败
代码:
public void BindGridview()
{
string strConnString = ConfigurationManager.ConnectionStrings["infoConnectionString"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(strConnString);
sqlcon.Open();
string strquery2 = "select TopicID from Topic where TopicName='" + ddltopic.SelectedItem.Text+ "'";
string strquery3 = "select i.name ,i.score from info as i,Topic as t where i.topic_id=@topicid";
SqlCommand cmd = new SqlCommand(strquery3,sqlcon);
cmd.Parameters.AddWithValue("@topicid",strquery2);
cmd.Connection = sqlcon;
SqlDataReader dr;;
this.GridView1.DataSource =cmd.ExecuteReader();
this.GridView1.DataBind();
sqlcon.Close();
}
}
谁能告诉我哪里出错了?任何帮助将不胜感激..请尽快回复..提前谢谢..
【问题讨论】:
-
TopicID的数据类型是什么? -
@user3048066 :如果你想获取 TopicID 值并将其传递给其他查询存储过程,我认为这很好。
-
Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已随 ANSI-92 SQL 标准(超过 20 年前)
-
@user3048066: 请尝试创建可读查询..
标签: c# sql sql-server