|
Hi. I have a webform that has several dropboxes that are dynamically populated from a ms access database. Im fairly new to database design. In my drop boxes i have add a "All" Item. which will allow me to search the database for all records.
my problem is:
Drop box one is listing several industries. (independant)
Drop box two lists several uses ( which dynamically change based on the industry selected)
Drop box 3 lists categories (independant)
drop box 4 lists options based on the selected category
I want to be able to search my table for records that match all of the user selected criteria.
im trying to use parameters and I cant figure out the best way to do this... I know what ive done probably isn't the best way to do this... but this is an example of how im creating my query string.
Sub CreateQuery()
Dim SelectPhotos As OleDbCommand
Dim Photo As OleDbDataReader
Dim strSelect As String
Dim SelectedIndustry As String
Dim SelectedUse As String
Dim SelectedCategory As String
Dim SelectedOption As String
strSelect = "Select * From Catalog1 Where "
SelectedIndustry = dropIndustry.SelectedItem.Value
SelectedUse = dropUse.SelectedItem.Value
SelectedCategory = dropCategory.SelectedItem.Value
SelectedOption = dropOption.SelectedItem.Value
SelectPhotos = New OleDbCommand(strSelect, conPhoto)
If SelectedIndustry > 0 Then
SelectPhotos.Parameters.Add("@selectedIndustry", SelectedIndustry)
strSelect += "IndustryID=@SelectedIndustry And "
End If
If SelectedUse > 0 Then
SelectPhotos.Parameters.Add("@selectedUse", SelectedUse)
strSelect += "UseID=@SelectedUse And "
End If
If SelectedCategory > 0 Then
SelectPhotos.Parameters.Add("@selectedCategory ", SelectedCategory)
strSelect += "CategoryID=@SelectedCategory And "
End If
If SelectedOption > 0 Then
SelectPhotos.Parameters.Add("@selectedOption ", SelectedOption)
strSelect += "OptionID=@SelectedOption"
End If
conPhoto.Open()
Photo = SelectPhotos.ExecuteReader()
Photo.Close()
conPhoto.Close()
lblTest.Text = strSelect
End Sub
|
|
|
|
|
Hi
i have a oledbcommand with parameter:
"insert into T(a,b) values (@a,@b);"+"select @@IDENTITY as '" + "a" + "'";
.
.
.
add CommandParameters
.
.
.
then
.
.
.
int retValue = oleDbCommand.ExecuteScalar();
.
.
.
but i get error :
Characters found after end of SQL statement
What should i do
|
|
|
|
|
WDI wrote:
"insert into T(a,b) values (@a,@b);"+"select @@IDENTITY as '" + "a" + "'";
This is nuts. Why are you concatenating string literals like this? You could do all this in one literal string:
insert into T(a,b) values (@a,@b);select @@IDENTITY as 'a' And as you can see this is much easier to read. I suspect there is something more going on here because you are unlikely to have a table named T with columns name a and b. So, we cannot see what your proper string is, which is the cause of your problem, because you have chosen to obfuscate it. This does not help us help you. If you need to obfuscate parts of the code then do that to code that is incidental to the problem, not directly related to it.
Next, what is this as 'a' on the end. I'm guessing this will confuse the query parser because the apostrophe is for string literals in SQL, not for naming columns. Use [a] or "a" - better yet, because you are using ExecuteScalar, don't name the column at all.
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
hello dear,
I want to register the server from my local enteprize manager .bu this error apeared :
the server doesn't exist or access is denied
I called the manager of the server he said the server have 20 sites that working properly with out problem.and you should setting the control pannel of your local computer.what setting?how?
thanks in advanced.
|
|
|
|
|
If you have any adea please tell me?
thanks in advanced.
|
|
|
|
|
I want to take all the data from a column of a table. Foreach value i get, I used it as a field to my insert function into another table . I some kind like
for(int i=0;i
|
|
|
|
|
Yes, you could do it very easily like this:
string sql = "INSERT INTO Table2 VALUES (@value1, .....)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@value1", SqlDbType.Int);
foreach(DataRow row in table1.Rows)
{
cmd.Parameters[0].Value = row[1];
cmd.ExecuteNonQuery();
} It can be done with a stored procedure in the same way, only if you change the sql variable to the name of a stored procedure that takes one parameter named value1 .
I hope it helps.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I means that we'll do all that things in a single store procedure, not in C# (include the way we take each row from a table). And then, in C#, we just call that store procedure to execute automatic. Can you help me?
Thanks your reply.
|
|
|
|
|
Ah I see. You could try this:
INSERT INTO Table2 (valueFromTable, fixedColumn1, ...) SELECT column1, fixedValue1 ... FROM Table1 I'm no SQL expert, but if I remember correctly, I once used something like that to copy a table.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
it won't work until you don't provide a values statement into your insert
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Yes, it works. I've used the following statement in a stored procedure:
INSERT INTO Shutters(ID_pedido, Fabricado, Precio, Descripcion, Tipo, Madera, Color, Acabado)
(
SELECT @ID_pedido, 0, Precio, Descripcion, Tipo, Madera, Color, Acabado
FROM Cotizacion_Shutters
WHERE ID_pedido = @ID
)</code> and it works as expected, copying some records from one table to another with most columns the same, and changing only the values of one column (Fabricado , set to 0).
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Wow, it's too strange and really interesting . I've never seen it before (even in SQL Help). You're right, it really works... Thanks a lot!
|
|
|
|
|
Blue_Skye wrote:
You're right, it really works...
I know, I learned it here!
I'm glad to have helped you. Good luck!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I've just found the solution. The way i solve the problem is using cursor :
declare @VariableName datatype<br />
declare CursorName<br />
for<br />
select TableColumn from Table1<br />
open CursorName<br />
fetch next from CursorName into @VariableName<br />
while(@@FETCH_STATUS = 0)<br />
begin<br />
Insert into Table2 values(@VariableName,...,...)<br />
fetch next from CursorName into @VariableName<br />
end<br />
close CursorName<br />
deallocate CursorName
The example i used here take just one column but you can use as many columns as you like. This cursor remind me while(Reader.Read()) in C#
Thanks your reply. I'm very happy to talk with you
|
|
|
|
|
I'm glad you solved it, but cursors are very inefficient. I would recommend you at least try my idea, it should work much faster. See my response to toxcct above; the code I gave you works.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Hello,
When l got to update my database l get this error message "Missing the datacolumn 'DepartmentName' in the DataTable 'Employee' for the sourceColumn 'DepartmentName' ".
I have a database application. 2 tables. Department and Employee.
Department -> DepartmentCode (PK)
DepartmentName
Employee -> EmployeeID (PK)
Name
Surname
DepartmentCode (FK)
I have create this relationship in code using relationships in the form load event. see below:
<br />
private void frmEmployees_Load(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
cnn.Open();<br />
OleDbCommand cmd = cnn.CreateCommand();<br />
cmd.CommandType = CommandType.Text;<br />
cmd.CommandText = "SELECT * FROM Employee";<br />
da.SelectCommand = cmd;<br />
da.FillSchema(ds,SchemaType.Source,"Employee");<br />
da.Fill(ds,"Employee");<br />
<br />
cmd.CommandText = "SELECT * FROM Department";<br />
da.SelectCommand = cmd;<br />
da.FillSchema(ds,SchemaType.Source,"Department");<br />
da.Fill(ds,"Department");<br />
<br />
DataColumn parentColumn = ds.Tables["Department"].Columns["DepartmentCode"];<br />
DataColumn childColumn = ds.Tables["Employee"].Columns["DepartmentCode"];<br />
<br />
ds.Relations.Clear();<br />
drEmployees = new DataRelation("EmployeeDetails",parentColumn,childColumn);<br />
ds.Relations.Add(drEmployees);<br />
<br />
txtEmployeeID.Text = ds.Tables["Employee"].Rows[0]["EmployeeNumber"].ToString();<br />
txtFirstName.Text = ds.Tables["Employee"].Rows[0]["FirstName"].ToString();<br />
txtSurname.Text = ds.Tables["Employee"].Rows[0]["Surname"].ToString();<br />
txtDateOfBirth.Text = ds.Tables["Employee"].Rows[0]["DateOfBirth"].ToString();<br />
txtAddress1.Text = ds.Tables["Employee"].Rows[0]["Address1"].ToString();<br />
txtAddress2.Text = ds.Tables["Employee"].Rows[0]["Address2"].ToString();<br />
cboDepartment.Text = ds.Tables["Employee"].Rows[0]["DepartmentCode"].ToString();<br />
<br />
string departmentCode = ds.Tables["Employee"].Rows[0]["DepartmentCode"].ToString();<br />
DataRow[] departmentRow = ds.Tables["Department"].Select("DepartmentCode = '" + departmentCode + "' ");<br />
string departmentName = departmentRow[0]["DepartmentName"].ToString();<br />
<br />
<br />
this.UpdateDepartments();
} <br />
catch ( OleDbException ex )<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
catch ( Exception ex )<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
When l click the update button, l run this code and get an error on the update.
<br />
ds.Tables["Employee"].Rows[0]["FirstName"] = txtFirstName.Text;<br />
ds.Tables["Employee"].Rows[0]["Surname"] = txtSurname.Text;<br />
ds.Tables["Employee"].Rows[0]["DepartmentCode"] = departmentCode;<br />
<br />
da.Update(ds,"Employee");
I hope someone can help me with this question. very important.
Thanks in advance,
Steve
|
|
|
|
|
Hey guys, morning....
Got a prob thats kept me up for a little too long tonight,
I need to find a way (is it possible?) to have a variable query written in vb.net that will change based on a text box input. Yah, i could write an if/then statment to get the solution out the door, but that seems the long way around, plus it would be more time consuming as I have about 60 different possible 'variables'.
Basically I have 60 different machines that record money transactions, and I need to beable to search by either a textbox input or a dropdown list of the machines (named in order from 01-60) to pull the value amount currently in the machines.
Help!
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
wetrivrrat wrote:
I need to find a way (is it possible?) to have a variable query written in vb.net that will change based on a text box input.
Yes, of course.
myCommand.CommandText = "SELECT someColumns From someTable WHERE machineName = @machineName";
myCommand.Parameters.Add("@machineName", someMachineName);
Assumes that your command object is called myCommand.
The variable someMachine contains the name of the machine that you retrieved out of your user interface.
Does this help?
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
in your code i'm assuming you're writing it in a command function? right, if
so then i'm only limited to 'GetType' unless i add the 'as string' or
someother class. so I guess my first Q would be, what class are you
expecting this to go under? if it helps any I was just using a
OleDbDataAdapter and DataSet to connect to sql(so if i had to use access later its already ready for it!).
Second Q was, is my interpretation of your code correct?
Function LoadLastInvoice()
Dim VAMachineName As String
VAMachineName = TextBox3.Text
LoadLastInvoice.CommandText = "SELECT last_invoice From Location_table WHERE
Location_ID = @machineName"
LoadLastInvoice.Parameters.Add("@machineName", VAMachineName)
End Function
Because I'm getting the following crash-related error...
'Object variable or With block variable not set'
and I'm not sure how to set the @machineName variable as I tried several ways and nothing seemed to work...
Thanks
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
hello my dear,
want to register the sql database from my local computer using enteprize manager but this error apeared :
sql server does not exist or access is denied.
i am sure about the correction of the user name and password and server name.my local windows is xp2000 but the windows of the server is 20003pro.the version of the sql in my local is personal no enteprize.
thanks in advanced
|
|
|
|
|
Anonymous wrote:
i am sure about the ... server name.
OK on your local intranet, otherwise you will need to use an IP address. Could the problem be as simple as that?
cheers
Phil
|
|
|
|
|
thank you,
I used the ip but did'nt get advantage.
|
|
|
|
|
can i bind a parameter to a column of a datagrid, or it can only be binded in table's columns?
I have the aggregated values of total sales for each year(from sh schema) in a datagrid, and i want to retrieve all the detail tuples from a year which user will 'click'.
i use the following query to do this:
select decode(prod_subcategory,null,'ALL',prod_subcategory) as subcateg,decode(prod_category,null,'ALL',prod_category) as categ,sum(amount_sold) as dolars
from sh.products,sh.sales,sh.times
where products.prod_id=sales.prod_id and times.time_id=sales.time_id and end_of_cal_year=:param1
group by rollup(prod_category,prod_subcategory)
where param1 it's an OracleParameter with parameter->set_SourceColumn("END_OF_CAL_YEAR") and i get an
ORA-01008 error( not all variables binded )
any help?
thanks in advance
|
|
|
|
|
A big problem is a Datetime object in the CommandText property. I always get an error. Is .NET unable to convert Datetime object into the right SQL Server system format? When it's possible ... how to solve it?
|
|
|
|
|
Seraphin wrote:
A big problem is a Datetime object in the CommandText property.
Why are you putting a DateTime in the CommandText property?
Seraphin wrote:
Is .NET unable to convert Datetime object into the right SQL Server system format?
.NET is perfectly capbable of converting a DateTime object to what ever format you wish in multiple calendars (should you so wish it).
Seraphin wrote:
how to solve it?
If you need to use a DataTime (or anything else for that matter) in a query you should use parameters. This reduces the possibility of security holes.
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM MyTable WHERE SomeDate > @theDate";
cmd.Parameters.Add("@theDate", myDateTimeObject);
Does this help?
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|