|
I tried to insert data into SQL server database using the following code. All data that I received from all my connections (10 socket). It runs for sometime and I was able to insert some data then suddenly in the middle of the process I encountered
System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ossrop.Class1.SaveToSQLDB(String strFile, String strOpt) in d:\ossrop\class1.cs:line 560
I declare my DBConnect as modular and with CONNECTION POOLING.
//DBConnect = new SqlConnection("Pooling=true;server=OSSSQL01;database=nfmse;uid=alarmadmin;pwd=alarmadm1n;Min Pool Size=3;Max Pool Size=100;");
DBConnect.Open();
Processdata()
{
iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx]; // + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
StringBuilder szData = new StringBuilder();
szData.Append(chars);
SaveTOSQL();
}
void SaveTOSQL()
{
string strInsert = "insert into rop values ('" + strjv.Trim() + "','" + strdate.Trim() + "','" + strclass.Trim() + "','" + strFile + "','" + strFile + "')" ;
DBCommand = new SqlCommand(strInsert,DBConnect);
DBCommand.ExecuteNonQuery();
}
PS : I don't close the connection nor the command becuase this is a 24 hour process.
Thanks in advance.
/dabsukol
|
|
|
|
|
One of two possibilities that I can think of.
(1) You read data with a DataReader and didn't close it when you were done reading.
(2) You are still reading data with the DataReader when you call the code to insert data on the same connection as the DataReader .
The solution for (1) is to remember to Close() DataReader s when you're done with them.
The solution for (2) is to open a new connection because you cannot run two or more commands on the same connection at the same time. (The Data Reader is still running the command because it doesn't get the data from the database until it neads it) - Since you're using Connection pooling this isn't much of an overhead.
Actually since you're using Connection pooling why not close the connection after each operation the any subsequent operations will just pull the connection back out of the pool.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
|
|
|
|
|
Hi,
Your're correct I 'm seleceting records from the first table using ofcourse the Connection. then, while selecting ip/ports from my first table i'm also at the same time inserting data into the 2nd table with the same connection. That's why I got this error message.
Solution: all data selected from the 1st table is temporarily inserted into an array variable and from it I try to run connect and insert retrieved data into SQL using a different connection. At the same time I use a Connection Polling.
SqlConnection sqlconn = new SqlConneciotn(connstring)
open connection
Select from table1
insert into array variable
close connection
SqlConnection sqlconn = new SqlConneciotn(connstring)
open connection
insert into SQL
close connection
It work so far. Thank you very much and God Bless you.
/dabuskol
Ps : My program is running for 1 1/2 hour now with no problem registering to the log file and console.
|
|
|
|
|
dabuskol wrote:
My program is running for 1 1/2 hour now with no problem registering to the log file and console
Excellent! I'm glad it's working out.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
|
|
|
|
|
How would I go about creating a borderless CheckBox. I don't want it to display a border as I will be overlaying it onto an image that already has the border for the CheckBox.
|
|
|
|
|
Give checkbox border style to Groove
Thanks
Ramakrishna.J
SoftProSys.ltd.
|
|
|
|
|
I do not know if the string enter is number.
|
|
|
|
|
Here is one possible test implementation, but of course exceptions should not really be used like this (it is bad because an exception takes so much time to handle).
try
{
Convert.ToInt32(myString);
}
catch
{
}
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
|
|
|
|
|
|
You can do it in CurrentCellChanged event of datagrid.
Mazy
"Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland
|
|
|
|
|
I want to how I can hook the keyboard and mouse messagea using c#?
saleh
|
|
|
|
|
|
|
If you only want to hook these messages in your application, implement IMessageFilter and add an instance of your implementation to the list of message filters using Application.AddMessageFilter . See the documentation for the aforementioned method for more information and an example.
If you only want to do this for an application, DO NOT use a system-wide hook as you'll degrade the performance of the entire system at the cost of your application, and could possibly corrupt the entire system if you don't implement it correctly or efficiently. Also, the second response will only work per-Form and only if you have KeyPreview set to true so that the parent (the Form ) receives and processes messages sent to the child. You would have to do this on every form, then, which is pretty inefficient and can lead to code duplication.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I want to know how can I make the listview scroll to index that I want to view?? e.g If I have 100 items in my listView, I want to view it start from items 40 ~ , or I wnat to the range start from 80..??
Is there any way can do that? I've tried to use .items[index].selected = true. but seems doesn't work. Can someone tell me..
Thanks a lot
|
|
|
|
|
Selected only makes it the selected item. To make sure it's visible, use listView1.Items[index].EnsureVisible() . This won't make it selected, but you've already figured out how to do that.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you very much ~!!
It works, but is it possible to make the selected range always display in the middle of list view??
I just set the start and end index to visible, am I correct ?
listView1.Items[start].EnsureVisible();<br />
listView1.Items[end].EnsureVisible();
but sometimes, the range showed at very bottom.
e.g. if I currently seeing item 5~20, then I wnat to jump to see item 70~80, so the items display 70~80 at the bottom.
Is there a way to improve ?
thank you
|
|
|
|
|
EnsureVisible only makes sure the item is visible. "It" doesn't care where in the list view it's visible.
If you always want a certain number to be at the top or anything of that nature, you can always calculate the position of what item you want at the top and send the SBM_SETPOS to the control (which gets passed to the scroll bars) or use the SetScrollPos function (recommended).
So, if you wanted item 70 to be at the top, you would do something like this:
[DllImport("user32.dll")]
[return:MarshalAs(UnmanagedType.SysInt)]
private static extern int SetScrollPos(IntPtr hWnd,
[MarshalAs(UnmanagedType.SysInt)] int nBar,
[MarshalAs(UnmanagedType.SysInt)] int nPos,
bool bRedraw);
private const int SB_HORZ = 0;
private const int SB_VERT = 1;
SetScrollPos(listView1.Handle, SB_VERT, 70, false);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Guys,
I tried to insert data into SQL server database using the following code. All data that I received from all my connections (10 socket). It runs for sometime It was able to insert some data then suddenly in the middle of the process I encountered
"The connection is already Open (state=Open)". when it goes to SaveTOSQL.
I declare my DBConnect as modular and with CONNECTION POOLING.
//DBConnect = new SqlConnection("Pooling=true;server=OSSSQL01;database=nfmse;uid=alarmadmin;pwd=alarmadm1n;Min Pool Size=3;Max Pool Size=100;");
Processdata()
{
iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx]; // + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
StringBuilder szData = new StringBuilder();
szData.Append(chars);
DBConnect.Open();
SaveTOSQL();
}
void SaveTOSQL()
{
string strInsert = "insert into rop values ('" + strjv.Trim() + "','" + strdate.Trim() + "','" + strclass.Trim() + "','" + strFile + "','" + strFile + "')" ;
DBCommand = new SqlCommand(strInsert,DBConnect);
DBCommand.ExecuteNonQuery();
}
PS : I don't close the connection nor the command becuase this is a 24 hour process.
Thanks in advance.
/dabsukol
|
|
|
|
|
Are you sure the error is not actually occurring on DBConnect.Open(); ?
I say this because the debugger will often highlight the line after the one where the error is if the error occurred in a method for which there is no source code.
I cannot see anything in SaveTOSQL() that would generate the message you are getting, and if the error did occur there then you would be saying which line in SaveTOSQL() generated the error.
I suggest that you enclose the DBConnect.Open(); in an if statement checking the State property of the connection before trying to open it.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
|
|
|
|
|
Yes definitely it's not from DBConnect.Open(). I apologize because the DBconnect was placed on top of the process function meaning it will only be executed once, once the program started.
I was able to solve the problem becuase of too much testing I forgot to remove my other DBConnect.open on one of the function. SORRY IT's my mess....
This is actually the real problem. I will open a new thread as not confused others.
System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ossrop.Class1.SaveToSQLDB(String strFile, String strOpt) in d:\ossrop\class1.cs:line 560
|
|
|
|
|
while (samen.EndsWith(",") )
{
string aant = samen.Length.ToString();
int aantal = (int)Convert.ChangeType(aant,typeof(int));
// samen.Remove(aantal - 1,1);
samen.Substring(0,aantal-1);
}
How come this doesn't work? I have a string that ends with a few ',' and i want them out of the string. Replace is not an option because there are other ',' who have to stay in the string. Only the last ones have to be deleted.
Thx in advance!
|
|
|
|
|
string aant = "this is a test,please be guided,";
string newaant="";
newaant = aant.Substring(0,aant.Length-1);
try this one.
thanks
|
|
|
|
|
|
To note why, a string is immutable meaning that anything you do to it creates a new string. If you want a mutable string, use the StringBuilder , but it really wouldn't work so great in this case.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|