|
I can't have my cocoa until the nurse has bought me my tablets and she's late.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas?
Because 31 Oct = 25 Dec.
|
|
|
|
|
I'm afraid she broke down, after having to bring you truck loads of beer, coffee and cigarettes all day long. Besides, the local pharmacies have long closed, and you shouldn't expect her to travel all the way to the other side of Wolverhampton, just to buy you some tablets. I suggest you have some more After Eights and call it a day. You can catch up on tablets on Monday.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
|
Problem solved!
I'm so excited!Thank you. This is a good forum!
|
|
|
|
|
hi, I'm using below code for a basic client/server communication. I send a string to server and server sends it back to client with no problem. But I can do this only once and then connection goes, what should I do to be able to send more strings one after other without loosing the connection? Thanks in advance
/// Server Code:
private void button1_Click(object sender, EventArgs e)
{
TcpListener tcpListen = new TcpListener(IPAddress.Parse("192.168.1.6"), 1234);
tcpListen.Start();
Socket socket = tcpListen.AcceptSocket();
if (socket.Connected)
{
while (true)
{
NetworkStream stream = new NetworkStream(socket);
StreamReader streamRead = new StreamReader(stream);
StreamWriter streamWrite = new StreamWriter(stream);
try
{
streamWrite.WriteLine("Incoming message: " + streamRead.ReadLine());
streamWrite.Flush();
}
catch (Exception)
{
label1.Text = "Server is being shutdown";
return;
}
}
}
socket.Close();
}
/// Client Code:
public TcpClient client;
NetworkStream stream;
StreamReader streamRead;
StreamWriter streamWrite;
private void button1_Click(object sender, EventArgs e)
{
try
{
client = new TcpClient("192.168.1.6", 1234);
}
catch (Exception)
{
MessageBox.Show("Not Connected");
}
stream = client.GetStream();
streamRead = new StreamReader(stream);
streamWrite = new StreamWriter(stream);
try
{
streamWrite.WriteLine(textBox1.Text);
streamWrite.Flush();
string line = streamRead.ReadLine();
MessageBox.Show("Message from server: " + line);
}
catch (Exception)
{
MessageBox.Show("Connection error");
}
streamWrite.Close();
streamRead.Close();
stream.Close();
}
|
|
|
|
|
teknolog123 wrote: what should I do to be able to send more strings one after other without loosing the connection?
Dah, How about not closing the socket.
Of course, the connection is lost each time, you told it to do that. Create the connection outside of the button click event and don't close it until necessary.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
thanks but even if I don't close the socket and listener, it throws exception. I've tried many things but no success, I'll go on trying
|
|
|
|
|
the smart thing to do is to catch exceptions in a variable, say exc, and look at them using
exc.ToString() ; that would tell you precisely what went wrong, and enable you to find an adequate cure.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
... and when you close a Socket, you should dispose of it too.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Where to start.. where to start. there's so much wrong with this code that it seems clear you haven't even a basic idea of how execution scopes work, or what happens when you leave a block of code. I'm not criticizing you, but you're lacking some fundamental knowledge here.
First, in your "server" you click a button to start.. ok, but all your code is running within that button click event.. which will lock up your UI on the server until processing is finished. This is probably not what you have in mind for a long running connection. You will most likely have to create a worker thread to handle this code, which I won't go into detail about much here.. but something for you to research into.
Next, you create new NetworkStream and StreamReader/Writer objects within your while loop. This is highly wasteful, and will likely cause a lot of problems. I believe that when the NetworkStream object is destroyed at the end of your while loop, it will close the socket that it's attached to unless you specify false for socket ownership in the constructor. So your first step would be to move those three lines to outside of your while loop (but inside the if statement), that will keep them around while you process things.
Next, you need a way to tell when the socket has been closed. Certainly, your exception handler will do that, but it's not the "correct" way. ReadLine() returns null when the socket is closed. So you should change your code to exit the loop when ReadLine() returns null.
Now on to the client. First, you will probably want to create a "connect" button and a "disconnect" button (or you can get fancy and use the same button for both). This will create the TcpClient connnection and leave it open. The close will close the connection. Then you button click will do nothing but send the text in your textbox. You may want to create a second textbox to display the output that comes back instead of using a MessageBox.
--
Where are we going? And why am I in this handbasket?
|
|
|
|
|
Hey guys, I am stumped with this one.
Here is an Insert Statement that works fine in my SQLEXPRESS database.
da.InsertCommand = new SqlCommand("INSERT INTO VW411Data VALUES('S100000','P100000','12/08/57','11:04','Steven Baldwin', 'C:\\ImageDir\\S100000a.jpg', 'C:\\ImageDir\\S100000b.jpg', '0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1')", cs);
The first seven fields come from objects on my form and I wrote a simple query using command parameters to create a partial record. This works also.
da.InsertCommand = new SqlCommand("INSERT INTO VW411Data(SerialNbr, ModelID, Date, Time, UserName, ImageLink1, ImageLink2, Pass,) VALUES(@SerialNbr, @ModelID, @Date, @Time, @UserName, @ImageLink1, @ImageLink2, @Pass)", cs);
da.InsertCommand.Parameters.Add("@SerialNbr", SqlDbType.Char).Value = tbBC1.Text;
da.InsertCommand.Parameters.Add("@ModelID", SqlDbType.Char).Value = tbBC2.Text;
da.InsertCommand.Parameters.Add("@Date", SqlDbType.Char).Value = textBox3.Text;
da.InsertCommand.Parameters.Add("@Time", SqlDbType.Char).Value = textBox4.Text;
da.InsertCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = tbUserName.Text;
da.InsertCommand.Parameters.Add("@ImageLink1", SqlDbType.VarChar).Value = "C:\\ImageDir\\" + tbBC1.Text +"a.jpg";
da.InsertCommand.Parameters.Add("@ImageLink2", SqlDbType.VarChar).Value = "C:\\ImageDir\\" + tbBC1.Text + "b.jpg";
da.InsertCommand.Parameters.Add("@Pass", SqlDbType.Int).Value = 1;
cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();
The remaining fields are coming from a vision system over Ethernet in a csv format. My plan was to build a statement using a variable to hold the string for the remaining fields and attach the variable to a SQL statement. My final SQL Insert statement will be an all statement so I won't have to identify the fields. Here is my question...
Can I build a statement that contains both the command parameters and holds a variable containing the remaining statement or must I create two Insert statements (First 8 fields, then remaining 126 fields)? I know the latter is preferred but it will require me to identify all of the fields in my query for a partial Insert.
I do not get involved with SQL very often so my status is newbie at this point.
Thanks of all suggestions.
Steve
|
|
|
|
|
If you want to assign variables to parameters then they must be defined, no way around it. A more advanced solution may be pass one delimited variable to a stored procedure and have it parse everything.
With the number of parameters you have I would look at Entity Framework or Linq to SQL and let the framework handle the underlying assignments and sql statements.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Thanks,
I love a challenge. I'll start researching now. If you can think of anything else, just let me know. This job has so many facets I don't know if I'll ever get it right.
Steve
|
|
|
|
|
Hi!
I have to add an object to an entity, but the name is not hard-coded, so I have to use the AddObject(name,obj) method instead of AddToX(obj) .
Having only obj, how to elegantly obtain name?
The following two approaches work, but they are not very elegant.
1.
_context.AddObject(_table.CommandText.Substring(1,_table.CommandText.Length-2), item);
2.
_context.AddObject(((IQueryable) _table).ElementType.Name, item);
EDIT: I came up with another idea:
_context.AddObject(typeof(T).Name, item);
Is there a better solution? If not, which of these is better?
Thanks.
Greetings - Jacek
modified on Saturday, June 5, 2010 10:56 AM
|
|
|
|
|
This make no sense. You have an entity object you should know what type it is, it's your code, right? Unless you have a method such as this, which is a poor design to begin with.
Add(object obj)
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: Add(object obj)
No, but I have a method such as this:
Add(T obj)
in a generic class.
It would be cool if an entity object had an Add method. But it does not. Instead of this, the objectContext.AddToX(X entity) method must be used, where X is a name of an entity.
However, if you look into assembly, all these AddToX methods are implemented in the following fashion:
public void AddToX(X x)
{
base.AddObject("X", x);
}
So, calling AddObject(name, object) directly is not as bad as it looks like.
I think I don't exactly understand how all this works. The properties objectContext.X represent tables, doesn't they? Why there is no Add method in them? If they had Add(T item) method then my problem would be nonexistant. Now I have to use sick statements like typeof(T).Name to get it working.
Greetings - Jacek
modified on Saturday, June 5, 2010 6:36 PM
|
|
|
|
|
Jacek Gajek wrote: I think I don't exactly understand how all this works.
Exactly.
IMO trying to make a generic method such as you are trying is fruitless and unnecessary. Don't you think Microsoft would have implemented such method already if it was such a good idea?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: Exactly.
IMO trying to make a generic method such as you are trying is fruitless and unnecessary. Don't you think Microsoft would have implemented such method already if it was such a good idea?
Alright, but the statement does not solve my problem.
My intension was to automate Insert and Delete commands for ListBox/ListView controls. Now I don't have to handle a KeyDown event for each of them. Instead I use
EventManager.RegisterClassHandler(typeof(ListBox), KeyDownEvent, new KeyEventHandler(ListBox_KeyDown));
private static void ListBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
var listBox = sender as ListBox;
if (listBox != null)
{
object item = listBox.SelectedItem;
if (item != null)
{
var list = listBox.ItemsSource as IList;
if (list != null)
list.Remove(item);
}
}
}
}
As ItemsSource I set
class ObservableTable<T> : ObservableCollection<T> where T : class
{
private readonly ObjectQuery<T> _table;
private readonly ObjectContext _context;
public ObservableTable(ObjectQuery<T> table, ObjectContext context) : base(table)
{
_table = table;
_context = context;
}
public ObjectQuery<T> Table
{
get { return _table; }
}
protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
_context.AddObject(typeof(T).Name, item);
}
protected override void RemoveItem(int index)
{
T item = this[index];
base.RemoveItem(index);
_context.DeleteObject(item);
}
}
Now, if a user presses delete key in any ListBox then the current item it is marked for deletion without writing a single line of code.
I don't know how to achieve this level of automation in other (better?) way.
Greetings - Jacek
|
|
|
|
|
Nice idea but I prefer more a loosely coupled architecture. The same thing can be achieved without tying the listbox directly to the datacontext.
IMO this is not a good design. What if the user changed their mind and did not want to insert the item? You are forcing them to delete the object rather cancel the action.
This solution also cannot be used with disconnected objects since you are relying on the context to track the objects for addition and deletion. At point do you commit the changes? You are also not taking into account possible conflicts, such as primary key violations.
Not helping to solve the problem you are having? Correct, because IMO the design is flawed and thus there is no problem to solve.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Should I leave that idea and handle KeyDown event separately for each control which I want to support item deletion? Will it be a good design?
I am very new to WPF.
Greetings - Jacek
|
|
|
|
|
how to handle Server Not found exception at client side in .net remoting.
when server is not running.
currently i am handling socket exception, but each remoting type
error gives socket exception, Is there any specific exception
generated by .net when server is not running?
|
|
|
|
|
Intellisense will tell you what exceptions could be generated by a given method. If there is no apparent exception for "server not found", then just handle a standard Exception to catch everything not explicitly defined:
try
{
}
catch (InvalidOperationException e1)
{
}
catch (ThisOtherException e2)
{
}
catch (Exception e3)
{
}
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hi,
I am bit confusing about output of fallowing variable 'a' which produce '-1' as result.
sbyte a =unchecked((sbyte)255);
I have not cleared yet why this store -1
plz clarify me.
no knowledge in .net
|
|
|
|
|
sbyte (signed byte) has a range from -128 to 127.
Because you used unchecked no overflow is detected. So, 255 is signed -1 .
Cheers
I don't like my signature at all
|
|
|
|
|
The sign is represented by the MSb (most significant bit).
Binary Hex byte sbyte
00000000 0x00 0 0
...
01111111 0x7F 127 127
10000000 0x80 128 -128
...
11111111 0xFF 255 - 1
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
modified on Saturday, June 5, 2010 9:09 AM
|
|
|
|
|