SQLCE Issue: update requires the command clone to have a connection object
I am working on a new solution for an existing project. now I am facing
some troubles with the sql ce connection. I modified my connection with
the following class:
namespace CommonTools
{
/// <summary>
///
/// </summary>
public class SQLCEConnectorClass
{
private SqlCeConnection m_openConnection;
/// <summary>
/// Opens the SQLCE connection.
/// </summary>
/// <param name="connectionString">The connection string.</param>
/// <returns></returns>
public SqlCeConnection OpenSQLCEConnection(string connectionString)
{
bool isWorking = false;
while (!isWorking)
{
if (TryToConnect(connectionString))
{
isWorking = true;
}
else
{
// sleep for 100 ms to try the connection again
Thread.Sleep(100);
}
}
return m_openConnection;
}
/// <summary>
/// Tries to connect.
/// </summary>
/// <param name="connectionString">The connection string.</param>
/// <returns></returns>
private bool TryToConnect(string connectionString)
{
try
{
m_openConnection = new SqlCeConnection(connectionString);
if (m_openConnection.State == ConnectionState.Closed)
{
m_openConnection.Open();
return true;
}
}
catch
{
return false;
}
return false;
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose()
{
m_openConnection.Close();
m_openConnection.Dispose();
m_openConnection = null;
}
}
}
so it's possible to use the SQL CE with multiple user, otherwise it is
used by another process and I can not access from a different machine. I
changed also the following code segements:
ConnectionString = "DataSource='" + dbPath + fileName + "';
Password ='" + password + "';";
cn = sqlce.OpenSQLCEConnection(ConnectionString);
// only if the connection string is not empty and a connection to the
database was possible
if (ConnectionString != string.Empty)
{
// for the categories
string sql = "select * from Kategorien";
SqlCeCommand cmd = new SqlCeCommand(sql, cn);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
categories.Add(Convert.ToString(reader[0]));
categoriesSAP.Add(Convert.ToString(reader[0]),
Convert.ToString(reader[1]));
}
}
else
{
XtraMessageBox.Show("Es konnte keine Verbindung zur Datenbank
hergestellt werden!", "Fehler", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
sqlce.Dispose();
now I want to update my changes:
private void SaveTable()
{
try
{
DataRow[] rows = editedRows.ToArray();
dAdapter.SelectCommand.Connection =
sqlce.OpenSQLCEConnection(ConnectionString);
dAdapter.Update(rows);
int rowCount = dAdapter.Update(dTable);
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Fehler",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
sqlce.Dispose();
}
the first change (SaveTable) will work, without any exception. the second
change does't work. I got the following exception at
dAdapter.Update(rows):
Update requires the command clone to have a connection object. The
Connection property of the command clone has not been initialized.
before I changed it to the new connection class, everything worked as
expected. any suggestions what I made wrong?
thanks, tro
Bohlmann
Thursday, 3 October 2013
Wednesday, 2 October 2013
Twitter4j Stream not Closing?
Twitter4j Stream not Closing?
I have been searching all over the internet for a way to stop the stream
of tweets from Twitter's Streaming API. I saw this bit of code, but
nothing happens when it is exicuted, and i still get tweets. I have also
tried to set the connection to null, which gave me an error:
Stream.shutdown();
How can i stop this stream? Thanks.
Here is some more of my code:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey("xxxxxxxx").setOAuthConsumerSecret("xxxxxx").setOAuthAccessToken("xxxxxx").setOAuthAccessTokenSecret("xxxxxx");
TwitterStream twitterStream = new
TwitterStreamFactory(cb.build()).getInstance();
After this, i check a boolean to see wheather i should close the stream or
note. If flase, then i do. (sort of swapped around, but false = close
here). Here is that it is eclosed in:
if(!CheckTweets)
{
Stream.shutdown();
}
else
{
// Show Tweets....
}
I have been searching all over the internet for a way to stop the stream
of tweets from Twitter's Streaming API. I saw this bit of code, but
nothing happens when it is exicuted, and i still get tweets. I have also
tried to set the connection to null, which gave me an error:
Stream.shutdown();
How can i stop this stream? Thanks.
Here is some more of my code:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey("xxxxxxxx").setOAuthConsumerSecret("xxxxxx").setOAuthAccessToken("xxxxxx").setOAuthAccessTokenSecret("xxxxxx");
TwitterStream twitterStream = new
TwitterStreamFactory(cb.build()).getInstance();
After this, i check a boolean to see wheather i should close the stream or
note. If flase, then i do. (sort of swapped around, but false = close
here). Here is that it is eclosed in:
if(!CheckTweets)
{
Stream.shutdown();
}
else
{
// Show Tweets....
}
Adding multiple partial views and first one is missing form
Adding multiple partial views and first one is missing form
I have the following:
_ImageGalleryPartial
@if (Model != null)
{
foreach (var item in Model)
{
@Html.Partial("_ImageItem", item);
}
}
_ImageItemPartial
@model WebsiteEngine.Models.PortfolioImage
@{
string url = VirtualPathUtility.ToAbsolute(String.Format("~/{0}",
Html.DisplayFor(m => m.FileName)));
}
@using (Html.BeginForm("DeleteImage", "Portfolio", FormMethod.Post, new {
@class = "deleteImageForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true);
@Html.HiddenFor(m => m.Name)
@Html.HiddenFor(m => m.FileName)
@Html.HiddenFor(m => m.Order)
@Html.HiddenFor(m => m.PortfolioID)
@Html.HiddenFor(m => m.PortfolioImageID)
<div class="span3">
<div class="item">
<a class="fancybox-button" data-rel="fancybox-button"
title="Photo" href="@url">
<div class="zoom">
<img src="@url" alt="Photo" />
<div class="zoom-icon"></div>
</div>
</a>
<div class="details">
<a href="#" class="icon deleteImage" data-form=""><i
class="icon-remove"></i></a>
</div>
</div>
</div>
}
When I inspect the page elements using Chrome dev tools, the first element
is missing the surrounding form. If I inspect the page source (using
right) click then the form is there. This lead me to believe some JS was
removing the form so I disabled JS but it was still missing.
If change _ImageGalleryPartial so it's like this (notice the addition of
an empty model):
@if (Model != null)
{
@Html.Partial("_ImageItem", new WebsiteEngine.Models.PortfolioImage());
foreach (var item in Model)
{
@Html.Partial("_ImageItem", item);
}
}
Then all the "correct" elements get a form but again, this first item
doesn't.
I'm still inclined to think this is a JS issue but I'm a little stumped as
disabling JS doesn't fix it. Is this some off behaviour with MVC?
Just to note, I have simplified my layout above, I do actually have one or
2 nested forms but assume that's Ok as everything else is Ok, it's just
this first partial that's broken.
Any ideas?
I have the following:
_ImageGalleryPartial
@if (Model != null)
{
foreach (var item in Model)
{
@Html.Partial("_ImageItem", item);
}
}
_ImageItemPartial
@model WebsiteEngine.Models.PortfolioImage
@{
string url = VirtualPathUtility.ToAbsolute(String.Format("~/{0}",
Html.DisplayFor(m => m.FileName)));
}
@using (Html.BeginForm("DeleteImage", "Portfolio", FormMethod.Post, new {
@class = "deleteImageForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true);
@Html.HiddenFor(m => m.Name)
@Html.HiddenFor(m => m.FileName)
@Html.HiddenFor(m => m.Order)
@Html.HiddenFor(m => m.PortfolioID)
@Html.HiddenFor(m => m.PortfolioImageID)
<div class="span3">
<div class="item">
<a class="fancybox-button" data-rel="fancybox-button"
title="Photo" href="@url">
<div class="zoom">
<img src="@url" alt="Photo" />
<div class="zoom-icon"></div>
</div>
</a>
<div class="details">
<a href="#" class="icon deleteImage" data-form=""><i
class="icon-remove"></i></a>
</div>
</div>
</div>
}
When I inspect the page elements using Chrome dev tools, the first element
is missing the surrounding form. If I inspect the page source (using
right) click then the form is there. This lead me to believe some JS was
removing the form so I disabled JS but it was still missing.
If change _ImageGalleryPartial so it's like this (notice the addition of
an empty model):
@if (Model != null)
{
@Html.Partial("_ImageItem", new WebsiteEngine.Models.PortfolioImage());
foreach (var item in Model)
{
@Html.Partial("_ImageItem", item);
}
}
Then all the "correct" elements get a form but again, this first item
doesn't.
I'm still inclined to think this is a JS issue but I'm a little stumped as
disabling JS doesn't fix it. Is this some off behaviour with MVC?
Just to note, I have simplified my layout above, I do actually have one or
2 nested forms but assume that's Ok as everything else is Ok, it's just
this first partial that's broken.
Any ideas?
How to complexity
How to complexity
Please can any one provide with a better algorithm then trying all the
combinations for this problem.
Given an array A of N numbers,
find the number of distinct pairs (i, j) such that j >=i and A[i] = A[j].
First line of the input contains number of test cases T.
Each test case has two lines, first line is the number N,
followed by a line consisting of N integers which are the elements of
array A.
For each test case print the number of distinct pairs.
Constraints
1 <= T <= 10
1 <= N <= 10^6
-10^6 <= A[i] <= 10^6 for 0 <= i < N
I think that first sorting the array then finding frequency of every
distinct integer and then adding nC2 of all the frequencies plus adding
the length of the string at last. But unfortunately it gives wrong ans for
some cases which are not known help. here is the implementation.
code:
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long fun(long a) //to find the aC2 for given a
{
if(a==1) return 0;
return (a*(a-1))/2;
}
int main()
{long t,i,j,n,tmp=0;
long long count;
long ar[1000000];
cin>>t;
while(t--)
{cin>>n;
for(i=0;i<n;i++)
{
cin>>ar[i];
}
count =0;
sort(ar,ar+n);
for(i=0;i<n-1;i++)
{
if(ar[i]==ar[i+1]){tmp++;}
else {count +=fun(tmp+1);tmp=0; }
}
if(tmp!=0)
{count+=fun(tmp+1);}
cout<<count+n<<"\n";
}
return 0;
}
Please can any one provide with a better algorithm then trying all the
combinations for this problem.
Given an array A of N numbers,
find the number of distinct pairs (i, j) such that j >=i and A[i] = A[j].
First line of the input contains number of test cases T.
Each test case has two lines, first line is the number N,
followed by a line consisting of N integers which are the elements of
array A.
For each test case print the number of distinct pairs.
Constraints
1 <= T <= 10
1 <= N <= 10^6
-10^6 <= A[i] <= 10^6 for 0 <= i < N
I think that first sorting the array then finding frequency of every
distinct integer and then adding nC2 of all the frequencies plus adding
the length of the string at last. But unfortunately it gives wrong ans for
some cases which are not known help. here is the implementation.
code:
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long fun(long a) //to find the aC2 for given a
{
if(a==1) return 0;
return (a*(a-1))/2;
}
int main()
{long t,i,j,n,tmp=0;
long long count;
long ar[1000000];
cin>>t;
while(t--)
{cin>>n;
for(i=0;i<n;i++)
{
cin>>ar[i];
}
count =0;
sort(ar,ar+n);
for(i=0;i<n-1;i++)
{
if(ar[i]==ar[i+1]){tmp++;}
else {count +=fun(tmp+1);tmp=0; }
}
if(tmp!=0)
{count+=fun(tmp+1);}
cout<<count+n<<"\n";
}
return 0;
}
When i add image i cannot see the text box in jquery mobile?
When i add image i cannot see the text box in jquery mobile?
I am new to jquery mobile. when i add back ground image all the text box
are hidden . what can be done to add image and to see the text box .
Thanks in Advance
I am new to jquery mobile. when i add back ground image all the text box
are hidden . what can be done to add image and to see the text box .
Thanks in Advance
Tuesday, 1 October 2013
A bit of integration involving a continuous function
A bit of integration involving a continuous function
If $f$ is continuous on $[0,1]$ and if $\int_{0}^{1}f(x)x^ndx=0$ for
$n=0,1,2,3...$then $\int_{0}^{1}f^2(x)dx=0$.
This is how I proceeded. For $n=1$, $\int_{0}^{1}f(x)xdx=0$ . (Using by
parts) $\implies$ $x\int f(x)dx]_0^1-\int_{0}^{1}(\int f(x)dx)dx=0$. Let
$I(x)=\int f(x)dx$. Then $I(1)=\int_{0}^{1}I(x)dx$. For $n=2$ we have
$\int_{0}^{1}f(x)x^2dx= x\int xf(x)dx]_0^1-\int_{0}^{1}(\int xf(x)dx)dx=0$
In general for any $n$, $\int_{0}^{1}f(x)x^ndx=x\int
x^{n-1}f(x)dx]_0^1-\int_{0}^{1}(\int x^{n-1}f(x)dx)dx=0$.
Then I don't see how I can use this information to get the desired result.
If $f$ is continuous on $[0,1]$ and if $\int_{0}^{1}f(x)x^ndx=0$ for
$n=0,1,2,3...$then $\int_{0}^{1}f^2(x)dx=0$.
This is how I proceeded. For $n=1$, $\int_{0}^{1}f(x)xdx=0$ . (Using by
parts) $\implies$ $x\int f(x)dx]_0^1-\int_{0}^{1}(\int f(x)dx)dx=0$. Let
$I(x)=\int f(x)dx$. Then $I(1)=\int_{0}^{1}I(x)dx$. For $n=2$ we have
$\int_{0}^{1}f(x)x^2dx= x\int xf(x)dx]_0^1-\int_{0}^{1}(\int xf(x)dx)dx=0$
In general for any $n$, $\int_{0}^{1}f(x)x^ndx=x\int
x^{n-1}f(x)dx]_0^1-\int_{0}^{1}(\int x^{n-1}f(x)dx)dx=0$.
Then I don't see how I can use this information to get the desired result.
Function instance variables inside a class
Function instance variables inside a class
I'm trying to implement a so-called static variable in my method, similar
to the decorator method described in this Stackoverflow thread.
Specifically, I define a decorator function as follows:
def static_var(varName, value):
def decorate(function):
setattr(function,varName,value)
return function
return decorate
As the example shows, this can be used to attach a variable to the function:
@static_var('seed', 0)
def counter():
counter.seed +=1
return counter.seed
This method will return the number of times it has been called.
The issue i am having is that this does not work if I define the method
inside a class:
class Circle(object):
@static_var('seed',0)
def counter(self):
counter.seed +=1
return counter.seed
If I instantiate a Circle and run counter,
>>>> myCircle = Circle()
>>>> myCircle.counter()
I get the following error: NameError: global name 'counter' is not defined.
My response to this was that maybe I need to use self.counter, i.e.
class Circle(object):
@static_var('seed',0)
def counter(self):
self.counter.seed +=1
return self.counter.seed
However this produces the error, AttributeError: 'instancemethod' object
has no attribute 'seed'.
What is going on here?
I'm trying to implement a so-called static variable in my method, similar
to the decorator method described in this Stackoverflow thread.
Specifically, I define a decorator function as follows:
def static_var(varName, value):
def decorate(function):
setattr(function,varName,value)
return function
return decorate
As the example shows, this can be used to attach a variable to the function:
@static_var('seed', 0)
def counter():
counter.seed +=1
return counter.seed
This method will return the number of times it has been called.
The issue i am having is that this does not work if I define the method
inside a class:
class Circle(object):
@static_var('seed',0)
def counter(self):
counter.seed +=1
return counter.seed
If I instantiate a Circle and run counter,
>>>> myCircle = Circle()
>>>> myCircle.counter()
I get the following error: NameError: global name 'counter' is not defined.
My response to this was that maybe I need to use self.counter, i.e.
class Circle(object):
@static_var('seed',0)
def counter(self):
self.counter.seed +=1
return self.counter.seed
However this produces the error, AttributeError: 'instancemethod' object
has no attribute 'seed'.
What is going on here?
Subscribe to:
Comments (Atom)