Label not displaying the dropdownlist selected value - asp.net

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?

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";

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

Asp.net how to solve the error

Im designing my web page, in that i have typed in the book id and typed Nameofthebook. and then click the button the stack of book are available is go to another page and then not available is display the error message is book is not available My project (Library management system)
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.Data.SqlClient;
public partial class Bookcheck : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string constr = null;
constr = ConfigurationManager.ConnectionStrings["librarymanagementconnetionstring"].ConnectionString;
SqlConnection cnn = new SqlConnection(constr);
cnn.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("SELECT*FROM BOOKREGISTRATIONDETAILS WHERE bookId='" + txtid.Text.Trim() + "'AND Nameofbook='" + txtnb.Text.Trim() + "'", constr);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
Response.Redirect("Issueofbook.aspx");
}
else
{
Msg.Text = "NO book available";
Msg.Visible = true;
}
}
}
Error:
Object reference not set to an instance of an object.
I noticed that in librarymanagementconnetionstring, connection is spelled incorrectly. Is this the correct entry in your web.config for the connection string?
I'd say that it's the line
if (ds.Tables[0].Rows.Count > 0)
that giving the error.
If your DataSet has no tables then ds.Tables could be null.
If that's a possibility then you need to code for that.
If there are no rows ds.Tables[0].Rows won't be null and your test should work.
Change
if (ds.Tables[0].Rows.Count > 0)
to
if(ds.HasRows)

Dropdownlist is not allowing new items added dynamically

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.

Resources