CheckBoxList.aspx (UI)
<form id="form1" runat="server">
<h2>
Welcome to Hotel Customer’s Choice
</h2>
<table style="width:600px;border-color:Green;border-width:thick;">
<tr>
<td class="auto-style1">
Enter customer name
</td>
<td>
<asp:TextBox ID="txtName" runat="server"
Width="250px" MaxLength="30"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Select food type</td>
<td>
<table style="width:250px;">
<tr>
<td>
<asp:CheckBox ID="chkVeg"
runat="server" Text="Veg"/>
</td>
<td>
<asp:CheckBox ID="chkNonVeg"
runat="server" Text="NonVeg" />
</td>
<td>
<asp:LinkButton ID="lnkbtnShow"
runat="server" onclick="lnkbtnShow_Click">
Show Items</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
Foods of Selected Type(s)
</td>
<td>
<asp:ListBox ID="lstFoodTypes"
runat="server" Width="250px" Rows="6" SelectionMode="Multiple">
</asp:ListBox>
</td>
</tr>
<tr>
<td></td>
<td>
<table>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="125px" OnClick="btnSubmit_Click" />
</td>
<td>
<asp:Button ID="btnClear" runat="server" Text="Clear" Width="125px" OnClick="btnClear_Click" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server"
Text="Hello
User..."></asp:Label>
</td>
</tr>
</table>
</form>
</body>
Code Behind – CheckBoxList.aspx.cs
public partial class Checkbox : System.Web.UI.Page
{
protected void lnkbtnShow_Click(object sender, EventArgs e)
{
lstFoodTypes.Items.Clear();
if (chkVeg.Checked)
{
//lstFoodTypes.Items.Add("Idly");
//lstFoodTypes.Items.Add("Dosa");
//lstFoodTypes.Items.Add("Pongal");
ShowFoodsOf("Veg");
}
if (chkNonVeg.Checked)
{
//lstFoodTypes.Items.Add("Briyani");
//lstFoodTypes.Items.Add("Fish
fry");
//lstFoodTypes.Items.Add("Parotta
with chicken");
ShowFoodsOf("NonVeg");
}
}
protected void ShowFoodsOf(String ftype)
{
SqlConnection con;
con = new SqlConnection(ConfigurationManager
.ConnectionStrings["MyDBConStr"].ConnectionString);
con.Open();
string qry = "Select FoodName from
tabFoods where FoodType=@foodtype";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@foodtype", ftype);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
lstFoodTypes.Items.Add(dr.GetString(0));
}
dr.Close();
cmd.Dispose();
con.Close();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con;
con = new SqlConnection(ConfigurationManager
.ConnectionStrings["MyDBConStr"].ConnectionString);
con.Open();
int ItemIndex =0;
int totalItems = lstFoodTypes.Items.Count;
while (ItemIndex < totalItems)
{
if (lstFoodTypes.Items[ItemIndex].Selected)
{
string qry = "insert into
tabCustomerFoods values(@custName,@foodName)";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@custName", txtName.Text);
cmd.Parameters.AddWithValue("@foodName", lstFoodTypes.Items[ItemIndex].Text);
cmd.ExecuteNonQuery();
}
ItemIndex++;
}
lblMsg.Text = "Food Details Saved...";
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtName.Text = "";
chkVeg.Checked = false;
chkNonVeg.Checked = false;
lstFoodTypes.SelectedIndex = -1;
lstFoodTypes.Items.Clear();
lblMsg.Text = "Enter next customer's choice";
}
}
protected void ShowFoodsOf(String ftype)
ReplyDelete{
SqlConnection con;
con = new SqlConnection(ConfigurationManager
.ConnectionStrings["MyDBConStr"].ConnectionString);//exception in this line
con.Open();
}
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.
First you have to create database in SQL Server and
ReplyDeleteYou have to add connection string information in web.config file
then try these example