Object reference not set to an instance of an object

Posted by Kelapa under C# on 12/23/2011 | Points: 10 | Views : 4179 | Status : [Member] | Replies : 1
string[] content = System.IO.File.ReadAllLines(e.FullPath);
foreach (string line in content)
{
string[] arr;
arr = line.Split(','); //splitting the line which was read by the stream reader object
DataRow row = table.NewRow();
row["L_DateTime"] = DateTime.Parse(arr[0].Substring(0, 4) + "-" + arr[0].Substring(4, 2) + "-" + arr[0].Substring(6));
row["L_Direction"] = arr[1];
row["L_CardID"] = arr[2];
row["L_GateID"] = Convert.ToInt32(arr[3]);
table.Rows.Add(row);
i2++;
}

//Connect to DB
string conString = ConfigurationManager.ConnectionStrings["Data Source=HEIZ-PC\\SQLEXPRESSR2;Initial Catalog=attandent;User ID=sa;Password=pwd"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
try
{
//Execute the command to make a temp table
SqlCommand cmd = new SqlCommand(tmpTable, con);
cmd.ExecuteNonQuery();

//BulkCopy the data in the DataTable to the temp table
using (SqlBulkCopy bulk = new SqlBulkCopy(con))
{
bulk.DestinationTableName = "#templogDetail";
bulk.WriteToServer(table);
}

//Now use the merge command to upsert from the temp table to the production table
string mergeSql = "merge into logDetail as Target using #templogDetail as Source on" +
"Target.L_DateTime=Source.L_DateTime, Target.L_Direction=Source.L_Direction," +
"Target.L_CardID=Source.L_CardID, when matched then update set Target.L_GateID=Source.L_GateID," +
"when not matched then insert (L_DateTime,L_Direction,L_CardID,L_GateID)" +
"values (Source.L_DateTime,Source.L_Direction,Source.L_CardID,Source.L_GateID);";

cmd.CommandText = mergeSql;
cmd.ExecuteNonQuery();

//Clean up the temp table
cmd.CommandText = "drop table #templogDetail";
cmd.ExecuteNonQuery();
}


hi...greeting coder...
im totally noob in c#....has error with this : Object reference not set to an instance of an object
the error stated at the this line. string conString = ConfigurationManager.ConnectionStrings["Data Source=HEIZ-PC\\SQLEXPRESSR2;Initial Catalog=attandent;User ID=sa;Password=pwd"].ConnectionString;

what is wrong with my connection string?? any advice would be my pleasure...thanks in advance..




Responses

Posted by: Suvendu on: 12/25/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Kelapa,
There are two ways to solve this problem, either you can put your connection string in the web.config file or, can simply use it on the page you are doing ado activities. Now, before showing these, let me clarify - why you are getting an error message. 'ConfigurationManager' class is used to access the properties of configuration elements in a configuration file, generally web.config. There is no overload for ConfigurationManager.Connectionstrings, which takes a connection string, but it gets the connection string stated in connectionstrings element of configuration with a name and here you just have to pass that name. If you want this way to solve your problem then follow these steps-

------------------------way 1-----------------------------
1)find <connectionStrings/> tag in your web.config file and then replace it with following..

<connectionStrings/>	

<add name="conString" connectionString="Data Source=HEIZ-PC\\SQLEXPRESSR2;Initial Catalog=attandent;User ID=sa;Password=pwd" />
</connectionStrings>


2)now replace the line on your page,
string conString = ConfigurationManager.ConnectionStrings["Data Source=HEIZ-PC\\SQLEXPRESSR2;Initial Catalog=attandent;User ID=sa;Password=pwd"].ConnectionString;


with following
string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;


---------------------way 2-------------------
1)replace the line
string conString = ConfigurationManager.ConnectionStrings["Data Source=HEIZ-PC\\SQLEXPRESSR2;Initial Catalog=attandent;User ID=sa;Password=pwd"].ConnectionString;


with following
string conString = "Data Source=HEIZ-PC\\SQLEXPRESSR2;Initial Catalog=attandent;User ID=sa;Password=pwd";



I hope it will help you resolving your problem..if i am missing something then please let me know...
Thanks

Kelapa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response