Commandtext属性尚未初始化 [英] Commandtext property has not been initialized

查看:293
本文介绍了Commandtext属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListBox ddlcity = new ListBox();
                ddlcity.ID = "city" + i;
                ddlcity.SelectionMode = ListSelectionMode.Multiple;
                string sql = "";
                if (radio.SelectedValue == "0")
                {
                    sql = "select citynew.cityid,citynew.cityname from citynew left join state on citynew.stateid=state.stateid left join country on state.countryid=country.countryid where country.countryname='india'";
                }
                else if (radio.SelectedValue == "1")
                {
                    sql = "select citynew.cityid,citynew.cityname from citynew left join state on citynew.stateid=state.stateid left join country on state.countryid=country.countryid where country.countryname!='india'";
                }
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);

                ddlcity.DataSource = ds;

                ddlcity.DataTextField = "cityname";
                ddlcity.DataValueField = "cityid";
                ddlcity.DataBind();
                ddlcity.Items.Insert(0, new ListItem("--Select--", "0"));


                plc.Controls.Add(ddlcity);







sql查询将在插入数据时显示值bt我收到错误

CommandText属性尚未初始化



我尝试了什么:



sql查询将在插入数据时显示值bt我收到错误

CommandText属性尚未初始化




The sql query will display the value bt while inserting the data i got the error
CommandText property has not been initialized

What I have tried:

The sql query will display the value bt while inserting the data i got the error
CommandText property has not been initialized

推荐答案

这是不好的做法使用

It is bad practice to use
if ()
else if ()



即使您认为您的变量永远不会有意外价值,也没有最终的其他声明。



将会发生什么如果你的定义语句没有达到,那么程序会在最后一个if之后运行到语句。



要么添加else语句处理这个未定义的情况或更改为带有默认语句的switch语句。




without a final else statement even if you think your variable will never have an unexpected value.

What will happen if your defined statements are not met is that the program runs through to the statement after the last else if.

Either add an else statement that takes care of this undefined case or change to a switch statement with a default statement.

if (radio.SelectedValue == "0")
{
    sql = ...
}
else if (radio.SelectedValue == "1")
{
    sql = ...
}
else
{
    throw new Exception(string.Format("Selected value is undefined: {0}.", radio.SelectedValue));
}






or

switch (radio.SelectedValue)
{
    case 1: sql = ...; break;
    case 2: sql = ...; break;
    default: throw new Exception(string.Format("Selected value is undefined: {0}.", radio.SelectedValue));
}


确保 SqlCommandText 不为空/空执行之前SqlCommand

如果您选择的值大于1,则SqlCommandText值将为空,这将导致异常。



使用前验证

Ensure that the SqlCommandText is not null/Empty before executing an SqlCommand
If you selected value is greater than 1, the SqlCommandText value will be empty which results in Exception.

Validate it before usage
if (sql != "")  // Validate for non empty
            {

                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);

                ddlcity.DataSource = ds;

                ddlcity.DataTextField = "cityname";
                ddlcity.DataValueField = "cityid";
                ddlcity.DataBind();
                ddlcity.Items.Insert(0, new ListItem("--Select--", "0"));
            }


这篇关于Commandtext属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆