|
Welcome to CodeProject and don't worry about the downvoting. Please feel free to ask as many questions as you want, but remember to state your questions in a clear and easy-to-comprehend manner, preferably with an example and/or code.
|
|
|
|
|
Ignore the downvotes. They don't really count for anything, and don't be scared to post new questions. The more you ask, the more you learn how the forums work and you'll soon get used to it.
|
|
|
|
|
Firstly I would have a look here so that you can construct your connection string[^]
then I can only assume that you are after declaring a string variable that you can use through out your class, so then I would look at this
public class myDatabaseClass
{
private string _ConnectionString = "my database string here";
public void ShowString()
{
Debug.Print(_ConnectionString);
}
}
Myself personally I prefer to use the AppConfig to store such changeable information as its easier to change without having to recompile and distrubite the application again.
I would have a read of this as it will give you a start : Configuring a Connection String in the App.Config File During Runtime in C#[^]
I know it is for Visual Studio 2010 but it will give you the general idea.
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
you can use below code Separete class file
public SqlConnection con;
public SqlTransaction trans;
public SqlConnection CreateConnection()
{
//string strCon = "";
//strCon = System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString");
con = new SqlConnection(ConnectionString.Conn());
return con;
}
private void OpenConnection()
{
CreateConnection();
con.Open();
}
private void CloseConnection()
{
con.Close();
con.Dispose();
}
public void BeginTransaction()
{
OpenConnection();
trans = con.BeginTransaction(IsolationLevel.ReadUncommitted);
}
public void CommitTransaction()
{
trans.Commit();
//con.BeginTransaction();
trans.Dispose();
CloseConnection();
}
public void RollbackTransaction()
{
trans.Rollback();
trans.Dispose();
CloseConnection();
}
Call Bunn Layer
Connection conn = new Connection();
conn.BeginTransaction();
|
|
|
|
|
i want to create a connection string in class file and i have to use that connection string in my .cs file that is C#.net i m using visual studio 2008 and sqlserver 2005 .. anyone plz post ur ans r example for this
modified 13-Aug-12 9:17am.
|
|
|
|
|
This isn't going to go well for you.
The rules at the top say do not post programming questions here, and...
Member 9259606 wrote: anyone plz post ur ans r example for this
You will not get a response using language like that, use proper words and ask in the Quick Answers section.
Every man can tell how many goats or sheep he possesses, but not how many friends.
Shed Petition[ ^]
|
|
|
|
|
you just commited blasphemy. You posted a programming question in the lounge and violated about every rule in the book.
Please go to the C# forum and post your question there AFTER reading the guidelines (guidelines are always the first post in each forum, highlighted in yellow)
prepare for some downvoting and flaming, but don't take it personally, just follow the guidelines in the future and you'll be fine.
V.
modified 13-Aug-12 9:14am.
|
|
|
|
|
V. wrote: you just commited balphemy
Balphemist!
|
|
|
|
|
woopsy-daisy
V.
|
|
|
|
|
sure u can have answer, best 2 stare in space for long time - annswer will cum to u in flash
|
|
|
|
|
Yeah... annswer is a terrible guy he always make the same...
|
|
|
|
|
Norm .net wrote: best 2 stare in space for long time - annswer will cum to u in flash
That's what she said...
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Norm .net wrote: best 2 stare in space for long time - annswer will cum to u in flash
That's a completely different website.
|
|
|
|
|
Going to work I was listening a strange low music... you know that typical songs that are played during those old terror films just before the ghostly spirits make their entrance... I was wondering which kind of blasphemous horror had happened and then I saw your post...
|
|
|
|
|
Hi all,
I am developing a Winform appln in C#, which contains multiple tabs..
i want to use same shortcut keys on different tabs for different controls.
but i am not able to the same, can any one help ??
Thanks in advance..
|
|
|
|
|
Set the Form's KeyPreview property to true and handle the KeyDown/KeyPress events. Don't handle the KeyDown/KeyPress events of the tab or the constituent controls. Within the event handler, you can look for specific key combinations and take action based on the active tab.
|
|
|
|
|
I'd recommend a simple if or case statement (whichever you prefer) to identify the current selected tab.
switch (tabControl.SelectedIndex)
{
case 0:
break;
case 1:
break;
}
This would work under the assumption that you were using the tab indices to find out what's currently selected. There's also a function that allows you to check the current tab by its name. It'd work in the same respect as the .SelectedIndex function, expect you'd be using the tab names rather than the indices. It's the .SelectedTab function.
I hope this was of some help. Happy programming!
|
|
|
|
|
There are some cool classes and tricks and short cuts in C#. I know this but I have not worked with C# enough to have them memoried.
One cool trick is this. If you read from a file data that is arranged in a similar line-by-line structure where each line is arranged in columns spaced out by tabs, the data can be read into a kind of class in C# and then you can parse out one column of data by simply doing a "for each" command. Does anyone know off-hand how this is done?
I need to update this.
I wrote a C# program that reads code one line at a time. Each line has data seperated by tabs.
Here is what my C# code is supposed to do. The line is supposed to be split into a string array by using the "Split" string methodIt is supposed to be spit read in text one line at a time and I use the "Split" method to group the line according to the tab character.
But, as this screen shot shows, it does not work. Any idea why?
http://i67.photobucket.com/albums/h292/Athono/problem.png
-- modified 18-Aug-12 15:16pm.
|
|
|
|
|
You might want to phrase it as an obvious question next time; as soon as I read "One cool trick is this", I assumed you where sharing a tip and skipped reading the rest.
You're looking for the filehelpers[^] library. An alternative would be using OleDb[^].
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Let's look at a simple example: I want to create a Button class with a different BackColor and Size than the "normal" button (.Net 4, Windows Forms). Looks easy, doesnt't it? But I face some problems.
A simple "solution" would be:
public class BernieButton : Button
..
public BernieButton()
{
this.BackColor = Color.Green;
this.Size = new Size(150,50);
}
Compile the project, drag a BernieButton onto Form1, and everything looks fine.
But now Sales decides that the BackCOlor must be yellow, and the size is too big. OK, change that in the constructor, test the new button, looks fine. Rebuild the old projects, and - the buttons there look like before. Failure!
How could that fail? When the button was dragged onto the form, the Designer added some lines in the InitializeComponent method of the form:
this.BernieButton1.BackColor = Color.Green;
this.ernieButton1.Size = new Size(150,50);
and that code gets executed after the constructor of the button, thus overriding the recent changes of the default value.
I tried overriding the properties:
[DefaultValue(typeof(Color), "Green")]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
and then set the default value in the constructor (using reflection, thus circumventing double storage of the new default value). That really works as it is intended, the designer does not write a line for BackColor in the InitializeComponent method.
But it does not work with Size. There is an important difference: BackColor is "virtual " in the Button class, Size is not.
I tried with the "new " keyword, but it fails: a line is added to InitializeComponent , and later changes of the default value are ignored.
I thought of setting the DefaultValueAttribute of the properties in the constructor. But that is not possible, attributes are read-only at run-time.
Do you have some ideas on how to overcome these limitations with inherited non-virtual properties?
|
|
|
|
|
There is a property DefaultSize that you can override. This works for me:
public class MyButton : Button
{
private static readonly Size SizeDefault = new Size(100, 50);
protected override Size DefaultSize
{
get { return SizeDefault; }
}
}
|
|
|
|
|
Sorry, it does not:
Drag the button onto a form, and it looks OK. Then change the DefaultSize, e.g.
private static readonly Size SizeDefault = new Size(200, 100); , rebuild your solution, and the button has still the old size. Only new buttons get the new default size, "old" buttons do not get updated.
Again, a look into Form1.Designer.cs shows the reason:
this.myButton1.Size = new System.Drawing.Size(100, 50); The key to the solution is (likely) in preventing the designer from writing that line - as with the BackColor property.
But a
public bool ShouldSerializeSize() does not help either.
And
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Size Size
{
get { return base.Size; }
set { base.Size = value; }
} prevents individually sized buttons.
|
|
|
|
|
Hmmm, I see what you're getting at now. It seems this is the case with all controls so maybe it's something coded into System.Windows.Forms.Control ?
I don't have reflector here, but I will have a poke around later and have a look in the source code...
|
|
|
|
|
I have written a simple and minimalist HTTP proxy server that runs on command line. In the Start() method, a TcpListener blocks until it gets a client request and creates a new thread (ThreadHandleClient method) that processes this client, fetches its url and relays data.
The trouble is in the relay logic that I want to refine. In the first inner loop, I receive all webserver data and send it to client until zero bytes are left. In second inner loop, I do vice-versa: receive all client data and send to web-server until zero bytes left. However, the code gets blocked in the beginning of second inner-loop at client.receive(). This is what I'm basically doing:
Any help will be truly appreciated. Thanks in advance.
public void Start(IPAddress ip, int port)
{
try
{
TcpListener listener = new TcpListener(ip, port);
listener.Start(100);
while (!stopFlag)
{
Socket client = listener.AcceptSocket();
IPEndPoint rep = (IPEndPoint)client.RemoteEndPoint;
Thread th = new Thread(ThreadHandleClient);
th.Start(client);
}
listener.Stop();
}
catch (Exception ex)
{
Debug.Print("START: " + ex.Message);
}
}
public void ThreadHandleClient(object o)
{
try
{
Socket client = (Socket)o;
NetworkStream ns = new NetworkStream(client);
byte[] buffer = new byte[2048];
int rec = 0, sent = 0, transferred = 0, rport = 0;
string data = "";
do
{
rec = ns.Read(buffer, 0, buffer.Length);
data += Encoding.ASCII.GetString(buffer, 0, rec);
} while (rec == buffer.Length);
string line = data.Replace("\r\n", "\n").Split(new string[] { "\n" }, StringSplitOptions.None)[0];
Uri uri = new Uri(line.Split(new string[] { " " }, StringSplitOptions.None)[1]);
Debug.Print("CLIENT REQUEST RECEIVED: " + uri.OriginalString);
if (uri.Scheme == "https")
{
rport = 443;
Debug.Print("HTTPS - 443");
}
else
{
rport = 80;
Debug.Print("HTTP - 443");
}
IPHostEntry rh = Dns.GetHostEntry(uri.Host);
Socket webserver = new Socket(rh.AddressList[0].AddressFamily, SocketType.Stream, ProtocolType.IP);
webserver.Connect(new IPEndPoint(rh.AddressList[0], rport));
byte[] databytes = Encoding.ASCII.GetBytes(data);
webserver.Send(databytes, databytes.Length, SocketFlags.None);
Debug.Print("SENT TO SERVER. WILL NOW RELAY: " + data);
buffer = new byte[2048];
rec = 0;
data = "";
do
{
transferred = 0;
do
{
rec = webserver.Receive(buffer, buffer.Length, SocketFlags.None);
Debug.Print("RECEIVED FROM WEBSERVER[" + rec.ToString() + "]: " + Encoding.ASCII.GetString(buffer, 0, rec));
sent = client.Send(buffer, rec, SocketFlags.None);
Debug.Print("SENT TO CLIENT[" + sent.ToString() + "]: " + Encoding.ASCII.GetString(buffer, 0, rec));
transferred += rec;
} while (rec == buffer.Length);
Debug.Print("loop-1 finished");
if (transferred == 0)
break;
transferred = 0;
do
{
rec = client.Receive(buffer, buffer.Length, SocketFlags.None);
Debug.Print("RECEIVED FROM CLIENT: " + Encoding.ASCII.GetString(buffer, 0, rec));
sent = webserver.Send(buffer, rec, SocketFlags.None);
Debug.Print("SENT TO WEBSERVER[" + sent.ToString() + "]: " + Encoding.ASCII.GetString(buffer, 0, rec));
transferred += rec;
} while (rec == buffer.Length);
Debug.Print("loop-2 finished");
} while (transferred > 0);
Debug.Print("LOOP ENDS. EXITING THREAD");
}
catch (Exception ex)
{
Debug.Print("Error occured: " + ex.Message);
}
|
|
|
|
|
If no data has arrived, then Receive is going to block. If this is not what you want, then use BeginReceive .
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|