Database Search C#.net

rmokkenstorm

Active member
Joined
Feb 20, 2006
Messages
31
I have a problem
Im busy writing a C#.NET project but walked into a problem.

Ive got a Form With 3 Textboxes a Button and a DataGrid.

If i typ a value into the textboxes and i press the button. The datagrid Shows me the correct data.


this.oracleSelectCommand1.CommandText = @"SELECT Collom1, Collom2, Collom3, Collom4, Collom5
FROM Database
WHERE (Collom1 = :PARAM2)
AND (Collom2 = :PARAM3)
AND (Collom3 = :PARAM4)";


But now i want it like This:
If i Typ a value for the first and last colom and not the second one.
I still want him te show me the data he found and ignore the Where at "PARAM3".
If i do this now he says that he havent got enough info to fill op my dataAdaptor.
Wich is logic.
But i realy dont have a clue how to do this.. can you help me?
 
Hey,

Cant you just do this.
If the textbox is empty than the value to search is by default % (wildcard).

Regards.

rmokkenstorm said:
I have a problem
Im busy writing a C#.NET project but walked into a problem.

Ive got a Form With 3 Textboxes a Button and a DataGrid.

If i typ a value into the textboxes and i press the button. The datagrid Shows me the correct data.


this.oracleSelectCommand1.CommandText = @"SELECT Collom1, Collom2, Collom3, Collom4, Collom5
FROM Database
WHERE (Collom1 = :PARAM2)
AND (Collom2 = :PARAM3)
AND (Collom3 = :PARAM4)";


But now i want it like This:
If i Typ a value for the first and last colom and not the second one.
I still want him te show me the data he found and ignore the Where at "PARAM3".
If i do this now he says that he havent got enough info to fill op my dataAdaptor.
Wich is logic.
But i realy dont have a clue how to do this.. can you help me?
 
TripleB said:
Hey,

Cant you just do this.
If the textbox is empty than the value to search is by default % (wildcard).

Regards.


If I type a % into my textboxes as Values in the running program. he gives a error that he also gives when i leave a textfield blank.

suppose the code would look like this:

if (txtBorgtochtNummer.Text == string.Empty)
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value = "%";
}
else
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value = txtBorgtochtNummer.Text;
}
Or am I wrong..
 
Last edited by a moderator:
What is the error that you get?
Maybe try to step trough the code to see if he does the check correctly

realized something, if you use wildcards that you have to you LIKE and not = in your SQL statement ... if I am not mistaking.

Greetz,

rmokkenstorm said:
If I type a % into my textboxes as Values in the running program. he gives a error that he also gives when i leave a textfield blank.

suppose the code would look like this:

if (txtBorgtochtNummer.Text == string.Empty)
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value = "%";
}
else
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value = txtBorgtochtNummer.Text;
}
Or am I wrong..
 
TripleB said:
What is the error that you get?
Maybe try to step trough the code to see if he does the check correctly

realized something, if you use wildcards that you have to you LIKE and not = in your SQL statement ... if I am not mistaking.

Greetz,

yes thats true .. its prety strange cause if i only execute the query in SQL plus. Theres no problem.. i quote them between and it works...
now my script. including the IF, ELSE.. works.. but I have Still have to Fill up all the fields.
If I dont he notice me that my OracleDataAdaptor1.Fill is not correct.. witch is pretty logic. cause he misses one parameter..

Conculusion: My If, Else is not correct or I do not place my Quotes Correct.
 
God im supid...
Left the fields on number..
No wonder it didnt work..
But... now they are on varchar it works almost like I want it.

if (":PARAM2" != string.Empty)
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value = txtBorgtochtNummer.Text;
}
else
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value = "%";
}

Why doesnt he put the "%" into his param.
I dont want to let the user fill in a %.. or set the textbox at "%" ..
why does the system not fill this in itself?
 
C#:
private void btnbutton1_Click(object sender, System.EventArgs e)
	{
			
if ((string)oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value == string.Empty)
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value = "%";
}		
else
{				
oracleDataAdapter1.SelectCommand.Parameters[":PARAM2"].Value = txtBorgtochtNummer.Text;
}

if ((string)oracleDataAdapter1.SelectCommand.Parameters[":PARAM3"].Value == string.Empty)
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM3"].Value = "%";
}		
else
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM3"].Value = txtBorgtocht2.Text;				
}

if ((string)oracleDataAdapter1.SelectCommand.Parameters[":PARAM4"].Value == string.Empty)
{
oracleDataAdapter1.SelectCommand.Parameters[":PARAM4"].Value = "%";
}		
else
{				
oracleDataAdapter1.SelectCommand.Parameters[":PARAM4"].Value = txtBorgtocht3.Text;
}

dataSet11.Clear();
oracleDataAdapter1.Fill(dataSet11);
			
}

Seems good doesnt it?
But ...
If I do not type any values into the textboxes I have to click twice at the submit button.
If I type al of the values into the textboxes I only have to click once.

If Fill in a single value in one textbox and press the button he doesnt show data. Then I type the second value in another textbox and pres the button.
He shows me the data from the previos value. The single value that is.
After that the program is some kind of bugged cause no matter what I type.
No data will be shown annylonger.
 
Last edited by a moderator:
Where are you actually binding the controls to the dataset? Where is the dataset declared? If you could post more of the relevant code it might help...

It looks as though you are doing the binding in the Page_Load event but binding against the dataset loaded on the previous button click.
Also are you sure you mean to check the parameters current value agains string.empty rather than the textbox contents agains string.empty.
In your code the first time it is executed each parameter is probably empty so you set it to %, on subsequent executions they will not be empty and so you then assign the textbox contents to the control regardless of the textboxs value.
 
PlausiblyDamp said:
Where are you actually binding the controls to the dataset? Where is the dataset declared? If you could post more of the relevant code it might help...

It looks as though you are doing the binding in the Page_Load event but binding against the dataset loaded on the previous button click.
Also are you sure you mean to check the parameters current value agains string.empty rather than the textbox contents agains string.empty.
In your code the first time it is executed each parameter is probably empty so you set it to %, on subsequent executions they will not be empty and so you then assign the textbox contents to the control regardless of the textboxs value.


private void InitializeComponent()
{
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
this.oracleDataAdapter1 = new System.Data.OracleClient.OracleDataAdapter();
this.oracleDeleteCommand1 = new System.Data.OracleClient.OracleCommand();
this.oracleConnection1 = new System.Data.OracleClient.OracleConnection();
this.oracleInsertCommand1 = new System.Data.OracleClient.OracleCommand();
this.oracleSelectCommand1 = new System.Data.OracleClient.OracleCommand();
this.oracleUpdateCommand1 = new System.Data.OracleClient.OracleCommand();
this.txtBorgtochtNummer = new System.Windows.Forms.TextBox();
this.txtBorgtocht2 = new System.Windows.Forms.TextBox();
this.txtBorgtocht3 = new System.Windows.Forms.TextBox();
this.btnbutton1 = new System.Windows.Forms.Button();
this.dgrdataGrid = new System.Windows.Forms.DataGrid();
this.dataSet11 = new HELLYEAH.DataSet1();
this.cmbcomboBox1 = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.dgrdataGrid)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// oracleDataAdapter1
//
this.oracleDataAdapter1.DeleteCommand = this.oracleDeleteCommand1;
this.oracleDataAdapter1.InsertCommand = this.oracleInsertCommand1;
this.oracleDataAdapter1.SelectCommand = this.oracleSelectCommand1;
this.oracleDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[]


The datasets are automaticly genererated by microsoft visual studio.net 2003
I did not do this on my onw.

After I click on the search button he puts the stuff in the dataset. nowhere else.

the datagrid src = set on the dataset

If i explaind well.
But...
I set the string.empty to the textboxes like you advised en now it works.
stupid thing in the past It was focused on the textbox dont know why I used the param.

Short theori (please rely if Im wrong):
Why the params didnt work:
If i Do not fill in a value in the textbox and i press on the button.
this is when hes gonna check the params. now he notice that the are empty and Fills them up whit the %.
But he already has filled the dataset with empty values not the "%".

Is this correct.
It think not cause if I do fill in all fields. he works right away.

someknow why it didnt work with param as empty.sting command and as textfield.text it does work?
 
Back
Top