Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my ConnectionString:

<add name="LifetimeConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Sample.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />

This is C# code:
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 System.Data.SqlClient;
using System.Web.Configuration;
using System.Configuration;

namespace LifeTime
{
    public partial class index : System.Web.UI.Page
    {
        //static Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/LifeTime");
        //SqlConnection con = null;
        //string constr = ConfigurationManager.ConnectionStrings["LifetimeConnection"].ConnectionString;
        //System.Configuration.Configuration webconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
        //const string CONSTRINGNAME = "LifetimeConnection";
        //ConnectionStringSettings conString = rootWebConfig.ConnectionStrings.ConnectionStrings[CONSTRINGNAME];
        //SqlConnection con = new SqlConnection(rootWebConfig.ConnectionStrings[CONSTRINGNAME].ConnectionStrings);
        protected void Page_Load(object sender, EventArgs e)
        {
            //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LifetimeConnection"].ConnectionString);
            //SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog = Sample;Trusted_Connection=true;");
            //Response.Write(con);
            //SqlCommand cmd = new SqlCommand("Select ID,location_name from dbo.location where flag = 0;",con);
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LifetimeConnection"].ConnectionString);

            try
            {
                Response.Write("Hi Hello");
                con.Open();
                Response.Write("Con is opened");
                SqlCommand cmd = new SqlCommand("Select top 1 * from location;", con);
                dropdownlist.DataSource = cmd.ExecuteReader();
                dropdownlist.DataBind();
                dropdownlist.DataTextField = "location_name";
                dropdownlist.DataValueField = "ID";
                dropdownlist.DataBind();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                con.Close();
            }
        }
    }
}
Please suggest me a way. I will be thankful to you.

What I have tried:

I have tried all google ways, but no result come
Posted
Updated 18-May-17 19:01pm
v2
Comments
F-ES Sitecore 19-May-17 4:37am    
Remove the trry\catch\finally, it is simply masking any errors your code might be generating, or at least just use the debugger to step through your code as that will also show you if your code is generating errors.

1 solution

Hi Member 13209785,

Do you just want to just display the Id along with location_name? Then a simple concatenation with the Id and displaying the concatenated item is the quickest solution for this.
SqlCommand cmd = new SqlCommand("Select top 1 * from location;", con);
The above line changes to:
SqlCommand cmd = new SqlCommand("Select top 1 *, CONVERT(VARCHAR, ID) + ' ' + location_name AS LocationNameWithId from location", con);
Then set the datasource to LocationNameWithId:
dropdownlist.DataTextField = "LocationNameWithId";
Also I don't see any use of the first DataBind() statement. You can remove that.

However, your code is vulnerable to SQL injection attacks. Please read about them and take care accordingly.
 
Share this answer
 
Comments
Khurana Dips 19-May-17 2:16am    
No, i just want to display the name of the loaction in the dropdown and take the Id as selectedvalue for that.Please suggest me the fruitful way. it's very urgent but the above code is not working. After con.Open(), It exits from the try block. I think my database connection is not properly done here. Please show me a way. Thanks in advance.
Mehedi Shams 19-May-17 2:39am    
In that case I don't see any problem in your code. If it is exits after con.open() that would mean connection failed. You can try to trap the error in the CATCH block and see what the problem is. E.g.

Response.Write("An error occurred. Error msg: " + ex.Message);

As long as I know AttachDbFilename is uniquely used for SQLExpress. Are you using SQL Express?

Alternatively you can try other formats of connection string. E.g.:
connectionString ="Server=.; Initial Catalog=Sample; Integrated Security=True"
Khurana Dips 19-May-17 3:11am    
This connectionstring format--> connectionString ="Server=.; Initial Catalog=Sample; Integrated Security=True" already I've used in C# code. But nothing happened.
Then, I created this connection string --><add name="LifetimeConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Sample.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" /> in web.config file and tried to connect with database. But no result came.

I am using SQL server database which is in visual studio.
Suggest me an way.
Mehedi Shams 22-May-17 0:17am    
Did you try the error-trapping through TRY-CATCH block? Any msg flashed in the browser?

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



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