I have two problems

1. I have develop and Installed a project on my computer. that was work on my PC so many days. but now prject not work properly.

It give me error message that SQl connection not avaliable.

Is their is any way to overcome this problem.

Can we store the path of connectionstring in one method and then call that where i need in entire project.

2. crystal report:
I want to dispaly crystal report on combobox index chage event
I have ten tables.

I have use parameterized queary for that but when designing the crystal report. I have to set dataset and spacify the table and drag and drop the feild of that table. Report show the data of that table but i want to dispaly data on user selection table name.

string val = comboBox1.Text.ToString();
            string contr = "Data Source=WELL\\SQLEXPRESS;Initial Catalog=hire;Integrated Security=True";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = contr;
            cn.Open();
            string str = "select concd,deb,crd,vec from " + comboBox1.Text + " where hrnm=@val";
            SqlCommand cmd = new SqlCommand(str, cn);
            cmd.CommandType = CommandType.Text;
            SqlParameter sql;
            sql = cmd.Parameters.Add("@val", SqlDbType.VarChar, 15);
            sql.Value = val;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds, comboBox1.Text);
            CrystalReport13 cr = new CrystalReport13();
            cr.SetDataSource(ds);
            crystalReportViewer1.ReportSource = cr;
            cn.Close();

what changs I have to perform in my designing or coding
thanks in advance.....

Recommended Answers

All 12 Replies

The best way is that You should have to store your connection string in configuration file (app.config/web.config) and read it from there in your coding. So that it will not create any trouble if you are installing it on any other computer. Another benefit is when you need to make any changes in connection string then you just have to make changes in configuration file only not in your code side..

I have two problems

1. I have develop and Installed a project on my computer. that was work on my PC so many days. but now prject not work properly.

It give me error message that SQl connection not avaliable.

Is their is any way to overcome this problem.

Can we store the path of connectionstring in one method and then call that where i need in entire project.

2. crystal report:
I want to dispaly crystal report on combobox index chage event
I have ten tables.

I have use parameterized queary for that but when designing the crystal report. I have to set dataset and spacify the table and drag and drop the feild of that table. Report show the data of that table but i want to dispaly data on user selection table name.

string val = comboBox1.Text.ToString();
            string contr = "Data Source=WELL\\SQLEXPRESS;Initial Catalog=hire;Integrated Security=True";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = contr;
            cn.Open();
            string str = "select concd,deb,crd,vec from " + comboBox1.Text + " where hrnm=@val";
            SqlCommand cmd = new SqlCommand(str, cn);
            cmd.CommandType = CommandType.Text;
            SqlParameter sql;
            sql = cmd.Parameters.Add("@val", SqlDbType.VarChar, 15);
            sql.Value = val;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds, comboBox1.Text);
            CrystalReport13 cr = new CrystalReport13();
            cr.SetDataSource(ds);
            crystalReportViewer1.ReportSource = cr;
            cn.Close();

what changs I have to perform in my designing or coding
thanks in advance.....

In config file their is already the path of connection string.

I show u the config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WindowsFormsApplication2.Properties.Settings.masterConnectionString"
            connectionString="Data Source=WELL\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

how can i call this in my other forms

Oh great !!

Now, you can call this connection string in your form by below way :

[B]SqlConnection objSqlCon = new SqlConnection(Convert.ToString(ConfigurationSettings.AppSettings["WindowsFormsApplication2.Properties.Settings.masterConnectionString"].ToString()));[/B]

I have one suggestion for you, don't give that's much long name of your connectionsting. It should be short and easily remember.

In config file their is already the path of connection string.

I show u the config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WindowsFormsApplication2.Properties.Settings.masterConnectionString"
            connectionString="Data Source=WELL\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

how can i call this in my other forms

If I change user name or format the PC and install the project then it will create error or not.

I have change the name from "WindowsFormsApplication2.Properties.Settings.masterConnectionString" to "str"
But it give me the error message that nullreferanceExceptionUnhandled

Object reference not set to an instance of an object.

did you have change the name in config file also ? where you have changed the name ?

I have change the name from "WindowsFormsApplication2.Properties.Settings.masterConnectionString" to "str"
But it give me the error message that nullreferanceExceptionUnhandled

Object reference not set to an instance of an object.

yes i have change the config name.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name= "str" connectionString="Data Source=WELL\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
           
            
             
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

if i dont change the name then also it give me error.

I use your line of code i.e

SqlConnection objSqlCon = new SqlConnection(Convert.ToString(ConfigurationSettings.AppSettings["WindowsFormsApplication2.Properties.Settings.masterConnectionString"].ToString()));

or

string s= ConfigurationManager.AppSetting["str"].tostring();

but it work in some project but in this project both lines are not working.

in my line the conifigurationManager is not available in the config list.

It's strange... anyways do one thing
1)close your dot net IDE.
2)start IDE again and open your project
3)Clean your solution then rebuild it again.

yes i have change the config name.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name= "str" connectionString="Data Source=WELL\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
           
            
             
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

if i dont change the name then also it give me error.

I use your line of code i.e

SqlConnection objSqlCon = new SqlConnection(Convert.ToString(ConfigurationSettings.AppSettings["WindowsFormsApplication2.Properties.Settings.masterConnectionString"].ToString()));

or

string s= ConfigurationManager.AppSetting["str"].tostring();

but it work in some project but in this project both lines are not working.

in my line the conifigurationManager is not available in the config list.

in my line the conifigurationManager is not available in the config list.

You need to add a new Reference (System.Configuration) - right click on References in Solution explorer and choose Add refernece. Go down and select System.Configuration.

Now you write:

private static string connString = ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString;

and when you create a connection simply call this value, like

void Method()
{
    using(SqlConnection sqlConn = new SqlConnection(connString))
    {
         //the rest of the code!
         //with using keyWord "using" you dont even need to call Close() method for connection!
    }
}

Hope it helps,
Mitja

Yes Its working. In that project config file items was not include by default when i add from referance it start to work. Thanks a lot both of you.

if i change the user name or password then it will give error or not.

and if you know any thing about crystal report please tell me.

Coooool... what you want to know about crystal report ?

Yes Its working. In that project config file items was not include by default when i add from referance it start to work. Thanks a lot both of you.

if i change the user name or password then it will give error or not.

and if you know any thing about crystal report please tell me.

I want to display crystal report at run time.
because i have create my tables at run time so i want to show that tables crystal report.
I have use some code for that but its not work.

and I have told my fried to use connection string like this but on his PC it give an error message that object referance is not set to an instance of an object.

It is realy reliable to use. because it give error some time and some time it work properly. I dont know why this happend.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.