Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
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);



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
Posted
Updated 2-May-16 2:24am
v2
Comments
Herman<T>.Instance 2-May-16 7:07am    
what is the value of radio.SelectedValue ?
If it is > 1 than: the string sql = "";
Karthik_Mahalingam 2-May-16 8:15am    
post the insert code.
Member 12497501 2-May-16 8:29am    
protected void AddControls()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnect"].ConnectionString);
conn.Open();
int val = Columns;
for (int i = 0; i < val; i++)
{

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'";
}
else
{

throw new Exception(string.Format("Selected value is undefined:"));
}



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);
}
conn.Close();
ViewState["controlsadded"] = true;
}

protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (ViewState["controsladded"] == null)
{
AddControls();
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
ListBox newcity = new ListBox();


int val = Columns;
for (int i = 0; i < val; i++)
{

newcity = (ListBox)(plc.FindControl("city" + i));
foreach (ListItem li in newcity.Items)
{
if (li.Selected == true)
{
Response.Write(li.Text);
}
}
}
}

}
}


this is my asp code
<asp:RadioButtonList ID="radio" runat="server">
<asp:ListItem Value="0">domestic
<asp:ListItem Value="1">international


<asp:TextBox ID="txtdays" runat="server" CssClass="form-control" OnTextChanged="add_onclick" AutoPostBack="true">
<asp:Panel ID="plc" runat="server">

<asp:Button ID="btnsubmit" runat="server" Text="btnsubmit_onclick"
onclick="btnsubmit_Click" />
Karthik_Mahalingam 2-May-16 8:36am    
in which line you are getting the error?
i have posted the solution, check if it helps.
once again i am not seeing any code for insert data to sql

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

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.

C#
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
C#
switch (radio.SelectedValue)
{
    case 1: sql = ...; break;
    case 2: sql = ...; break;
    default: throw new Exception(string.Format("Selected value is undefined: {0}.", radio.SelectedValue));
}
 
Share this answer
 
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
C#
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"));
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900