I am trying to invoke the SqlInfoMessageEventHandler, but not able to. Can anyone please help? Here is my code:
class Program
{
static void Main(string[] args)
{
SqlConnection sqlConnect = new SqlConnection(#"Integrated Security=SSPI;Initial Catalog=DEV_ENV;Data Source=SQL15");
using (SqlCommand command = new SqlCommand("SELECT 1 FROM EMPLOYEE", sqlConnect))
{
sqlConnect.Open();
sqlConnect.InfoMessage += new SqlInfoMessageEventHandler(MessageEventHandler);
command.ExecuteNonQuery();
}
}
static void MessageEventHandler(object sender, SqlInfoMessageEventArgs e)
{
Console.WriteLine(e.Message);
}
}
Related
Trying to code chat in Java, but i have a problem with inputting strings from Scanner to DataOutputStream.
Client part:
public class Client {
private static Scanner sc = new Scanner(System.in);
private static Socket client;
private static Random r = new Random();
public static int status = 0;
public static String nickname;
public static void main(String[] args) {
connect();
handle();
// end();
}
private static void connect()
{
try {
client = new Socket("localhost",9999);
DataOutputStream dos = new DataOutputStream(client.getOutputStream());
//sc - Scanner(System.in);
//sl - nickname i want to put into DataOutputStream
String sl = sc.nextLine();
sendPacket(new Greetings(sl));
} catch (IOException e) {
e.printStackTrace();
}
}
private static void sendPacket(Packet packet)
{
try {
DataOutputStream dos = new DataOutputStream(client.getOutputStream());
System.out.print(dos.size());
dos.writeInt(packet.getId());
packet.send(dos);
} catch (SocketException se) {} catch (IOException e) {
e.printStackTrace();
}
}
private static void handle()
{
}
private static void end()
{
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am using package system to send information. Format of auth package is [id(int)]+[name(String)]
If i send information without scanner (like hardcoded name and id) it works.
Greetings packet code:
public class Greetings extends Packet{
private String nickname;
public Greetings(){}
public Greetings(String nickname)
{
this.nickname = nickname;
}
#Override
public int getId() {
System.out.print(nickname);
return 120;
}
#Override
public void send(DataOutputStream dos) {
System.out.print("GOT IT");
try {
dos.writeUTF(nickname);
System.out.println("Nickname "+nickname);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void receive(DataInputStream dis) {
}
}
It seems like something stupid that i can't see, and it's driving me crazy.
Server's part:
//For each new client new handler
public class ClientHandler extends Thread {
private Socket socket;
public ClientHandler(Socket socket)
{
this.socket = socket;
start();
}
public void run()
{
while (true)
{ try {
if (!isInterrupted())
{DataInputStream dis = new
DataInputStream(socket.getInputStream());
if (dis.available() > 0)
{
Packet p = PacketManager.getPacketById(dis.readInt());
p.receive(dis);
p.handle();
}} } catch (SocketException se)
{} catch (IOException e)
{} finally {
end();
}
}
}
private void end()
{
interrupt();
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
It seems like smt simple i just can't see. Like, there's something different between scanner's string and usual string. But theirs hex code is the same...
Problem appears when you write both INT and STRING into dataOutuputStream.
I'm saying i'm not a programmer but a guy who has been learning to program with java for a while. I hope to find the solution to my problem here. I'm trying to program my home automation system and remote control and to do this, I chose to use Kryonet. My problem is that every time I send the data to the server, the client opens a new connection. It's been 3 weeks since googlo and I try to figure out how to do it but with no results.
Every help is seriously appreciated. This is my code. Thank you.
This code work in my home network.
Sorry for my english...
public class MainActivity extends AppCompatActivity {
Button button;
String IP = "";
EditText editText;
TextView textView;
EditText editText3;
public static String msg_response;
public static String msg_request;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new MyHandler();
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
editText3 = (EditText) findViewById(R.id.editText3);
textView = (TextView) findViewById(R.id.textView);
int MY_PERMISSIONS_REQUEST_INTERNET = 1;
int MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE = 1;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.INTERNET},
MY_PERMISSIONS_REQUEST_INTERNET);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_NETWORK_STATE},
MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE);
int MY_PERMISSIONS_REQUEST_ACCESS_WIFY_STATE = 1;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_WIFI_STATE},
MY_PERMISSIONS_REQUEST_ACCESS_WIFY_STATE);
textView.setText(msg_response);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
msg_request = valueOf(editText3.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}
MyThread myThread = new MyThread(handler);
myThread.start();
}
});
}
private class MyHandler extends Handler {
#Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
if (bundle.containsKey("msg da server")) {
String msgin = bundle.getString("msg da server");
textView.setText(msgin);
}
}
}
class MyThread extends Thread {
private Handler handler;
public MyThread(Handler handler) {
this.handler = handler;
}
public void run() {
System.out.println("MyThread running");
Client client = new Client();
client.start();
Kryo kryoClient = client.getKryo();
kryoClient.register(SampleRequest.class);
kryoClient.register(SampleResponse.class);
try {
client.connect(5000, "192.168.0.101", 54555, 54666);
} catch (IOException e) {
e.printStackTrace();
}
client.addListener(new Listener() {
public void received(Connection connection, Object object) {
if (object instanceof SampleResponse) {
SampleResponse response = (SampleResponse) object;
System.out.println(response.text);
msg_response = response.text.toString();
invia_activity(msg_response);
}
}
});
SampleRequest request = new SampleRequest();
request.text = msg_request;
client.sendTCP(request);
}
private void invia_activity(String invia) {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("msg da server", "" + invia);
msg.setData(b);
handler.sendMessage(msg);
}
}
}
I dont have an direct solution, but i have an tutorial for it. I used the same one. So there the connections keeps open, and you can send as many packets as you need. Its without audio, but the code works well. After that you can experiment with the code. It works fine for me. This is the tutorial
I hope i can help you with this.
EDIT:
Maybe you can make an
public static Connection conn;
and you could use that object again and again as your connection to the server.
I have created a WCF which I then created a WP8 application and added the service reference. The error is "An exception of type 'System.ServiceModel.FaultException' occurred in System.ServiceModel.ni.dll but was not handled in user code"
If anyone could possible give me some indication as I have googled this and found nothing the same.
The error comes up here
public void EndAddUser(System.IAsyncResult result)
{
object[] _args = new object[0];
base.EndInvoke("AddUser", _args, result);
}
My main classes:
public partial class MainPage : PhoneApplicationPage
{
private Service1Client _serviceClient;
// Constructor
public MainPage()
{
InitializeComponent();
_serviceClient = new Service1Client();
_serviceClient.LoginUserCompleted += new
}
private void loginBtn_Click(object sender, RoutedEventArgs e)
{
_serviceClient.LoginUserAsync(userNameTxtBox.Text, passwordTxtBox.Text);
}
private void newAccountBtn_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("/AddAccount.xaml", UriKind.Relative));
}
private void _serviceClient_LoginUserCompleted(object senter,
LoginUserCompletedEventArgs e)
{
if (e.Error == null && e.Result != null)
{
MessageBox.Show("Welcome " + e.Result + "!");
}
else
{
MessageBox.Show("Could not log in. Please check user name/password and try
again.");
}
}
}
public partial class AddAccount : PhoneApplicationPage
{
private Service1Client _serviceClient;
public AddAccount()
{
InitializeComponent();
_serviceClient = new Service1Client();
_serviceClient.AddUserCompleted += new EventHandler<AsyncCompletedEventArgs>
(_serviceClient_AddUserCompleted);
}
private void addAccountBtn_Click(object sender, RoutedEventArgs e)
{
_serviceClient.AddUserAsync(fullnameTxtBox.Text, userNameTxtBox.Text,
passwordTxtBox.Text);
}
void _serviceClient_AddUserCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show("User account created. Please login");
this.NavigationService.GoBack();
}
else
{
MessageBox.Show("User account could not be added, Please try again");
}
}
}
![Server][1]
Code Server :
public partial class Form1 : Form
{
private TcpClient tcpclient;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
IPAddress[] localip = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress address in localip)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
textBox1.Text = address.ToString();
textBox2.Text = "8888";
}
}
}
private void button1_Click(object sender, EventArgs e)
{
TcpListener listenner = new TcpListener(IPAddress.Any, int.Parse(textBox2.Text));
listenner.Start();
tcpclient = listenner.AcceptTcpClient();
}
![client][2]
Code Client
namespace CLIENT
{
public partial class Form1 : Form
{
private TcpClient tcpclient;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
labelmessage.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
tcpclient = new TcpClient();
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));
try
{
tcpclient.Connect(ipe);
if (tcpclient.Connected)
{
labelmessage.Visible = true;
labelmessage.Text = "Conected...";
}
}
catch
{
}
}
}
}
Use the TCPClient instance returned by the AcceptTCPClient() method.
TCPClient _client = _listener.AcceptTCPClient();
// Add to listview here for connection
Console.WriteLine(String.Format("{0} connected", _client.Client.RemoteEndPoint.ToString()));
// Same for others
UPDATE
TCPClient _client = _listener.AcceptTCPClient();
ListViewItem _item = new ListViewItem(_client.Client.RemoteEndPoint.ToString());
listview1.Items.Add(_item);
More on working with ListView Controls
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx
How to get the slideshow of images with time interval of 2 seconds. I had referred the below code from stackoverflow but while running iam nt getting any images displayed.. Please tel where iam went wrong...
Xaml:
<image Name=myImg Source={Binding bi}/>
Code:
private DispatcherTimer tmr = new DispatcherTimer();
private List<string> images = new List<string>();
private int imageIndex = 0;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
tmr.Interval = TimeSpan.FromSeconds(5);
tmr.Tick += new EventHandler(tmr_Tick);
LoadImages();
ShowNextImage();
}
private void LoadImages()
{
// list the files (includede in the XAP file) here
images.Add("/images/filename1.jpg");
images.Add("/images/filename2.jpg");
images.Add("/images/filename3.jpg");
images.Add("/images/filename4.jpg");
}
private void ShowNextImage()
{
Imagesource bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
myImg.Source = bi;
imageIndex = (imageIndex + 1) % images.Count;
}
void tmr_Tick(object sender, EventArgs e)
{
ShowNextImage();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (!tmr.IsEnabled)
{
tmr.Start();
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
tmr.Stop();
base.OnNavigatedFrom(e);
}
Thanks in Advance
Change Name=myImg to x:Name="myImg" and remove the Source attribute entirely. Otherwise it looks like it should work.