我的代码中有一个小问题,我试图将我的文本框值与数据库匹配,但是我收到一条错误消息...
这是我的代码:
我的主要代码;
protected void btnAddNewTopic_Click(object sender, EventArgs e)
{
if (txtbAddNewTopic.Text == "")
{
MessageBox.Show("Please write topic!");
}
else if (goodob.Businness_Layer.AddNewTopic.NewTopicCheckSystem(txtbAddNewTopic.Text) == null)
{
MessageBox.Show("Your added topic is already in site!");
}
else
{
goodob.Businness_Layer.CreateNewTopic.crnewtopic(txtbAddNewTopic.Text);
MessageBox.Show("Your topic has successfully created!");
}
}
和我的支票码;
public static goodob.Businness_Layer.AddNewTopic NewTopicCheckSystem(string topic)
{
goodob.Businness_Layer.AddNewTopic xyz = null;
string query = @"SELECT [topic_name]
FROM topic
WHERE topic_name = @topic";
goodob.Class1 connection1 = new goodob.Class1();
connection1.sqlcommand.CommandText = query;
SqlParameter topicparam = new SqlParameter("@topic_name", SqlDbType.VarChar);
topicparam.Value = topic;
connection1.sqlcommand.Parameters.Add(topic);
System.Data.SqlClient.SqlDataReader reader = connection1.sqlcommand.ExecuteReader();
if (!reader.HasRows)
{
connection1.close_connection();
return null;
}
return xyz;
}
我在connection1.sqlcommand.CommandText = query中收到错误;请帮我!
最佳答案
您在添加参数时做了太多的困惑,您应该添加sqlparameter对象,即主题的主题参数。
在两个地方都使用@topic varibale。
或者您也可以尝试一下
cmd.Parameters.AddWithValue(“@topic”,topic);
装的
SqlParameter topicparam = new SqlParameter("@topic_name", SqlDbType.VarChar);
topicparam.Value = topic;
connection1.sqlcommand.Parameters.Add(topic);
关于sql - C#对象引用未设置为对象错误的实例,尝试匹配文本框和数据库值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17571326/