|
Try putting some Console.WriteLine s in the code so that you can see when some thread dies or is created. E.g.
int threadTimeout = 2000;
for (int loop=0; loop < syncServers.Length; loop++)
{
Console.WriteLine("Loop = " + loop.ToString());
this.syncServer = syncServers[loop];
Thread syncThread = new Thread(new ThreadStart(DoSync));
Console.WriteLine("Starting Thread");
syncThread.Start();
Console.WriteLine("Main Thread is sleeping");
Thread.Sleep(threadTimeout);
Console.WriteLine("Main Thread Awake");
if (syncThread.IsAlive)
{
Console.WriteLine(" Sync thread is alive");
syncThread.Abort()
Console.WriteLine(" Sync thread is dead");
}
}
Console.WriteLine("Finished"); Just so that you can see what's going on maybe that'll tell you where it's hanging.
You know you're a Land Rover owner when the best route from point A to point B is through the mud.
Ed
|
|
|
|
|
how can I know if the MDI Child Closing event triggered due to the MDI Parent close or itself?
-- modified at 13:01 Wednesday 7th June, 2006
or what event can I use in the parent before the closing event of the child are triggered ?
(the closing event of the parent is triggered after the child...)
|
|
|
|
|
As far as I know there is no clean way to have the MDI parent detect that the child window is closing before the child's Closing event fires. You could override OnClosing in the child Forms and cast the MdiParent property to whatever type it actually is and call a method defined on your parent form subclass. That method would serve as a notification to the MDI parent that a child window is closing.
Hope that helps,
Josh
|
|
|
|
|
Hi,
I have the small doubt,I created a table Author in oracle and i put primary key constraint on authorid field.And i created another table Article and foreign key constaraint placed on this table's authorid.Now iam inserting data into these two tables through front end.
My doubt is why we have to create
Dim pk(1) As DataColumn
pk(0) = ds.Tables(0).Columns("authid")
ds.Tables("authors").PrimaryKey = pk
This is for primary key.
The below is for foreign key:
Dim fk As ForeignKeyConstraint
fk = New ForeignKeyConstraint("fk", ds.Tables(0).Columns("authid"),
ds.Tables(1).Columns("authid"))
fk.DeleteRule = Rule.Cascade
fk.UpdateRule = Rule.Cascade
ds.Tables(1).Constraints.Add(fk)
ds.EnforceConstraints = True
with out creating this constraints iam getting exception "sys... violated" when iam trying to insert duplicate values.
Then what is the use of above code and when the code will be useful.
Thanks in advance.
-- modified at 9:49 Friday 9th June, 2006
|
|
|
|
|
I am a little confused by your problem statement versus what your code is attempting to do - the problem statement makes no mention of "custid" and given the names of the tables you mention, Author and Article, I'm not sure where a customer would fit in the picture.
Nevertheless, I've put my interpretation of the tables you are working with below (please note that I didn't try very hard to get the PL/SQL statements correct, but they should get my idea across (and you may have different types associated with your column definitions, but the concepts should still work)
CREATE TABLE Author
(
AuthorID INTEGER
FirstName VARCHAR(32)
LastName VARCHAR(32)
)
ADD PRIMARY_KEY CONSTRAINT PK_Author ON Author.AuthorID
CREATE TABLE Article
(
ArticleID INTEGER
AuthorID INTEGER
ArticleName VARCHAR(128)
PublishDate DATETIME
)
ADD PRIMARY_KEY CONSTRAINT PK_Article ON Article.ArticleID
ADD FOREIGN_KEY CONSTRAINT FK_Author Article.AuthorID ON Author.AuthorID
(please rememeber the SQL above is probably wrong!)
If the tables are defined as I have assumed, then you are absolutely right and you should not be getting a primary key violation. But, I do wonder if you made Article.AuthorID the foreign key into the Author table AND the primary key in the Article table. If you did that, then you should indeed get a primary key violation when attempting to add records to the Article table.
Hope this helps a bit...
/dave
----------
If you always do what you always done, you'll always get what you've always got - Anonymous
|
|
|
|
|
I have a C#/ASP.NET application that goes and creates a connection to a SQL Server 2000 database. Within the application itself, I have a textbox in which the user enters a part number. After entering the part number, they click a command button that returns data in a datagrid depending on the part number.
//Declaration of PartNumber string
protected string strPartNumberInput;
//more code
//Set PartNumber variable to what user entered
strPartNumberInput += txtPartNumber.Text;
//Now I want to run SQL to get cost data. Basically want 'Select * from Costs where costs.PartID = Parts.ID'
string SQLString = "Select * FROM Costs"; //What else to put here??
How do I programatically say, 'strPartNumberInput is Parts.ID'
Thanks! Let me know if this doesn't make sense.
|
|
|
|
|
Well, looking at your code, the cmd:
strPartNumberInput += txtPartNumber.Text;
does not set a part number, it adds just more numbers. it should be:
strPartNumberInput = txtPartNumber.Text;
And how that I see it... you just need this SQL:
"Select * from Costs where Costs.PartID = Parts.ID And Costs.PartID = " + strPartNumberInput;
should work...
Good-Luck...
NaNg.
|
|
|
|
|
NaNg15241 wrote: "Select * from Costs where Costs.PartID = Parts.ID And Costs.PartID = " + strPartNumberInput;
Your solution is susceptable to a SQL Injection Attack. Please learn how to defend against these attacks as they could compromise your systems. For more information see SQL Injection Attacks and Tips on How to Prevent Them[^]
"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)
My: Website | Blog
|
|
|
|
|
Unless I'm missing something...
string SQLString = "Select * FROM Costs WHERE costs.PartID = " + strPartNumberInput;
|
|
|
|
|
Gerald Schwab wrote: string SQLString = "Select * FROM Costs WHERE costs.PartID = " + strPartNumberInput;
Your solution is susceptable to a SQL Injection Attack. Please learn how to defend against these attacks as they could compromise your systems. For more information see SQL Injection Attacks and Tips on How to Prevent Them[^]
"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)
My: Website | Blog
|
|
|
|
|
Uhh, he asked what he should concatenate to the end of the SQLString string variable in order to generate a SQL statement that would filter by Parts.ID. He didn't ask for a "solution". So, I didn't provide him with "my" solution, I simply demonstrated how to create the SQL string he was interested in creating. I am fully aware of SQL injection attacks and always use typed parameters with stored procedures in "my" solutions. Maybe you should try being less arrogant next time.
|
|
|
|
|
Gerald Schwab wrote: Uhh, he asked what he should concatenate to the end of the SQLString string variable in order to generate a SQL statement that would filter by Parts.ID
No, he didn't ask what he should concatenate on to the end of the string, he said:
string SQLString = "Select * FROM Costs"; //What else to put here??
How do I programatically say, 'strPartNumberInput is Parts.ID'
Gerald Schwab wrote: I simply demonstrated how to create the SQL string he was interested in creating. I am fully aware of SQL injection attacks and always use typed parameters with stored procedures in "my" solutions.
Then why give people answer that lead them down the road to a very well known yet easily correctable security flaw? I doesn't matter one jot if you use parameters in "your" solutions if you don't share the benefits with other people.
Gerald Schwab wrote: Maybe you should try being less arrogant next time.
I'm just trying to do my bit to ensure the world is a more secure place. I don't want my credit card details, or any other of my details, falling into the hands of criminal gangs because someone didn't know how to secure their system properly because someone was lazy in answering a question on a forum.
People have a tendancy to do "just enough" to get something working without really thinking about the security holes they are leaving open in the process. That could be because they don't know about them, or don't care. I can't help with the latter but I can do something about the former.
But, if you think that it is arrogant of me to try and help others secure their systems.....
"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)
My: Website | Blog
|
|
|
|
|
As i have understood from your description, you need this SQL query
String SQLString="SELECT * FROM Costs WHERE PartID= "+
"(SELECT ID FROM Parts "+
"WHERE PartNumber="+txtPartNumber.Text+")";
This query is right if your part number is not the part id which stored seprately in parts table along with part id. But if Part no. and part id are the same then you could use following query:
String SQLString="SELECT * FROM Costs WHERE PartID= "+txtPartNumber.Text;
I think it should work....
Wasif Ehsan
|
|
|
|
|
Wasif Ehsan wrote: String SQLString="SELECT * FROM Costs WHERE PartID= "+txtPartNumber.Text;
Your solution is susceptable to a SQL Injection Attack. Please learn how to defend against these attacks as they could compromise your systems. For more information see SQL Injection Attacks and Tips on How to Prevent Them[^]
"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)
My: Website | Blog
|
|
|
|
|
All of your replies, disappointingly, contain a major security flaw. You should never inject values into a SQL string when you can use a parameter instead. For more information see SQL Injection Attacks and Tips on How to Prevent Them[^]
You may want to re-write your code to resemble this:
string SQLString = "SELECT * FROM Costs WHERE Costs.PartID = @PartID";
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = SQLString;
cmd.Parameters.Add("@PartID", strPartNumberInput);
If Costs.PartID is an int column then you'll have to convert the strPartNumberInput into an integer first: Convert.ToInt32(strPaetNumberInput)
Does this help?
"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)
My: Website | Blog
|
|
|
|
|
I have not had a chance to read the article you referenced, but I am wondering why injecting values into the string is considered a security risk?
..big thanks to all who have replied to my question!
|
|
|
|
|
Because if you inject strings into the SQL, especially ones that come straight from the user interface, then an attacker can produce malformed SQL and gain access to your system. (Where do you live? I can come and do one of my SQL Injection Attack presentations in your town if you want a real live demonstration where I compromise a SQL Server into divulging the inner most secrets of the server it is running on. And I mean the whole server, not just the SQL Server process.*)
Lets say you have a simple bit of SQL like this:
cmd.CommandText = "SELECT * FROM Products where Name = '"+txtSearch.Text+"'";
What happens if the user types in the following?
'; DELETE FROM Products; --
The whole string becomes:
SELECT * FROM Products where Name = ''; DELETE FROM Products; --
That will return a dataset back to the application, which is what it expects, and then deletes all the products from the database. When the next customer comes to the website what is it going to show when there are no products in the database?
Okay - there may be some constraints on the table (foreign key constraints) that don't permit the rows to be deleted. How about something equally damaging to the company. Let's set their entire inventory to a penny!
The mallicious user then types:
'; UPDATE Products SET Price = 0.01; --
The word will quickly spread around the internet and the company will soon be out of business or have a huge number of very pissed off customers.
If you don't secure your system the possibilities for attack are endless.
* The demonstration is done on a server box that I own. Performing a SQL Injection Attack on a system without the permission of the system owner is a breach of the 1990 Misue of Computers Act and can carry a penalty of 5 years in jail.
"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)
My: Website | Blog
|
|
|
|
|
Unfortunately I live in the boring state of South Dakota in the United States which would be a bit far for you to travel....
This is for an intranet site that only other programmers will be accessing. Apparently the company does not trust other users to update product information. Therefore security right now is not a concern to my boss but I'd like to get the security set in anticipation that non-programmers could use the application.
I skimmed the article and read your post. Very interesting stuff. I hope everyone takes the time to read it.
Thanks again!
|
|
|
|
|
Colin--I entered the code you provided...
private void btnSearchPartNumber_Click(object sender, System.EventArgs e)
{
strPartNumberInput = txtPartNumber.Text;
Convert.ToInt32(strPartNumberInput);
string SQLString = "Select * FROM Costs where Costs.PartID = @PartID";
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = SQLString;
cmd.Parameters.Add = ("@PartID", strPartNumberInput);
//Call and build grid
BindGrid(strConnectSQL, SQLString, DataGrid1);
}
However, I am getting a compiling error where I bolded the strPartNumberInput. I'm getting 'expected ;' What am I missing here?
|
|
|
|
|
Add is a method, not a property.
Use this instead...
cmd.Parameters.Add("@PartID", strPartNumberInput);
|
|
|
|
|
Two changes:
the line:
Convert.ToInt32(strPartNumberInput);
becomes
int partNumber = Convert.ToInt32(strPartNumberInput);
And the line
cmd.Parameters.Add = ("@PartID", strPartNumberInput);
becomes
cmd.Parameters.Add("@PartID", partNumber);
leckey wrote: BindGrid(strConnectSQL, SQLString, DataGrid1);
I'm not sure what that does (I'm guessing it is a method you created), but you need to send the command, not the string (remember the string now contains a parameter and the SQL Server needs to know what that means, which is what the cmd.Parameters.Add(...) does) And the method will have to be updated to use the command rather than the string.
I'm also guessing that strConnectSQL is your connection string - If so then you'll have to replace my reference to myConnection with your connection string.
"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)
My: Website | Blog
|
|
|
|
|
Okay...here is all the code...
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebBasedPartsDB
{
///
/// Summary description for WebForm1.
///
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblPartNumber;
protected System.Web.UI.WebControls.Button btnSearchPartNumber;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox txtPartNumber;
protected string strPartNumberInput;
//Get the SQL connection string from the web.config file
public String strConnectSQL = (ConfigurationSettings.AppSettings["ConnectionString"]);
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.btnSearchPartNumber.Click += new System.EventHandler(this.btnSearchPartNumber_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSearchPartNumber_Click(object sender, System.EventArgs e)
{
strPartNumberInput = txtPartNumber.Text;
int partNumber = Convert.ToInt32(strPartNumberInput);
string SQLString = "Select * FROM Costs where Costs.PartID = @PartID";
SqlCommand cmd = new SqlCommand();
cmd.Connection = strConnectSQL;
cmd.CommandText = SQLString;
cmd.Parameters.Add ("@PartID", partNumber);
//Pass the command, not the string
BindGrid (strConnectSQL, SQLString, DataGrid1 );
//BindGrid(strConnectSQL, cmd, DataGrid1);
}
//**********************************************************
//BindData()
//**********************************************************
private void BindGrid (string DBconnectString, string sqlCommand, System.Web.UI.WebControls.DataGrid DGrid)
{
// create data connection
SqlConnection conn = new SqlConnection(DBconnectString);
// Call SP from db
SqlCommand command = new SqlCommand(sqlCommand, conn);
// create data adapter
SqlDataAdapter adapter = new SqlDataAdapter(command);
// create and fill dataset
DataSet ds = new DataSet();
adapter.Fill(ds);
// fill and bind data to Datagrid
DGrid.DataSource = ds;
DGrid.DataBind();
// Close Connection
conn.Close();
}
}
}
The only compile error I'm getting is 'Cannot implicitly convert type 'string' to 'System.Data.SqlClient.SqlConnection' where I highlighted....
Sorry, folks. I'm a project manager who has been thrown into programming. I'm sure this seems simple to you!
|
|
|
|
|
Connection expects to be assigned a SqlConnection object, not a string. Use this instead:
cmd.Connection = new SqlConnection( strConnectSQL );
Also, you should be sure to call Dispose on cmd at the end of the method, which releases the resources used by that command object.
Josh
|
|
|
|
|
I've read you can Close and Dispose. Is Dispose better?
|
|
|
|
|
As far as I know they do the same thing. Usually if a class implements both a Dispose and Close method, one of them just calls the other.
Josh
-- modified at 15:40 Wednesday 7th June, 2006
Oops, SqlCommand does not have a Close method. SqlConnection has a Close method. You should call Dispose on the SqlCommand.
|
|
|
|
|