SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from Student1", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
drpdownlist.DataTextField = ds.Tables[0].Columns["FullName"].ToString();
drpdownlist.DataValueField = ds.Tables[0].Columns["Id"].ToString();
drpdownlist.DataSource = ds.Tables[0];
drpdownlist.DataBind();
Its giving problem for the first line.
Please help.

Nitesh KejriwalPosted Oct 13, 2012, 4:35 AM
This problem and workaround is explained in detail in http://support.microsoft.com/?kbid=2002980 . It's a SQL express limitation if the project is under the default project location, such as c:\users\[ACCOUNTNAME]\Documents\Visual Studio 20XX . If this is a personal computer, it might be a better idea to create the web application project in a non-default folder to avoid the workaround steps.
Also, VS2005, VS2008, VS2010 all supports the following ways to create aspnetdb database:
1. After create Visual Studio website or web application, Click "ASP.NET Configuration" menu item inside Website menu (for website project) or Project menu (for web project).
2. Commandline aspnet_regsql method to create the aspnetdb schema and basic data in any SQL database
Satyapriya NayakPosted Oct 13, 2012, 4:19 AM
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;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("select * from Student1", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = ds.Tables[0].Columns["FullName"].ToString();
DropDownList1.DataValueField = ds.Tables[0].Columns["Id"].ToString();
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataBind();
}
}
}
Sandeep Singh ShekhawatPosted Oct 13, 2012, 4:09 AM
like
using System.Configuration;
AND
SqlConnection con = new SqlConnection(
ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from Student1", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
drpdownlist.DataTextField = ds.Tables[0].Columns["FullName"].ToString();
drpdownlist.DataValueField = ds.Tables[0].Columns["Id"].ToString();
drpdownlist.DataSource = ds.Tables[0];
drpdownlist.DataBind();
Deepali PatilPosted Oct 13, 2012, 4:08 AM
An attempt to attach an auto-named database for file H:\Practice\WebApplication1\WebApplication1\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Nitesh KejriwalPosted Oct 13, 2012, 4:05 AM
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
If this is true, please check your web.config file to check
If this does not help, please post exact error message!
Thanks.