Dropdownlist is not allowing new items added dynamically - asp.net

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Expt : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Bttnadd_Click(object sender, EventArgs e)
{
DropDownList11.Items.Add(ListBox1.SelectedItem);
ListBox1.Items.Remove(ListBox1.SelectedItem);
}
}
In this when i add the item in dropdown list it shows an error:"Dropdown list does not allow multiple seelection"
But when i used to print the selected item It shows null exception error.

Try to add new list items like that :
DropDownList11.Items.Add(
new ListItem(ListBox1.SelectedItem.Text, ListBox1.SelectedItem.Value)
);
Because you're adding the ListItem that selected by the ListBox, it's Selected property is true. Then you get this exception.

Related

How to add connection string dynamically in web.config

I have to create one application which will add connection string in web.config when user clicks on some control.
actually it runs fine,
But now together with it i have to run one SQL script which will create new database.
for that i have write following code..
now it gives me error as
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Web.Configuration;
using System.Collections.Generic;
public partial class connectionString : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnadd_Click(object sender, EventArgs e)
{
try
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
// System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var section = config.GetSection("connectionStrings") as ConnectionStringsSection;
section.ConnectionStrings.Add(new ConnectionStringSettings(txtNAme.Text, txtNAme.Text, "System.Data.SqlClient"));
config.Save();
string sqlConnectionString = #"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ccwebgrity;Data Source=SURAJIT\SQLEXPRESS";
FileInfo file = new FileInfo(#"G:\AAA\SCRIPT.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
file.OpenText().Close();
}
catch (Exception ex)
{
}
}
}
Define the connection string in web.config
<ConnectionStrings>
<add name="student" connectionString="Server=student;Max Pool Size=300;Initial Catalog=studentDB;User ID=student;Password=st123dent;" providerName="System.Data.SqlClient"/>
</Connectionstrings>
Configuration is read only so you can not do it in obvious way like
ConfigurationManager.ConnectionStrings["student"].ConnectionString = "new value";

Label not displaying the dropdownlist selected value

VS is giving error at this line:
Label1.Text = DropDownList1.SelectedItem.Text;
Error message says that object reference not set to an instance of the object. what could be the solution?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Login
{
public partial class ViewCourses : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string loginID = Convert.ToString(Session[0]);
DropDownList1.SelectedValue = loginID;
Label1.Text = DropDownList1.SelectedItem.Text;
}
}
}
I am guessing that DropDownList1.SelectedItem is null.
Put a breakpoint on that line and see what DropDownList1.SelectedItem's value is.
Are you familiar with the VS debugger and breakpoints?

PetaPoco orm tool with Asp.Net GridView

I am trying PetaPoco with my Asp.Net Project. How can I use PetaPoco with Asp.Net GridView. I am new at web programming.
I tried all code samples in the blog. They all are working with console app. But in Asp.Net I could not bind the datasource to GridView.
Thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PetaPoco;
using northwind;
using System.Dynamic;
namespace Mng
{
public partial class Default : System.Web.UI.Page
{
public northwindDB db;
protected void Page_Load(object sender, EventArgs e)
{
db = new northwindDB();
if (!Page.IsPostBack)
{
var q = db.Query<Product>("SELECT top 10 * FROM Products");
grdMng.DataSource = q;
}
else
{
Response.Write("Postback occurs");
}
}
}
}
Call DataBind method to build the grid
grdMng.DataSource = q;
grdMng.DataBind();

Can't override preload because not function?

Another .net newbie question
I created a class that inherits System.Web.UI.Page with the intention of overriding the preload and Unload functions. But Visual Studios complains I can't override because "System.Web.UI.Page.PreLoad" is not a function. What's wrong with my code? Here it is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class BTPage : System.Web.UI.Page
{
protected SqlConnection cnx;
public override void PreLoad(object sender, EventArgs e)
{
cnx = new SqlConnection(ConfigurationManager.AppSettings["btdatabase"]);
cnx.Open();
}
protected void Unload(object sender, EventArgs e)
{
cnx.Close();
}
}
If there's anything else alarming about my code, please tell me. I'm new to .Net and I'm not sure if I'm doing things in the ".NET way".
What I also want to do is have every web page inherit from BTPage so that they all open a connection to the database on preload.
PreLoad is not a method of the Page type. You need to override OnPreLoad

Any Way To get The selected Indices only in CheckBoxList?

I want to get the selected Indices or Items only in checkBox lsit instead of iterating through each item as Like is there in ListBox.
I am Getting Selected Value In tWo case In this way:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ChkBxList_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string li = "";
foreach(ListItem lt in CheckBoxList1.Items)
{
if(lt.Selected)
li += lt.Text;
}
Response.Write(li);
}
protected void Button2_Click(object sender, EventArgs e)
{
string li = "";
foreach (int lt in ListBox1.GetSelectedIndices())
{
li += ListBox1.Items[lt].Text;
}
Response.Write(li);
}
}
In ListBox we have the Option To get Seected Only Items is there any For Check Box List?
I dont think there is but you could use this extension method that does exactly that:
public List<ListItem> GetSelectedItems(this CheckBoxList checkBoxList)
{
List<ListItem> list = new List<ListItem>();
foreach(ListItem lt in checkBoxList)
{
if(lt.Selected)
list.Add(lt);
}
return list;
}
//Call it like this
checkBoxList.GetSelectedItems();
You actually answered your own question. There is no way to get the selected only items in the CheckBoxList control, unlike the ListBox control.
This article has an explanation and a work around help method.
http://weblogs.asp.net/jgalloway/archive/2005/10/02/426346.aspx

Resources