Linking of database tables by pID - asp.net

I have problem linking 2 datatables using a pID. It isn't an automated number. The thing is, I have to save the pID number into the database when I select an option in a drop down list. But the options shown are course names (Text).
After selecting from the drop down list, input text into a textbox and then click on a button to save both the pID number and description into the database. In 1 database table, it has the course name and pID number. The 2nd database table has the pID number and description(Save info into here).
I'm only able the save the selectedindex, which isn't the pID number.
.aspx.vb Code
Dim OdbcCon As New System.Data.Odbc.OdbcConnection
Dim cmd As New OdbcCommand
OdbcCon.Open()
cmd.Connection = OdbcCon
cmd.CommandText = " insert into StdBookInfo(pID,Description) values ('" & DropDownListStudent.SelectedIndex & "','" & TextBox1.Text & "') "
cmd.ExecuteNonQuery()
Code for drop down list:
sqlcmd3 = "select Course_code + ' - ' + package from StudentPackage order by pID asc "
sqlcmd4 = "select pID from StudentPackage order by Course_code,package asc "
cmd3.Connection = OdbcCon
cmd3.CommandText = sqlcmd3
DropDownListStudent.Items.Add("")
reader3 = cmd3.ExecuteReader
While reader3.Read
DropDownListStudent.Items.Add(reader3.Item(0).ToString)
End While
DropDownListStudent.SelectedIndex = -1
reader3.Close()
cmd3.Dispose()
cmd4.Connection = OdbcCon
cmd4.CommandText = sqlcmd4
reader4 = cmd4.ExecuteReader
I = 0
While reader4.Read
pID(I) = reader4.Item(0)
I = I + 1
End While
reader4.Close()
cmd4.Dispose()
OdbcCon.Close()

You need to set the Text and Value properties of each item of DropDownList, then use SelectedValue instead SelectedIndex.
<asp:DropDownList id="DropDownListStudent" Runat="Server">
<asp:ListItem Text="Item 1" Value="1"/>
<asp:ListItem Text="Item 2" Value="2"/>
<asp:ListItem Text="Item 3" Value="3"/>
<asp:ListItem Text="Item 4" Value="4"/>
<asp:ListItem Text="Item 5" Value="5"/>
</asp:DropDownList>

Related

How to select/ check radio button based on the value from database?

I have radio button like below
<dx:ASPxRadioButtonList ID="radClaimType" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" ValueType="System.String" Border-BorderStyle="None">
<Items>
<dx:ListEditItem Text="Expenses" Value="1" />
<dx:ListEditItem Text="Travel" Value="2" />
</Items>
</dx:ASPxRadioButtonList>
How do I select/check the radio button based on the value that I have inserted in database ?
Here is my code behind
Dim dt As New Datatable
Dim strSelect As String = "SELECT claim_type FROM detail_table WHERE id = '30'"
Dim myconn As New clsConnection
Try
myconn.OpenConnection()
myconn.FillDataTable(dt, strSelect)
myconn.CloseConnection()
If dt.Rows.Count > 0 Then
If dt.Rows(0).Item("claim_type") = 1 Then
radClaimType.SelectedIndex = 1
ElseIf dt.Rows(0).Item("claim_type") = 2 Then
radClaimType.SelectedIndex = 2
End If
radClaimType.Enabled = False
End If
Catch ex As Exception
MsgBox("Error")
myconn.CloseConnection()
End Try
The result is like this which is incorrect because the claim_type value for id = '30' is 1 which is Expense. I have debug the code and the value for claim_type is indeed 1 but the radio button does not checked/selected to Expense. How do I correct this ?
You should tag what 3rd party control library you are using here.
Anyway, as a general rule, the asp.net RB-list can be set by "index", starting at 0, or you can set by value.
So, for your code, then this:
radClaimType.SelectedValue = dt.Rows(0).Item("claim_type")
You do NOT want to set by index, since the RB list could have ANY values, and
0 = first one
1 = 2nd one
etc. etc. etc.
So, index starts at 0
but, you want to set the RB by "value", and hence:
radClaimType.SelectedValue = dt.Rows(0).Item("claim_type")
You can check your documentaton for that control (I don't have it).
But, you don't need a "if/then" here. You can set the value of the RB, and it should then select/display the value corectly
so with this:
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal" AutoPostBack="true" >
<asp:ListItem Value="100">Expenses</asp:ListItem>
<asp:ListItem Value="200">Travel</asp:ListItem>
</asp:RadioButtonList>
Then I can set 0 (first) or 1 (second) using "indexing" like this:
RadioButtonList1.SelectedIndex = 0 ' would select the 100
RadioButtonList1.SelectedIndex = 1 ' would select the 200
Or, by value
RadioButtonList1.SelectedValue = 100 ' would select the first one

How do i output a parameterized query on a GridView on a Visual Studio VB .NET Web Application?

I'm trying to create a report form where it can perform a query search with user input parameters.
For the query i have 3 values in total two are mandatory and one is an optional. The mandatory values are H.Str_ID and H.Tran_ID. The optional value is H.Rgst_ID. All three values are taken from textboxes.
How do i declare those values to perform a parameterized query search on the GridView.
GridView Columns
<Columns>
<asp:BoundField DataField="Emp_ID" HeaderText="Emp_ID" SortExpression="Emp_ID" />
<asp:BoundField DataField="Cust_ID" HeaderText="Cust_ID" SortExpression="Cust_ID" />
<asp:BoundField DataField="Rgst_ID" HeaderText="Rgst_ID" SortExpression="Rgst_ID" />
<asp:BoundField DataField="TRAN_LN_NUM" HeaderText="TRAN_LN_NUM" SortExpression="TRAN_LN_NUM" />
<asp:BoundField DataField="DISC_CD" HeaderText="DISC_CD" SortExpression="DISC_CD" />
<asp:BoundField DataField="AUTH_EMP_ID" HeaderText="AUTH_EMP_ID" SortExpression="AUTH_EMP_ID" />
<asp:BoundField DataField="ORIG_PRC" HeaderText="ORIG_PRC" SortExpression="ORIG_PRC" />
<asp:BoundField DataField="DISC_AMT" HeaderText="DISC_AMT" SortExpression="DISC_AMT" />
<asp:BoundField DataField="DISC_PCT" HeaderText="DISC_PCT" SortExpression="DISC_PCT" />
<asp:BoundField DataField="GL_ACCT_ID" HeaderText="GL_ACCT_ID" SortExpression="GL_ACCT_ID" />
</Columns>
SQLDataSource Select Command
SelectCommand="SELECT H.Emp_ID, H.Cust_ID, H.Rgst_ID, D.TRAN_LN_NUM, D.DISC_CD, D.AUTH_EMP_ID, D.ORIG_PRC, D.DISC_AMT, D.DISC_PCT, D.GL_ACCT_ID
FROM twOLTP.dbo.Transaction_Header AS
H INNER JOIN twOLTP.dbo.LN_Detail AS L ON H.Str_ID = L.Str_ID AND H.Rgst_ID = L.Rgst_ID AND H.Tran_ID = L.Tran_ID
INNER JOIN twOLTP.dbo.LN_Discount AS D ON L.Str_ID = D.Str_ID AND L.Rgst_ID = D.Rgst_ID AND L.Tran_ID = D.Tran_ID AND L.Tran_LN_Num = D.Tran_LN_Num
WHERE(H.Str_ID = #Str_ID)
And (H.Tran_ID = #Tran_ID)
And ((H.Rgst_ID = #Rgst_ID) Or (#Rgst_ID Is NULL Or #Rgst_ID = ''))">
You can do this several ways. One is to build up the sql string on the fly for each text box, but THEN you have also code in a set of if/then for the parameters.
So, to tame this type of beast and problem? Write the sql query to test against BOTH the parameter and the actual condition you want.
So, write the query like this:
the 'bla bla first part, and then:
strSQL = += "WHERE " &
"( (#str_ID = '-1') or (H.Str_ID = #Str_ID) )" &
"AND ( (#tRAN_ID = '-1') OR (H.Tran_ID = #Tran_ID) )" &
"AND ( (#rGST_id = '-1') OR (H.Rgst_ID = #Rgst_ID) )"
So note how we check against the #parm value and NOT a table column.
so, lets assume the first 2 are optional for this example.
You thus now have:
Dim rstData As New DataTable
Dim strSQL As String = our above sql string
Dim cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.Test3))
With cmdSQL.Parameters
.Add("#Str_ID", SqlDbType.NVarChar).Value = IIf(txtStrbox.Text = "", "-1", txtStrbox.Text)
.Add("#Tran_ID", SqlDbType.NVarChar).Value = IIf(txtTransID.Text = "", "-1", txtTransID.Text)
.Add("#Hrgst_ID", SqlDbType.NVarChar).Value = IIf(txtgST.Text = "", "-1", txtGst.Text)
End With
Dim da As New SqlDataAdapter(cmdSQL)
da.Fill(rstData)
Me.GridView1.DataSource = rstData
Me.GridView1.DataBind()
Of course, if the data type is integer etc., then change the above, and use -1 as opposed to "-1".
The above thus saves a LOT of if then, and gets you off the hook having to build the sql string up.
The only down side is that you could in some cases have data that matches your "catch" all when in fact you don't want a match. But, I can't say this has been a problem for my use cases.
In your case then you don't need the first two iif() as per above. So, less code then even the above.
Since the fist 2 variables are required then the first 2 in where clause is fine:
WHERE(H.Str_ID = #Str_ID)
And (H.Tran_ID = #Tran_ID)
however 3rd is optional so maybe you can check its length then build a sql string specific for that parameter. See below but excuse my sntax as I am freely writing this without syntax checker
//string to store sql for parm
string sqlRgst = "";
//since there is a parm value construct sql string for parm
if(#Hrgst_ID.Trim().Length > 0)
{
sqlRgst = " And ((H.Rgst_ID = " + #Rgst_ID + ") Or ( " + #Rgst_ID + " Is NULL Or " + #Rgst_ID + " = '') "
}
//now set your sql and append your sql parm variable you constructed to it
SelectCommand="SELECT H.Emp_ID, H.Cust_ID, H.Rgst_ID, D.TRAN_LN_NUM, D.DISC_CD, D.AUTH_EMP_ID, D.ORIG_PRC, D.DISC_AMT, D.DISC_PCT, D.GL_ACCT_ID
FROM twOLTP.dbo.Transaction_Header AS
H INNER JOIN twOLTP.dbo.LN_Detail AS L ON H.Str_ID = L.Str_ID AND H.Rgst_ID = L.Rgst_ID AND H.Tran_ID = L.Tran_ID
INNER JOIN twOLTP.dbo.LN_Discount AS D ON L.Str_ID = D.Str_ID AND L.Rgst_ID = D.Rgst_ID AND L.Tran_ID = D.Tran_ID AND L.Tran_LN_Num = D.Tran_LN_Num
WHERE(H.Str_ID = #Str_ID)
And (H.Tran_ID = #Tran_ID) " + sqlRgst

Reapeater Control button click

When I click on the vote button the detail will insert but all the the page detail insert I want only perticular button click data should insert.
Code:
Protected Sub vote(sender As Object, e As EventArgs) {
//( lblName,lbl_id ,lbl_name,Label1 this is all reapeater field id )
Dim lblName As Label
Dim lbl_id As Label
Dim lbl_name As Label
Dim Label1 As Label
For Each item As RepeaterItem In rpt_path.Items
lblName = CType(item.FindControl("lblName"), Label)
lbl_id = CType(item.FindControl("lbl_id"), Label)
lbl_name = CType(item.FindControl("lbl_name"), Label)
Label1 = CType(item.FindControl("Label1"), Label)
con=("Connection String")
con.Open()
Dim command As New OleDbCommand("insert into CFC(C_E_No,Name,Class_id,Party_id)values(#C_E_No,#Name,#Class_id,#Party_id)", con)
command.Parameters.AddWithValue("(#C_E_No", CType(item.FindControl("lblName"), Label).Text)
command.Parameters.AddWithValue("#Name", CType(item.FindControl("lbl_id"), Label).Text)
command.Parameters.AddWithValue("#Class_id", CType(item.FindControl("lbl_name"), Label).Text)
command.Parameters.AddWithValue("#Party_id", CType(item.FindControl("Label1"), Label).Text)
command.ExecuteNonQuery()
con.Close()
Design
<asp:Label ID="lbl_iid" runat="server" Text="Label" Visible="false"></asp:Label>
</div>
<asp:Label ID="lbl_iname" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:Label ID="lbl_icid" runat="server" Text="Label" Visible="false"></asp:Label>
<asp:Label ID="lbl_ipid" runat="server" Text="Label" Visible="false"></asp:Label>
Coding on Vote Button Click
Protected Sub vote(sender As Object, e As EventArgs)
Dim str As String = CType(sender, Button).CommandArgument
Session("vid") = str
MsgBox(Session("vid"))
Connection String
cmd = New OleDbCommand("select * from candi where C_E_No='" & str.ToString() & "' ")
ds = New DataSet
cmd.Connection = con
con.Open()
da = New OleDbDataAdapter(cmd)
MsgBox(cmd.CommandText)
da.Fill(ds, "candi")
lbl_iid.Text = ds.Tables(0).Rows(0)(0)
lbl_iname.Text = ds.Tables(0).Rows(0).Item(1).ToString()
lbl_icid.Text = ds.Tables(0).Rows(0).Item(2).ToString()
lbl_ipid.Text = ds.Tables(0).Rows(0).Item(4).ToString()
con.Close()
Connection String
cmd = New OleDbCommand("insert into CFC(Stu_E_No,C_E_No,Name,Class_id,Party_id)values('" & Session("stdid") & "','" & lbl_iid.Text & "','" & lbl_iname.Text & "','" & lbl_icid.Text & "','" & lbl_ipid.Text & "') ")
MsgBox(cmd.CommandText)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MsgBox("Succes")

Pass NULL value to rdlc report Boolean parameter

I have radio button list. asp code:
Auditing: <asp:RadioButtonList ID="RBLAudit" runat="server" Font-Bold="true" RepeatDirection="Horizontal" RepeatLayout="Flow" TextAlign="Left">
<asp:ListItem Value="true" Text=" Audited" Selected="True"/>
<asp:ListItem Value="false" Text=" Not Audited "/>
<asp:ListItem Value="" Text="ALL"/>
</asp:RadioButtonList>
and I have RDLC Report with Boolean parameter as bellow:
I want to pass null value when select ALL option of radio button list.
I try this code behind
If (RBLAudit.SelectedValue <> "") Then
p9 = New ReportParameter("State", RBLAudit.SelectedValue)
ElseIf (RBLAudit.SelectedValue = "") Then
p9 = New ReportParameter("State", DBNull.Value.ToString())
End If
Dim RepParams() As ReportParameter = {p1, p2, p3, p4, p5, p6, p7, p8, p9}
but this not work when RBLAudit.SelectedValue = "" . The error is "The value provided for the report parameter 'State' is not valid for its type."
How to pass null value for this Boolean parameter?
finally it works after very much trying and failing...
If (RBLAudit.SelectedValue <> "") Then
p9 = New ReportParameter("State", RBLAudit.SelectedValue)
ElseIf (RBLAudit.SelectedValue = "") Then
p9 = New ReportParameter("State", New String() {Nothing})
End If
any other idea?

Gridview Hypelink values based on row value and column header identifier

I have a gridview in vb.net wich provides a performance value of a list of business names for the previuous week, current week and next week as follows:
LOB W-1 W W+1
--------------------
AMEX 10 15 30
PPR 11 12 14
REM 12 11 10
What I need is for this values to be a hyperlink that opens another window based on the business name and the week to provide further detail. For example, if you want further datail of the value 15 for AMEX and the current week (W) then by pressing the value 15 a new window will open with information for AMEX for the current week.
I have the gridview created but I can not figure out how to do the hyperlinks. Any ideas?
See below code.
<asp:DropDownList ID="DateSelection" runat="server" Height="21px" Width="134px" >
</asp:DropDownList>
<asp:Button ID="Button_Update" runat="server" Text="UPDATE" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
GridLines="None" ForeColor="#333333">
<Columns>
<asp:BoundField DataField="LOB" HeaderText="LOB" />
<asp:BoundField DataField="W-1" HeaderText="W-1" />
<asp:BoundField DataField="W" HeaderText="W" />
<asp:BoundField DataField="W+1" HeaderText="W+1" />
</Columns>
</asp:GridView>
vb code
Dim dv_Groups As New System.Data.DataView
Dim dt_Groups As New System.Data.DataTable
dv_Groups = Group.Select(DataSourceSelectArguments.Empty)
dt_Groups = dv_Groups.ToTable()
Dim dv_Main As New System.Data.DataView
Dim dt_Main As New System.Data.DataTable
dv_Main = SQL_Main.Select(DataSourceSelectArguments.Empty)
dt_Main = dv_Main.ToTable()
Dim dt_Report As New DataTable()
dt_Report.Columns.Add("LOB", Type.GetType("System.String"))
dt_Report.Columns.Add("W-1", Type.GetType("System.String"))
dt_Report.Columns.Add("W", Type.GetType("System.String"))
dt_Report.Columns.Add("W+1", Type.GetType("System.String"))
Dim FindRow() As DataRow
Dim SearchText As String
Dim DateS As Date
Dim DateWeek As String
DateS = DateSelection.SelectedItem.Text
Dateweek = Format(DatePart(DateInterval.WeekOfYear, DateS))
For i As Integer = 0 To dt_Groups.Rows.Count - 1
dt_Report.Rows.Add()
dt_Report.Rows(i)(0) = dt_Groups.Rows(i)(0)
SearchText = "LOB like '" & Trim(dt_Report.Rows(i)(0)) & "%' And DateWeek = '" & Dateweek - 1 & "' "
FindRow = dt_Main.Select(SearchText)
dt_Report.Rows(i)(1) = FindRow(0).Item("A_SVL").ToString
SearchText = "LOB like '" & Trim(dt_Report.Rows(i)(0)) & "%' And DateWeek = '" & Dateweek & "' "
FindRow = dt_Main.Select(SearchText)
dt_Report.Rows(i)(2) = FindRow(0).Item("A_SVL").ToString
SearchText = "LOB like '" & Trim(dt_Report.Rows(i)(0)) & "%' And DateWeek = '" & Dateweek + 1 & "' "
FindRow = dt_Main.Select(SearchText)
dt_Report.Rows(i)(3) = FindRow(0).Item("S_SVL").ToString
Next
GridView1.DataSource = dt_Report
GridView1.DataBind()
Many Thanks
You can use a HyperLinkField or TemplateField to add links to your gridview, as shown here:
https://stackoverflow.com/a/8859655/849182

Resources