public void insertdata()
{
try
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customers(customer_name,address,email,mob_no) values('" + tname.Text + "','" + taddress.Text + "','" + temail.Text + "','" + tmobno.Text + "')";
cmd.ExecuteNonQuery();
MessageBox.Show("Registered Successfully");
con.Close();
tname.Text = "";
taddress.Text = "";
temail.Text = "";
tmobno.Text = "";
}
catch (Exception e)
{
MessageBox.Show("Problem in Registering Customer,Please Enter all Fields Correctly" + e);
}
}
''This is is function which i want to call in another Form which is advancePayment on button_click.
As you want to call the same function at multiple places, what you can do is create another class with insertdata() function in it. After that, you can add reference of that class in your respective forms.
when i am reading excel(2010) file, "External table is not in the expected format." error occurs. i am using Oledb connection to read excel file.please provide me best solution for this issue. thank you...
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;IMEX=1;HDR=YES'";
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
System.Data.DataTable dtExcelSchema;
dtExcelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string firstSheet = "";
int count = dtExcelSchema.Rows.Count;
conn.Close();
//Read Data from First Sheet
conn.Open();
DataTable dt = new DataTable();
var tempDataTable = (from dataRow in dtExcelSchema.AsEnumerable()
where !dataRow["TABLE_NAME"].ToString().Contains("FilterDatabase")
select dataRow).CopyToDataTable();
dt = tempDataTable;
firstSheet = dt.Rows[0]["TABLE_NAME"].ToString();
if (!firstSheet.EndsWith("$"))
{
firstSheet = dt.Rows[0]["TABLE_NAME"].ToString() + "$";
}
cmd.CommandText = "select * from [" + firstSheet + "]";
string query1 = "SELECT count(*) FROM [" + firstSheet + "]";
cmd = new OleDbCommand(query1, conn);
cmd.CommandText = query1;
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
{
string sheetName = firstSheet.Replace(" ", "").Replace("'", "");
string query = "SELECT * FROM [" + sheetName + "]";
dtnew.TableName = firstSheet;
OleDbDataAdapter oda = new OleDbDataAdapter(query, conn);
oda.Fill(dtnew);
}
}
catch (Exception ex)
{
}
}
I am using the following code to connect my .net client (CQL based) to 3 node Cassandra cluster. I am getting the data (from RabbitMQ) 30 records/sec and they get stored in cassandra upto 800-900 rows smoothly. But after that i am getting this follwing exception. Can anyone please tell me what are the optimization/changes i can make to avoid this exception. I could't find specific solution to this problem anywhere.
Error: ERROR ErrorLog - error in Cassandra GetCWCRow Function Connection :None of the hosts tried for query are available (tried: X.X.X.201:9042, X.X.X.200:9042, X.X.X.X:9042)
Code :
using Cassandra;
using Consumer;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace RabbitMqCarWaleUserTracking
{
class DataAccessCassandra
{
public bool InsertCookieLogData(string cwc, string page_uri)
{
try
{
Logs.WriteInfoLog("Cassandra InsertCookieLogData a Function called");
Cluster cluster = Cluster.Builder().AddContactPoints(ConfigurationManager.AppSettings["cassandraCluster"].ToString().Split(',')).Build();
ISession session = cluster.Connect(ConfigurationManager.AppSettings["cassandraKeySpace"].ToString());
string pageCategory = string.Empty;
try
{
if ((Regex.IsMatch(page_uri, "/newcars/upcomingcars", RegexOptions.IgnoreCase)))
{
pageCategory = "upcomingCars";
}
else
if ((Regex.IsMatch(page_uri, "/newcars/dealers/newCarDealerShowroom", RegexOptions.IgnoreCase)) || (Regex.IsMatch(page_uri, "/newcars/dealers/listnewcardealersbycity", RegexOptions.IgnoreCase)
|| (Regex.IsMatch(page_uri, "/newcars/dealers/dealerdetails", RegexOptions.IgnoreCase))))
{
pageCategory = "newcarsDealers";
}
else
if ((Regex.IsMatch(page_uri, "/offers", RegexOptions.IgnoreCase)) || (Regex.IsMatch(page_uri, "/alloffers", RegexOptions.IgnoreCase)))
{
pageCategory = "offers";
}
else
if ((Regex.IsMatch(page_uri, "/dealer/testdrive", RegexOptions.IgnoreCase)))
{
pageCategory = "dealerTestDrive";
}
if (pageCategory != string.Empty)
{
Row result = session.Execute("select logdate from pageWiseCookieLog where cwc ='" + cwc + "' and page_uri ='" + pageCategory + "' and logdate= '" + DateTime.Today.ToString("yyyy-MM-dd") + "'").FirstOrDefault();
if (result == null)
{
session.Execute("insert into pageWiseCookieLog (cwc, page_uri, logdate) values ('" + cwc + "' , '" + pageCategory + "' , '" + DateTime.Now.ToString("yyyy-MM-dd") + "' )");
session.Execute("insert into pageWiseCookieLogByld (cwc, page_uri, logdate) values ('" + cwc + "' , '" + pageCategory + "' , '" + DateTime.Now.ToString("yyyy-MM-dd") + "' )");
session.Dispose();
cluster.Dispose();
return true;
}
}
else
{
//don't want to store the data for rest of the page category but need to return true
session.Dispose();
cluster.Dispose();
return true;
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra InsertCookieLogData function with cwc :" + cwc + "error is :" + ex.Message);
SendMail.HandleException(ex, subject);
session.Dispose();
cluster.Dispose();
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra InsertCookieLogData function connection :" + ex.Message);
SendMail.HandleException(ex, subject);
}
return false;
}
public string GetCWCRow(string cwc, int index, string mobileId)
{
try
{
Logs.WriteInfoLog("Cassandra GetCWCRow Function called");
Cluster cluster = Cluster.Builder().AddContactPoints(ConfigurationManager.AppSettings["cassandraCluster"].ToString().Split(',')).Build();
ISession session = cluster.Connect(ConfigurationManager.AppSettings["cassandraKeySpace"].ToString());
try
{
Row result = session.Execute("select cur_visit_id from usertracking where cwc ='" + cwc + "'").FirstOrDefault();
if (result != null)
{
session.Dispose();
cluster.Dispose();
return result[0].ToString();
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra GetCWCRow function with cwc :" + cwc + "error is :" + ex.Message);
SendMail.HandleException(ex, subject);
session.Dispose();
cluster.Dispose();
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra GetCWCRow Function Connection :" + ex.Message);
SendMail.HandleException(ex, subject);
}
return string.Empty;
}
public bool InsertCWCRecords(string cwv, Cut_Case caseType, int index, string mobileId)
{
try
{
Logs.WriteInfoLog("Cassandra InsertCWCRecords function called for case:" + caseType);
Cluster cluster = Cluster.Builder().AddContactPoints(ConfigurationManager.AppSettings["cassandraCluster"].ToString().Split(',')).Build();
ISession session = cluster.Connect(ConfigurationManager.AppSettings["cassandraKeySpace"].ToString());
try
{
bool _isProcessed = false;
string visitCount = "";
string[] leadParameters = cwv.Split('.');
string cwc = leadParameters[0];
string visitId = leadParameters[1];
string visitStartTime = leadParameters[2];
string visitPrevPageTime = leadParameters[3];
string visitLastPageTime = leadParameters[4];
if (leadParameters.Length == 6)
{
visitCount = leadParameters[5];
}
string TOT_TIME_SPENT = (Convert.ToInt64(visitLastPageTime) - Convert.ToInt64(visitPrevPageTime)).ToString();
if ((int)caseType == 1) //to enter new cwc data in summary table
{
session.Execute("insert into usertracking (cwc, cur_visit_id, cur_visit_last_ts, tot_page_view, tot_time_spent, tot_visit_count, cur_visit_datetime) values ('" + cwc + "' , '" + visitId + "' ," + visitStartTime + "," + "1" + "," + TOT_TIME_SPENT + "," + "1" + ", '" + DateTime.Today.ToString("yyyy-MM-dd") + "' )");
_isProcessed = true;
}
if ((int)caseType == 2) //if cwc exits and visit id is same
{
Row result = session.Execute("select tot_page_view, tot_time_spent from usertracking where cwc ='" + cwc + "'").FirstOrDefault();
int page_cnt_val = int.Parse(result[0].ToString()) + 1;
Int64 time_spt_val = Int64.Parse(result[1].ToString()) + Convert.ToInt64(visitLastPageTime) - Convert.ToInt64(visitPrevPageTime);
session.Execute("update usertracking SET cur_visit_last_ts = " + visitLastPageTime + ", tot_page_view = " + page_cnt_val + ", tot_time_spent = " + time_spt_val + " WHERE cwc = '" + cwc.Trim() + "'");
_isProcessed = true;
}
if ((int)caseType == 3) //if cwc exits ans visit id is different
{
Row result = session.Execute("select tot_page_view, tot_time_spent, tot_visit_count, cur_visit_last_ts, cur_visit_datetime from usertracking where cwc = '" + cwc + "'").First();
int page_cnt_val = int.Parse(result[0].ToString()) + 1;
Int64 time_spt_val = Int64.Parse(result[1].ToString()) + Convert.ToInt64(visitLastPageTime) - Convert.ToInt64(visitPrevPageTime);
int visit_val = int.Parse(result[2].ToString()) + 1;
Int64 prev_visit_ts_val = Int64.Parse(result[3].ToString());
String prev_visit_datetime_val = Convert.ToDateTime(result[4].ToString()).ToString("yyyy-MM-dd");
session.Execute("update usertracking SET cur_visit_id = '" + visitId + "' , tot_visit_count= " + visit_val
+ " , prev_visit_last_ts= " + prev_visit_ts_val + ", prev_visit_datetime = '" + prev_visit_datetime_val
+ "' , cur_visit_last_ts = " + visitLastPageTime
+ ", tot_page_view = " + page_cnt_val + ", tot_time_spent = " + time_spt_val
+ ", cur_visit_datetime='" + DateTime.Today.ToString("yyyy-MM-dd")
+ "' WHERE cwc = '" + cwc.Trim() + "'");
_isProcessed = true;
}
session.Dispose();
cluster.Dispose();
return _isProcessed;
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra InsertCWCRecords function with cwv :" + cwv + "error is :" + ex.Message);
SendMail.HandleException(ex, subject);
session.Dispose();
cluster.Dispose();
return false;
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra InsertCWCRecords function connection :" + ex.Message);
SendMail.HandleException(ex, subject);
return false;
}
}
public bool UpdateReferrerTimeSpent(string cwc, int referrerCategoryId, double referrerTimeSpent, int index, string mobileId)
{
bool _isUpdated = false;
try
{
Logs.WriteInfoLog("Cassandra UpdateReferrerTimeSpent function called");
Cluster cluster = Cluster.Builder().AddContactPoints(ConfigurationManager.AppSettings["cassandraCluster"].ToString().Split(',')).Build();
ISession session = cluster.Connect("cw");
try
{
Row result = session.Execute("select time_spent_in_sec from userTimeSpentPage WHERE cwc = '" + cwc.Trim() + "' And logdate = '" + DateTime.Today.ToString("yyyy-MM-dd") + "' And page_category_id =" + referrerCategoryId).FirstOrDefault();
if (result != null)
{
if (result[0].ToString().Trim() != string.Empty)
{
Int64 page_time_spent_val = Int64.Parse(result[0].ToString());
Int64 tot_time_spt_val = page_time_spent_val + Int64.Parse(referrerTimeSpent.ToString());
session.Execute("update userTimeSpentPage set time_spent_in_sec= " + tot_time_spt_val + "WHERE cwc = '" + cwc.Trim() + "' And logdate = '" + DateTime.Today.ToString("yyyy-MM-dd") + "' And page_category_id=" + referrerCategoryId);
}
}
else
{
session.Execute("insert into userTimeSpentPage (cwc, page_category_id, time_spent_in_sec, logdate) values ('" + cwc + "' ," + referrerCategoryId + "," + referrerTimeSpent + ", '" + DateTime.Now.ToString("yyyy-MM-dd") + "' )");
}
_isUpdated = true;
session.Dispose();
cluster.Dispose();
return _isUpdated;
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra UpdateReferrerTimeSpent function with cwc:" + cwc + "error is :" + ex.Message);
SendMail.HandleException(ex, subject);
session.Dispose();
cluster.Dispose();
return _isUpdated;
}
}
catch (Exception ex)
{
string subject = string.Concat(ex.Source, " : ", Environment.MachineName);
Logs.WriteErrorLog("error in Cassandra UpdateReferrerTimeSpent function connection" + ex.Message);
SendMail.HandleException(ex, subject);
return _isUpdated;
}
}
}
}
Edited Question:
output of netstat -an | awk '/^tcp/ {print $NF}' | sort | uniq -c | sort -rn
On Machine 1 While cassandra running :
773 ESTABLISHED
36 LISTEN
1 CLOSE_WAIT
After cassandra stopped :
274 ESTABLISHED
36 LISTEN
1 CLOSE_WAIT
Machine 2 while cassandra running :
3941 ESTABLISHED
26 LISTEN
7 CLOSE_WAIT
After cassandra stopped :
26 LISTEN
9 ESTABLISHED
On machine 3 while cassandra running :
500 ESTABLISHED
21 LISTEN
After cassandra stopped :
21 LISTEN
13 ESTABLISHED
The NoHostAvailableException can be thrown for many reasons. However this is all about the problem described in the driver documentation:
Exception thrown when a query cannot be performed because no host are
available. This exception is thrown if
either there is no host live in
the cluster at the moment of the query
all host that have been tried
have failed due to a connection problem
Now why is this happening - there could be several reasons for this.
Possibility of simultaneous major Garbage collection on all 3 nodes. I personally don't think it is the case, but you should definitely read on this and see if it may apply to your case. Here is a link to a very nice documednt describing how to tune GC in Cassandra. The fact that you create cluster and session objects practically for any call instead of storing them as singletons and just reusing them, may make things even worse.
After looking at the function that throws an error, I am more or less convinced that the problem is that after so many inserts, your nodes just timeout while reading the wide row during this statement: Row result = session.Execute("select cur_visit_id from usertracking where cwc ='" + cwc + "'").FirstOrDefault();. Further down the road you're performing a lot of updates for the same clustering key cws which, due to immutable nature of the SSTables, makes a lot of versioned data, resulting in added data retrieval time, since the cluster needs to combine all of this data for each your request.
It is hard to make recommendations without any table schema, and reverse engineering won't help much either, but I would recommend somehow utilizing a composite primary key for faster lookups. Tune your JVMs, and make session and cluster singletons and reuse them in your code. See if this helps.
Read through the Cassandra cluster logs, focusing on the times around when the issues happen. See if any clues are in these logs, like Garbage collection activity, or timeout errors.
HTH
Roman
I run this code two times from the same Excel file, and it works fine, and I'm getting the right table. But when I try to run it a third time it crashes and throws external table is not in the expected format.
Filename = filename;
if (ExcelSet == null)
{
string HDR = firstRowContainsColumnNames ? "Yes" : "No";
string strConn;
if (filename.Substring(filename.LastIndexOf('.')).ToLower() == ".xlsx")
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Filename + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=1\"";
}
else if (filename.Substring(filename.LastIndexOf('.')).ToLower() == ".xls")
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1\"";
}
else
{
throw new Exception("File is not an Excel file");
}
DataSet ds = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow schemaRow in schemaTable.Rows)
{
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
try
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
cmd.CommandType = CommandType.Text;
DataTable outputTable = new DataTable(sheet);
ds.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
outputTable.Dispose();
}
catch (Exception ex)
{
throw new Exception(ex.Message + string.Format("Sheet: {0}.File.F{1}", sheet, Filename), ex);
}
}
}
conn.Close();
conn.Dispose();
}
ExcelSet = ds;
}
I have no clue why it crahes, anyone having the same problem?
I am creating a sharepoint webpart solution where i need to upload a excel file
Here is my code:
string tempFilename = "";
SPSecurity.RunWithElevatedPrivileges(delegate
{
tempFilename = System.IO.Path.GetTempFileName();
flUpload.SaveAs(tempFilename);
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";", tempFilename);
connectionString = #"Provider=Microsoft.ACE.OLEDB.14.0;Data Source="+tempFilename+#";ExtendedProperties=""Excel 12.0;HDR=YES;""";
var adapter = new OleDbDataAdapter("SELECT * FROM [Failed Trades$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "anyNameHere");
DataTable data = ds.Tables["anyNameHere"];
The Connection String generated is:
Provider=Microsoft.ACE.OLEDB.14.0;Data Source=C:\Windows\ServiceProfiles
\NetworkService\AppData\Local\Temp\Demo1.xls;
ExtendedProperties="Excel 12.0;HDR=YES;"
I had looked hundrededs of solutions but none of them is workimg.
Here are few solutions what i tried:
Installed setups from microsoft (64 bit)
Change the application pool with 32 bit enabled but that caused my pool to stopped again and again
I checked my DSN as well
Change the version number of the OLEDB driver in your connection string from:
Microsoft.ACE.OLEDB.14.0
to
Microsoft.ACE.OLEDB.12.0
This should work presuming you've installed the Microsoft Access Database Engine 2010 Redistributable.
You can try this:
In ODBC Data source administration click in "User DNS", and click "Excel files", and "Configuration..."
In the next window: "ODBC Microsoft Excel Setup", click in "Version" and see the list of drives.
Put the correct version in your connection String. like:
Microsoft.ACE.OLEDB.12.0 or Microsoft.ACE.OLEDB.14.0
You can select all:
public string tables(string strFileName)
{
DataTable dt = null;
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.15.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception a)
{
MessageBox.Show("15 - " + a.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.14.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception b)
{
MessageBox.Show("14 - " + b.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.13.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception c)
{
MessageBox.Show("13 - " + c.Message);
try
{
recarrega = false;
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
conn.Close();
}
catch (Exception d)
{
MessageBox.Show("12 - " + d.Message);
}
}
}
}
}