Ok I don't know if my question makes sense but I'll try to describe this as best as I can.
I am doing a school project and I have a database which stores "Projects", fields include ProjectID, Name, Publications, Status and so on.
I also have another table called Publications, which stores ID, PublicationType, ProjectID, Description and so on.
On the project page, I have the option to add a new project, and it also shows a grid view of the Current projects, and if you click on their name, it takes you to a page where you can add Publications for that project. I pass the Value of ProjectID through a HyperLinkField with the value Publications.aspx?ProjectID={0}
The ProjectID appears in the URL when I'm redirected to the Publications page, however, when I try to insert My publications, Name, Type and ProjectID the ProjectID field is left empty in my database.
I'm doing this in design view but here are my Insert and Select statements for the Publications page.
SELECT ProjectID, PubType, PubDescription, PubDetail, PubLink FROM Publications WHERE (ProjectID = ProjectID)
INSERT INTO Publications(ProjectID, PubType, PubDescription, PubDetail, PubLink) VALUES (ProjectID, ?, ?, ?, ?)
I'm 100% sure this is a logical error, but I'm not sure how to fix it. I would appreciate some help, preferably through the design interface, but I don't mind doing some of the coding if it's required.
Thanks !~
Try something like this
SelectCommand="ProjectID, PubType, PubDescription, PubDetail, PubLink FROM Publications WHERE ProjectID = #prodID"
And the add a select parameter
<SelectParameters>
<asp:QueryStringParameter Name="prodID" QueryStringField="ProjectID " />
</SelectParameters>
Related
I am creating a view like this:
Let's say I originally have this:
select * from mydb.mytable
mydb.mytable has a field called FirstName, but I want to transform its value in the select statement. Conceptually, I want to do this:
select *, upper(firstname) firstname from mydb.mytable
The problem is that * is already returning FirstName, so adding another column of the same name to the select breaks the SQL. To get it to work, I have to list each field like this:
select upper(firstname) firstname, lastname, city, state, zip
This is just one example, but the table I really want to use this with has 30+ columns. I don't like the idea of having to list out each column because adding a new field to the table means I have to modify the SQL (ordinal field position doesn't matter).
Well, that's the way SQL is designed, it's not a specific Teradata problem.
You want something like "select * but firstname" and no DBMS has implemented such a syntax.
Btw, one of (my) basic SQL rules is: never write "SELECT *" :-)
As dnoeth says, that's just how SQL works. Also, I'd reinforce his comment about never using select *, especially in a view.
To address concerns like this, I keep the table and view DDL together in code. Whenever you change the table definition, you change the view definition at the same time. That way, whenever you add or remove columns from your table (your stated concern), your view always remains current.
Using AccessDataSource and ListView when I hit del in browser following error is shown
OleDbException (0x80004005)
: The record cannot be deleted or changed because table 'tblOrders' includes related records.
I am trying to solve the problem through a couple of ways which are as follows
DeleteCommand="DELETE FROM [tblCustomers] , [tblOrders] WHERE [pkeyCustomerID] = ? "
The Default command generated by ASP is
DeleteCommand="DELETE FROM [tblCustomers] WHERE (([pkeyCustomerID] = ?) OR ([pkeyCustomerID] IS NULL AND ? IS NULL)) "
Or
I removed <asp:Parameter Name="pkeyCustomerID" Type="String" /> from <DeleteParameters>
and replace it with the parameters of selected table
so the foreign key issue isnt affected
tag now there are no errors but the record isnt deleted either
How do i get around this?
Your first query:
DELETE FROM [tblCustomers] , [tblOrders] WHERE [pkeyCustomerID] = ?
Is not valid syntax, you cannot delete from two tables simulatenously, you would need something like
DELETE FROM [tblOrders] WHERE [pkeyCustomerID] = ?;
DELETE FROM [tblCustomers] WHERE [pkeyCustomerID] = ?;
However I don't think Access supports multiple statements. The best solution would be to edit this relationship to use the cascade delete referential action trigger.
To do this go into the relationships in Access:
Then double click on the relationship between tblOrders and tblCustomers which will bring up the properties. Then either uncheck "Enforce Referential Integrity", or ensure that the cascade referential action options are checked (ignore the field names, it was the first relation I came across in a test database I have):
This will ensure that when you delete a customer, you delete all related orders too.
i building a mini forum site.. and i constructed a few tables.
1) Users
2) Threads
3) Comments
4) Topics
i build a function that would insert a comment each time a user would submit a comment:
string saveComment = "INSERT INTO Comments(";
saveComment += " UsersID, ThreadsID, Date, Comments, CommentResponse";
saveComment += "Values('" + "','";// no idea what to insert in the UsersID
saveComment += "" + "','";// no idea what to insert in the ThreadsID
saveComment += DateTime.Now + "','";
saveComment += CommenttxtBox.Text + "','";
saveComment += commentResponseString + "')";
As you can see the fields have UsersID and ThreadID, both connected by a foreign key to the comments table.
Now, each time the user submits a comment, i guess i need to insert also to the UsersID field (which is an int in the comments table, and that field increases incrementally by 1 in the Users table). How can i insert a comment, and notify the other table not to increase the UserID by 1. in fact i want it the UserID to stay the same for each user submitting a comment..
How do i do that? i need to insert to a few fields in one table (comments) but keep the other tables informed that it is actually the same user who submitted the comment .
Note: i dont know any vb, only c#, and i use visual studio 2010. asp.net
BTW, the way you are inserting is a security issue, you could get SQL injection ...
Use the system.data.sqlclient.sqlparameters to passe values.
You are creating a very standard normalised structure. Your Users table will be responsible for controlling the UserID values that are generated.
You have two situations to cope with when inserting new comments:
The User exists and is logged in.
The User does not exist and is anonymous.
In the first situation, when you are inserting the comments you will not need to bother looking at the Users table. This assumes you have the UserID already loaded (as the user is logged in).
In the second situation, you will first need to a new row to the Users table and return the UserID that the table generates (assuming you are using an identity column). You can then pass this value to the Comments table.
The following script is an example of addressing the second situation:
DECLARE #userId int
INSERT INTO Users (Username, FirstName)
VALUES ('adamh', 'Adam')
SET #userId = SCOPE_IDENTITY()
INSERT INTO Comments(UserId, ThreadId, Comment)
VALUES (#userId, 1, 'My comment')
If you want to continue with your current coding style, simply concatenate the values into the relevant parts of the string.
However, with such as neatly defined structure as the one you have, I'd advise using something like Entity Framework 4.0, or LINQ to SQL, which cuts a lot of plumbing out once you have defined your structures.
I am looking for some advice on localization. I have an app that has been localized in the usual fashion (i.e., .resx files), which handles about 95% of the strings. However, I still need to localize some strings for category names that are stored in the database.
I'd like to avoid adding 15 new columns named categoryname_ES, categoryname_FR, etc, and then pulling the right column dynamically. If there would be some way to pull the data, and then do a substitution in the code, I think that would be a little less messy. Maybe along the lines of:
go through gridview row by row
if the language selected isn't english, look for this text value in a global resources file and replace it.
Anyone have a good idea of how to accomplish this? Or is adding a lot of categoryname columns for each language just the way to go (ewww).
I use the resource file method that you describe.
Add a CategoryName.resx file with a row for each category. Make sure the "Name" matches your database value exactly. Put the translation in the "Value".
Get the string in code via the resx's generated code file. (sorry for the C#)
Resources.CategoryName.ResourceManager.GetString(categoryName, new CultureInfo("fr"));
If you're binding to custom class, just make another property and bind to that property instead.
If you're binding to a DataSet, you might want to use the RowDataBound event to do the substitution.
You can do it with four database columns:
ID (unique primary key)
CultureCode
ProductID
ProductName
I presume that you are selecting your products from the database. Pass in the UI culture as part of the stored proc, then when you select from your product table join to the Culture table on the product ID and UICulture. You would phrase your SELECT something like this:
SELECT ProductID
,another field
,IsNull(Culture.ProductName, Product.ProductName)
,etc
FROM Product
LEFT JOIN Culture
ON Product.ProductID = Culture.ProductID
AND Culture.CultureCode = #UICulture
you get the idea. You could even check the UICulture for a hyphen (eg: fr-CA), split it into another variable, then do two joins to the Culture table - one for the exact culture, and one for the fallback culture, so in this example the first join would be for fr-CA and the second join would just fallback to fr. If all your culture joins fail (eg. because you don't have Zulu in the culture table), then the IsNull uses just the ordinary ProductName (which is probably in english).
You could create three tables:
Category
Category_ID (identity)
Category_Name (string)
Language
Language_ID (identity)
Language_Name (string)
CategoryLanguage
Category_ID (FK)
Language_ID (FK)
Translation (string)
The category table would have the names in English. The language table would have the languages that your application supports. The association table would then be able to provide the translated category name based on the selected language, if it exists. If it doesn't exist, then simply display the English version as the default.
I have been given a small project by the company I have applied for. They want a small application using asp.net GridView, FormView and an ObjectDataSource with a DataSet (xsd) file. I have been doing 3-tier applications but in a different way, I have my DataAccess layer as a c sharp file with methods accepting params and returning datatables etc. My business layer is another class with static methods returning business objects and collections making use of the DAL class. Now this ObjectDataSource is sth i did not really like, its generating some code that i can't even see where?
I could make the application work upto some point(90%). The other 10% is what my question about. I need to make a search by name functionality. There are two ObjectDataSources 1 and 2. ObjectDatasource1 just gets every record from the table on the first load. When search button cliked I set the datasource of gridview to the second ObjectDataSource which has a method called GetDataByNameSearch that is supposed to accept a parameter (all defined by wizzzardz) and parameter source is control (TextBox.Text). While my FormView works fine where it gets its parameter from QueryString, this search returns nothing. Search statement is as follows:
SELECT Birthday, CreatedAt, ID, Name, Surname
FROM Users
WHERE (Name LIKE '%#name%') OR
(Surname LIKE '%#name%')
Any idea about how these ObjectDataSources are supposed to be used, and make life easier(!)
Without code samples its hard to tell, but I did notice that your use of SQL parameters is a bit unusual.
You have:
SELECT Birthday, CreatedAt, ID, Name, Surname
FROM Users
WHERE (Name LIKE '%#name%') OR (Surname LIKE '%#name%')
I'm not sure if SQL '#' parameters will work when there are speechmarks around them. I think the above example will just result in the literal string '%#name%' being used in the query, which is why you might be getting no results.
SQL Parameters are usually used like this:
SELECT Birthday, CreatedAt, ID, Name, Surname
FROM Users
WHERE (Name LIKE #name) OR (Surname LIKE #name)
... but of course then you will lose the '%' wildcards. You might be able to add them directly into the parameter strings before they are passed to the query. If that is not possible, maybe try this version:
SELECT Birthday, CreatedAt, ID, Name, Surname
FROM Users
WHERE (Name LIKE '%' + #name + '%') OR (Surname LIKE '%' + #name + '%')