Autocomplete textbox in C# for windows application - c#

when i run this code this error comes..
object reference not set to an instance of an object.
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConString))
{
SqlCommand cmd = new SqlCommand("SELECT NAME FROM CUSTOMER", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
while (reader.Read())
{
MyCollection.Add(reader.GetString(0));
}
txtcname.AutoCompleteCustomSource = MyCollection;
con.Close();
}

Make sure your connection string in web.config something like
<connectionStrings>
<add name="ConString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

Problem : i'm sure that you didn't declare the connection string in your config file under <ConnectionStrings> section.
OR
you might have declared it with some defferent Key name but not ConString.
Solution: you should declare the proper Connection String with Name ConString in your config file under <ConnectionStrings> section.
Make sure that your config file either App.Config or web.config has ConnectionString similar to following:
<connectionStrings>
<add name="ConString" connectionString="Data Source=ServerName;Intial Catalog=DatabaseName;UID=userID;Password=password;Integrated Security=True;" />
</connectionStrings>

Related

Connection string in app.config

how to add this C# code connection string in app.config file for windows forms application program ?
i see examples of adding access db but i need to add excel file data , so can't find previous question on excel file connection in app.config.
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MISD_report.xlsx" + #";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
Try this in web config file-
<connectionStrings>
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 8.0'" />
<add name="ConnStrName" connectionString="Data Source=database;Initial Catalog=database-name;Persist Security Info=True;User ID=your username;Password=your password" />
</connectionStrings>
i solved this myself .
app.config settings :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MSIDConn"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MISD_report.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0'"
providerName="System.Data.OleDb" />
</connectionStrings>
</configuration>
added these 2 lines in Form1.cs
using System.Data.OleDb;
using System.Configuration;
in button click event :
private void button1_Click(object sender, EventArgs e)
{
string excelconn = ConfigurationManager.ConnectionStrings["MSIDConn"].ConnectionString;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = excelconn;
OleDbCommand command9 = new OleDbCommand
(
"SELECT P1, P2, P3 " +
" FROM [PE_Actual$] ", conn
);
DataSet ds9 = new DataSet();
OleDbDataAdapter adaptor9 = new OleDbDataAdapter(command9);
adaptor9.Fill(ds9, "testtable");
dataGridView1.DataSource = ds9.Tables[0].DefaultView;
}
Usually they sit in a section on their own:
<connectionStrings>
<add name="myConnectionName" providerName="myProvider" connectionString="Data Source=D:\MISD_report.xlsx;Extended Properties=Excel 12.0 Xml;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0" />
</connectionStrings>
I do not know what the providerName for OLE/ Excel is or what name to use so the provider finds the correct connection string.

Null reference exception in asp .net

we are working on a project in which we need to show places on google map. For places, we are providing latitude and longitude from database. we are facing null reference exception error in the following place:
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
Integrated Security=True"].ConnectionString))
How to resolve this error please guide me.
Cause of Exception:-
When you say:
System.Configuration.ConfigurationManager.
ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
Integrated Security=True"]
Since there is no connection string with name Data Source=KhUSHAL.., thus ConnectionStrings will return null and on that you are trying to access ConnectionString property which will result in Null reference exception. Read about this error here.
Basically you are mixing both, either do this:-
string CS ="Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;Integrated Security=True";
using (SqlConnection con = new SqlConnection(CS))
{
//Your code
}
Or fetch it from Web.Config(Preferred way):-
First define the connection in Web.Config:
<connectionStrings>
<add name="Test" connectionString="Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Then read it like this:-
using (SqlConnection con = new SqlConnection(System.Configuration
.ConfigurationManager.ConnectionStrings["Test"].ConnectionString))
{
//Your code
}
Do you have the ConnectionString in the Web.Config of the UI project?
Fix:
Copy that ConnectionString and Paste in your Web.Config
Your code,
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
Integrated Security=True"].ConnectionString))
is Invalid, this is not the way to declare connection string and access them.
How can we declare Connection Strings and can Access them??
No1:>In a Page
string strConnectionString="server=localhost;database=myDb;uid=myUser;password=myPass;;
Integrated Security=True";
using (SqlConnection con = new SqlConnection(strConnectionString))
{
}
No2.>Web.Config you can declare then under configuration and appSeting
And Can Access Like:
using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings("myConnectionString")))
{
}
No3>Web.Config you can declare then under configuration and connectionStrings
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
And Can Access Like:
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
{
}

windows service not add data to database after release

I`m new to ASP.Net and I need to create windows service for adding data to mssql database but the problem is my code is work in visual studio before release after I release its not adding data to database
string strConn = ConfigurationManager.ConnectionStrings["MyDBConfig"].ConnectionString;
SqlConnection oSqlConnection = new SqlConnection(strConn);
oSqlConnection.Open();
string insertText = #"INSERT INTO csvs(Agency,Status) VALUES ('Tst','0')";
using (var command2 = new SqlCommand(insertText, oSqlConnection))
{
command2.ExecuteNonQuery();
}
this is my app.config file
<connectionStrings>
<add name="MyDBConfig" connectionString="Data Source=.\SQLExpress;Initial Catalog=test;Integrated Security=True"/>
</connectionStrings>

visual web developer C# issue

So I am getting an error after the page is live and the information is trying to post.
SqlConnection conn = new SqlConnection("AddusConString");
SqlCommand cmd = new SqlCommand(sql);
int checkValue = 0;
checkValue++; //increment ID used to build parameter name
string parmName = String.Format("#Value{0}", checkValue);
SqlParameter newParameter = new SqlParameter();
newParameter.ParameterName = parmName;
// Handle TextBox value
cmd.Parameters.Add("#FName", FName.Text);
cmd.Parameters.Add("#LName", LName.Text);
It Highlights the
SqlConnection conn = new SqlConnection("AddusConString");
Saying "Format of the initialization string does not conform to specification starting at index0"
This is the code that I have for my web.config page
<configuration>
<connectionStrings>
<add name="AddusConString" connectionString="Data Source=localhost;Initial Catalog=AddusWebsite;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
</system.web>
You're using a string containing the value "AddusConString" rather than the value from the Web.Config. To get the value from the connection string section of the web.config you need to use the ConfigurationManager class.
Try:
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["AddusConString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
You have a few problems.
First, you must reference the connection string from web.Config through code similar to the following:
string myConnectionString = ConfigurationManager.ConnectionStrings["AddusConString"].ConnectionString;
SqlConnection conn = new SqlConnection(myConnectionString);
Second, there's nothing that associates the SqlCommand with the SqlConnection you've created, thusly:
SqlCommand cmd = new SqlCommand(sql, conn);
Lastly, and perhaps it was simply omitted for the purposes of the example, there's no code to indicate the query is ever going to be fired, such as:
cmd.ExecuteNonQuery(); //assuming the query is an Insert.
Your code doesnt recognise "AddusConstring" just like that.
Reference it so that it looks in the web.config:
string connString = ConfigurationManager.ConnectionStrings["AddusConstring"].ToString();

How to use configuration application block of enterprise library?

I have a list of activity code and full name which is placed in web.config file.
But now they are increased to a large number. So I want to remove that from config file and to create a db and access it with help of configuration application block.
Any ideas would be helpful.
You have to add a App.Config file.
Add the connectionstring to the configuration
<configuration>
<connectionStrings>
<add name="connectionstringname"
connectionString="server=localhost;user id=your_bd_userid;Password=your_db_password;database=your_db_name"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
Your form code goes here
public void LoadActivityData
{
string cnString = ConfigurationManager.ConnectionStrings["connectionstringname"].ConnectionString;
MySqlConnection myConn = new MySqlConnection(cnString);
DataSet dsActivity = new DataSet();
string selectStr = "SELECT * FROM YourActivityTable";
MySqlDataAdapter adapter = new MySqlDataAdapter(selectStr, myConn);
adapter.Fill(dsActivity, "Activities");
grdView1.DataSource = dsActivity;
}
Use System.Configuration namespace to get the configurationmanager class.

Categories