|
The common way to do this is to show the form before you call Application.Run, then open up the form. This might end up looking like:
public static void Main()
{
using (LoginDialog dlg = new LoginDialog())
{
if (dlg.ShowDialog() == DialogResult.OK)
{
if (dlg.IsValidLogin)
{
Application.Run(new MDIForm1());
}
else
{
MessageBox.Show("Login failed");
}
}
}
}
|
|
|
|
|
Hello I am coding a data transfer class for my project, in which I am transfering a file peer to peer from one machine to another. Im sending data through sockets and network stream. After syaing that here is my problem, on the client side i have a thread that reads the file converts it to bits and write it to the network stream. On the server side i have another thread listing for incomming connections. when a data transfer command comes it runs a method used to read the data from the network stream. The problem is that sometimes the client dosent have enought time to write the whole file to the stream beacuse the server immedeatly starts reading from the network stream and this is resulting in data being corruted.
My temp solution was to include the following code "Thread.sleep(5000)" before the RecieveData method so the server waits 5 seconds before attempting to read from the stream. This worked but I noticed that if the file size increases dramaticaly 5 seconds is not enough time for the client to write to the stream, is there any mathematical solution that i can use or any other way to maybe work out this problem. Thank you guys
|
|
|
|
|
You shouldn't need to worry about if the server has transferred some or all of the file before beginning your reading. What class and function are you using to receive data, and how are you calling it?
|
|
|
|
|
Hello, to recieve the data i am first waiting for the client to wrie all the data into the stream and then the server connects the network stream to the incoming socket and reads the data. Below i have pasted the code where the server is downloading the data from the client.
if (clientsocket != null)
{
//Retrieve Stream from the socket that is connected to the client machine
NetworkStream incomingNetworkStream = new NetworkStream(clientsocket);
//Output Stream which will be used to store the incoming file onto the machine's HDD
FileStream fsout = new FileStream(parser.TemporaryFilesPath + @"\database.zip", FileMode.OpenOrCreate, FileAccess.Write);
long size = Convert.ToInt64(incomigFileSize);
long rdby = 0;
int len = 0;
if (incomingNetworkStream.DataAvailable)
{
while (rdby < size)
{
//buffer to store a portion of the incoming file
byte[] buffer = new byte[1024];
len = incomingNetworkStream.Read(buffer, 0, buffer.Length);
fsout.Write(buffer, 0, buffer.Length);
rdby = rdby + len;
}
fsout.Flush();
fsout.Close();
isReading = false;
}
else
{
}
byte[] datatosend = Encoding.ASCII.GetBytes("TransferCompleet");
serverSendSocket.Send(datatosend, datatosend.Length, 0);
}
The problem is that sometimes the data isnt read correctly and i think because the client pc is not finsihed writing to the stream when the server starts reading from the stream.
|
|
|
|
|
Ah, I think I might see the issue.
The Read function is a little funny on streams. It only guarantees that you will get some data back, but not how much. You are doing the right thing by checking the return value of Read and incrementing len - forgetting to do this is actually a common mistake you see in real-life programs.
When you are writing to your file, you need to only write how much data you read.
Say your read function reads in 500 bytes...
Notice the line
fsout.Write(buffer, 0, buffer.Length);
Even though you only read 500 bytes, you're writing out 1024 (buffer.Length)! You need to only write out to the file however much you read in. Fix that line, and I'll bet your program works without the need for doing thread sleeps.
|
|
|
|
|
Thank you m8 what was good help .
|
|
|
|
|
Yes, don't call BeginReceive again until you are finished with the data returned, or if you do provide a different offset.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
hi every one,
I'm trying to connect to a database in SQL server 2005 from a c# application. My goal is to use SQL Authentication using a user id and a password. My problem is in sql server side where I'm trying to work with logins, users, roles, schemas, etc.
Further more is it possible to deploy the applicatin with sqlExpress so that no one could access the database except for the application using it?
thnx
|
|
|
|
|
If SQLExpress is installed on the machine, I don't see how unless you consistently encrypt the database.. You can control decrypting through your application to decrypt records on the fly but that might be slightly inefficient depending on what you are doing. Check into obfuscation for a broad range to start securing your code, and .Net has a lot of encryption related classes so you can learn how to store your data on a database to prevent people from snooping.
|
|
|
|
|
Hi guys, I am wondering if there is a way to add a RadioButtonList inside the Listview control. Currently, my Listview checkboxlist is behaving like a radio button. But I want to change its look so its UI looks appealing.
Thanks
|
|
|
|
|
I dont think what you want is easily done, would have to be with some paint event coding.
The best thing would prob be a custom control, wouldnt be too hard for your requirements
My opinion is... If someone has already posted an answer, dont post the SAME answer
|
|
|
|
|
Check out the RadioButtonRenderer class
|
|
|
|
|
Hi all,
Could you please help me how to access a database from the following config file(web config).
<configuration>
<sqlserver>SERVER_BMRD</sqlserver>
<sqldatabase>B</sqldatabase>
<sqluser>BD_user</sqluser>
<sqlpassword>^1^vtqhCBOwbVJc+ODAe2Sm7A==</sqlpassword>
<sqltrusted>False</sqltrusted>
</configuration>
How will i be able to connect to the above database?
For example :
SqlDataAdapter sda = new SqlDataAdapter("Stored_procedure", Connectionstring);
Can you give me how my connection string should look like?
Thank you so much
|
|
|
|
|
|
Thanks for your response Rob.
It was given to me as a database credentials to be able to connect to the database.
You can see that they dont use connection string ?so that is my problem
How can i connect to a database using those credentials?
Regards,
kibrom
modified on Monday, February 9, 2009 12:23 PM
|
|
|
|
|
OK, to my knowledge that won't be easy. Config files have to follow a certain format, and you define sections when you want to implement your own settings. Just placing individual bits in there like that will likely cause the ConfigurationManager to protest.
Your app.config file is invalid. As such, the best bet would be to bypass the ConfigurationManager altogether and process the file as any other xml file. One such way would be to use an XmlDocument and retrieve the settings using XPath.
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob ,
Thank you so much for your help.Sorry as i am new to this could you please give me a tip on how to use it with XMLReader or XMlDocument?
Thanks a lot.
|
|
|
|
|
Sure. I'm just about to leave work now, so I'll put something together in a couple of hours once home. A few things to check - this file is called web.config and resides in the usual place with your application?
Also, you absolutely cannot change the format of this file? A simple inclusion of appSettings would make this a breeze.
I'd create a class which reads this file and then exposes each setting you need as a public string property. If I remember correctly the ASP.NET world watches web.config and updates automatically - you wouldn't get that with this approach.
More later...
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob,
Your help is Much appreciated Rob.Yes please its a config file.But its not a webconfig file.For instance the above can be called admin.config file.
The file doesnt have app setting? we can still include appSettings.However they dont have it in this file.
Thanks again for your time.
|
|
|
|
|
The class below should do what you want, create a new instance passing in the config file as the parameter to the constructor.
public class DatabaseConfiguration
{
public readonly string SqlServer;
public readonly string Database;
public readonly string User;
public readonly string Password;
public readonly bool Trusted;
public DatabaseConfiguration(string path)
{
XmlDocument settings = new XmlDocument();
settings.Load(path);
SqlServer = settings.SelectSingleNode("/configuration/sqlserver").InnerText;
Database = settings.SelectSingleNode("/configuration/sqldatabase").InnerText;
User = settings.SelectSingleNode("/configuration/sqluser").InnerText;
Password = settings.SelectSingleNode("/configuration/sqlpassword").InnerText;
Trusted = settings.SelectSingleNode("/configuration/sqltrusted").InnerText.ToLower() == "true";
}
}
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob,
You are a champion,thanks a lot.I have done it like you said.
Sorry one more question, Can you give me an example on how to execute a stored procedure with the class.
Like for instance :-
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("Test", ???????????????);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add("@timeInStart", SqlDbType.DateTime);
sda.SelectCommand.Parameters["@timeInStart"].Value = from;
sda.SelectCommand.Parameters.Add("@timeInEnd", SqlDbType.DateTime);
sda.SelectCommand.Parameters["@timeInEnd"].Value = to;
sda.Fill(ds);
????????- This is a place for connection string? How do i go about that?
Thanks a lot Rob
|
|
|
|
|
Well, your connection string should look like this (from connectionstrings.com):
Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;
So, add a new string property to the class and construct that string using what you have. That's what you want for ?????????.
eg:
public string ConnectionString
{
get
{
return string.Format("Server={0};Database={1};User ID={2};Password={3};Trusted_Connection={4};", SqlServer, Database, User, Password, Trusted ? "True" : "False");
}
}
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob,
Thanks very much.Can we connect to the database with out using the connection string?
Just with the class you have given me? Can you give me just an example.
Sorry i am taking you time.
Thanks Rob
|
|
|
|
|
Yeah, you could add some functionality to that class which returns a valid open connection, and just use that as the connection
Something like:
public SqlConnection CreateConnection()
{
SqlConnection connnection = new SqlConnection(ConnectionString);
connection.Open();
return connection;
}
Remember to use the connection in a using bloke or call its dispose when you are done with it.
Regards,
Rob Philpott.
|
|
|
|
|
Do you think this should work to execute a stored procedure.
SqlDataAdapter sda = new SqlDataAdapter("Stored_proce", DatabaseConfiguration.connectionstring);
or simply use
SqlDataAdapter sda = new SqlDataAdapter("Stored_proce", DatabaseConfiguration.createConnection
I hope this will definely sort me out.
Thanks a million Rob.
|
|
|
|