i am e newbie programmer and i want to improve a "student program" for schools. In the program i want to add lesson information. For example:
Student X
Lesson name 1st exam 2nd exam 3rdexam
mathematics 80 70 80
history 70 70 70
...
I have a database that called KFS with three tables.
Identity
RecordID | firstname | lastname | address | city
Lesson
LessonID | name | description
Lessondetail
DataID | RecordID | LessonID | lessonname | firstpoint | secondpoint | thirdpoint
I can already show list of students in a GridView, but I also want to show the selected student's points.
Is my database enough for this application? If yes, how can i design that relationship and show in gridview? Or should i modify my database?
My first thought when looking at your database design (and it could do with editing to make more clear) is: Will a LessonDetail always have 3 exams? Or could it possibly have more at some time in the future?
I would be tempted to break it up into something like this:
LessonDetail(DetailID, DataID, RecordID, LessonID, LessonName)
LessonPoint(DetailID, Point)
That way a Lesson can have as many exam points as you need.
I'll second Dan Diplo's advice about normalizing LessonDetail if it's not certain that you'll always have 3 exams. Unfortunately, that requires a pivot to get your data in the format you want - but that's another question. Also, you have LessonName in LessonDetail - but it should be pulled from Lesson instead.
The ASP.NET side is fairly easy - it's just a standard Master/Details view:
<asp:GridView DataSourceId="dsStudents" runat="server" DataKeyNames="RecordId" />
<asp:DetailsView DataSourceId="dsLessons" runat="server" />
<asp:SqlDataSource ID="dsStudents" runat="server" SelectCommandText=
"SELECT RecordID, firstname, lastname, address, city
FROM Identity
ORDER BY RecordID"
/>
<asp:SqlDataSource ID="dsLessons" runat="server" SelectCommandText=
"SELECT D.LessonID, L.Name, FirstPoint, SecondPoint, ThirdPoint
FROM LessonDetail as D
JOIN Lesson as L ON
D.LessonID = L.LessonID
WHERE D.RecordID = #r
ORDER BY D.LessonID"
>
<SelectParameters>
<asp:ControlParameter Name="r" ControlID="gvStudents"
PropertyName="SelectedDataKey" />
</SelectParameters>
</asp:SqlDataSource>
Related
I have a grid view, which displays the monthly items purchased by our registerd dealers. On button click event, all the items displayed in the grid view row wise should be made purchased by the dealer on one single click. This is the case of bulk update.
grid view code::
<asp:GridView ID="GridCustomer" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="PURCHASED_MONTH" HeaderText="MONTH" ></asp:BoundField>
<asp:BoundField DataField="ITEM_NAME" HeaderText="ITEM NAME"></asp:BoundField>
<asp:BoundField DataField="PAYABLE_AMOUNT" HeaderText="PAYABLE AMOUNT"></asp:BoundField>
</Columns>
</asp:GridView>
The number of rows is dynamic, sometimes single row sometimes multiple.
The table data ::
ID MONTH ITEM_ID STATUS PAYABLE_AMOUNT
1 4 155 1
2 5 159 1
3 6 256 1
In the UPDATE statement the WHERE CLAUSE will be ID. According to cases, I will have single ID or multiple IDs. For example,
update tbl_wholesome_purchase set STATUS = 2 WHERE ID = 1 but in some case ID = 1, 2, 3... also.
How can I create such update case stored procedure in which the WHERE CLAUSE parameter is dynamic ?
Are you using SQL Server 2016 or newer ?.
In that case you can use the string_split function to get a very easy solution, passing the IDs as a single comma-separated string.
DECLARE #ids varchar(max) = '4,7,9,25'
UPDATE tbl_wholesome_purchase SET status = 2
WHERE id IN (SELECT value FROM split_string(#ids, ','))
Another solution is to pass the ids already as a list, on table valued parameters. You will need to follow the documentation of ASP.NET about how to call them on dot net.
CREATE TYPE ListIDs AS TABLE ( id int );
GO
CREATE PROCEDURE dbo. update_wholesome_purchase
#ids ListIDs READONLY
AS
BEGIN
UPDATE tbl_wholesome_purchase SET status = 2
WHERE id IN (SELECT id FROM #ids);
END
GO
I have Ref table and patientDetails in sql. for example in Ref I have Gender column with values (Female, Male etc), In patientDetails the Gender column is 24 and I am binding the data to patientDetails and I have 24 in the Gender column in the Gridview but it should be Female. ( Foreign key and navigation properties would not work because I have so many different rows in Ref table like ethnicity,religion etc). I was told RowDataBound will work so I have this method now but its not going past the If statement line..
More Details:
my Data Source is binding to PatientDetails table which has Gender column ( with number values - which is getting it from ref table). I mean I have Ref table ( Like a lookup for everything - with columns ID, name, Description) In name I have gender, Ethnic group, religion) In Description Column I have the values for instance for gender: male, female etc however in the Gender column in Patient is returning the corresponding Ref ID of the value - for female it will store 24 in Gender column in Patient) so my Gridview is bounding correctly and getting whats stored in PatientDetails table however having 24, 25 in Gender is not helpful to show so I was told RowDataBound Can bring the correct correponding value in Ref table
Protected Sub gvPatientDetails_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblGender As Label = e.Row.FindControl("lblGender)
End If
End Sub
<asp:GridView ID="gvPatientDetails" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvPatientDetails_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="lblGender" runat="server" Text='<% #Eval("lblGender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
OK, I got it working.
For all the beginners who might struggle with this, the above code is working as it should, when I debug it first time it just hit the IF statement then doesnt go inside ( because the first one is Header (I know duh!!) hit continue and it should give you the actual value of label then after that it should be simple. Just getDataFromRef ( a method to get data from Ref - id = lblGender ) then
lblGender.Text = ( The value from method - getDataFromRef ).
Basically I wasn't aware the debugger will start with the Header then when you hit continue will go through the gridview rows!
All the best all, happy coding!:)
SQL Server: I have 3 columns [ID], [Text], [Value]
ASPX page:
I have a dopdownlist populated by a SqlDataSource:
<asp:DropDownList ID="ddl" runat="Server" DataSourceID="sql" DataValueField="ID" DataTextField="Text" />
<asp:SqlDataSource ID="sql" runat="Server" SelectCommand="sp" SelectCommandType="StoredProcedure" />
Using DataValueField and DataTextField I can capture 2 of the values in the SQL Server table based on selected value.
Question:
How to capture the additional column [Value] based on the users #ddl selected value?
Notes:
I can achieve this using javascript to populate a matching dropdown list using the [Value] field instead of the [ID] field and match on [Text] field.
There must be a more efficient and appropriate way to handle this with just asp or VB.
ANSWER USED
I simply got the value at time of use using the ddl [ID] and a sql select statement.
SELECT #Value = [Value] FROM [tbl] WHERE [ID] = #ID
Passing in #ID as the dropdownlist selectedvalue.
I'm guessing the ID is a unique identifier of the row, so why would you need the text or value columns to be submitted by the form? You can just retrieve these from the database using the ID that is submitted. Or am I missing something here?
There are three tables -
1) Student - My fact Table (References Addresses with FK ADDRESS_ID
2) Addresses - This table Contains FK COUNTRY_ID references COUNTRY
3) Country - this has a NAME COLUMN which i would display. (PK IDENTIFIER)
I have written this but Not Sure if it is Correct
Basically i want to join Student (FACT TABLE) to COUNTRY
Consosts of Fact --- Def .. then this
<Dimension foreignKey="ADDRESS_ID" name="COUNTRY">
<Hierarchy name="COUNTRY NAME" hasAll="true" primaryKey="IDENTIFIER" primaryKeyTable="ADDRESSES">
<Join leftKey="IDENTIFIER" rightKey="IDENTIFIER">
<Table name="ADDRESSES" >
</Table>
<Table name="COUNTRIES" >
</Table>
</Join>
<Level name="Country Name" visible="true" table="COUNTRIES" column="NAME" nameColumn="NAME" uniqueMembers="false">
</Level>
</Hierarchy>
</Dimension>
You haven't made the join correctly. I'm going to assume your relational schema looks like this:
.-STUDENT-------.
| IDENTIFIER PK | .-ADDRESSES-----.
| ADDRESS_ID FK |----| IDENTIFIER PK | .-COUNTRIES-----.
| COUNTRY_ID FK |----| IDENTIFIER PK |
| NAME |
The JOIN element needs to make a join between the lowest-granularity dimension table ADDRESS and the next level up (COUNTRY). The keys that join these tables are COUNTRY_ID (for ADDRESS) and IDENTIFIER (COUNTRY). leftKey then needs to be set to COUNTRY_ID.
Also, you're using the Level's nameColumn attribute, but this is actually used to set the level's own name, not the member names. I would remove this. In all you'd en up with something looking like this:
<Dimension foreignKey="ADDRESS_ID" name="COUNTRY">
<Hierarchy name="COUNTRY NAME" hasAll="true" primaryKey="IDENTIFIER" primaryKeyTable="ADDRESSES">
<Join leftKey="COUNTRY_ID" rightKey="IDENTIFIER">
<Table name="ADDRESSES" >
</Table>
<Table name="COUNTRIES" >
</Table>
</Join>
<Level name="Country Name" visible="true" table="COUNTRIES" column="NAME">
</Level>
</Hierarchy>
</Dimension>
student code| student.Name |Region Name|Location Name| Assets Details |
I have to display asset details on the basis of each student. There can be more than one asset details for one student, how can i use this in repeater or gridview.
Table which i have in SQL.
Student Code | student name | Asset Details
1 Ray laptop
1 Ray Bed
2 Raj Phone
2 Raj charger\
....
I want to display asset details for each unique student code like this on my page.
Student Code | student name | Asset Details
1 Ray laptop
Bed
2 Raj Phone
Charger
How do i perform the same with only one hit from sql.?
You can do the following:
Use a stored procedure to return two results set for the following queries
SELECT DISTINCT StudentCode, studentname FROM StudentInfo
SELECT StudentCode, student name, Asset Details
Create a DataSet for the results with 2 tables. You can name the first table "Student" and the second table "StudentInfo"
Create a relationship for the tables (please use the intellisense for syntax)
DataSet.Relationships.Add("RelationshipName",Table1.StudentCode,Table2.StudentCode)
Bind the GridView with the DataSet
Create a Template column for Asset Details and insert a repeater or Gridview for that column.
On the ItemDataBound event of the GridView.
DataRowView drv = (DataRowView)e.ItemData;
Repeater.DataSource = dv.CreateChildView();
Instead of using two repeater control, try to make a sql query to get data in this format
use such query
select studentID,
stuff((select ',' + AssetDetails
from Tbl_Student t2
where t2.studentID= t1.studentID
order by studentID
for xml path('')),1,1,'') as AssetDetailsString
from Tbl_Student t1
group by studentID
this will return single row for every studentID with Assestdetails in CSV format in same row