I am trying to populate a series of dropdown lists in a GridView. In the RowDataBound event, I call based on other info in the gridviewrow to get the items for populating dropdowns in the row. I am getting "ORA-01036: illegal variable name/number error on parameter" on the following code.
Dim rowTemp As GridViewRow = ddlItemDesc.Parent.Parent
Dim lblYear As Label = rowTemp.FindControl("lblSchoolYear")
Dim strYear As String = lblYear.Text.Trim()
Dim strSelect As String = "SELECT FOO.BAR.PRODUCT_CODE || ' :: ' || FOO.BAR.PRODUCT_DESC AS txt, FOO.BAR.PRODUCT_KEY AS val, :currentYear AS CURRENT_YEAR FROM FOO.BAR WHERE (FOO.BAR.PRODUCT_KEY=:itemKey)"
Dim daTemp As New OracleDataAdapter(strSelect, oConn)
daTemp.SelectCommand.Parameters.Add("currentYear", CDate(strYear))
daTemp.SelectCommand.Parameters.Add("itemKey", strItemVal(0))
Dim dtProd As New DataTable
daTemp.Fill(dtProd)
If I remove ":currentYear AS CURRENT_YEAR" from the SELECT statement, it runs fine. I use the same statement in another SELECT from a join of other tables with no problem.
The strYear comes from a label in the gridviewrow and I have checked that it has data and that it is valid for CDate (I've also tried it simply as a string with the same results). StrItemVal(0) comes from another dropdownlist (the one that is successfully populated using :currentYear being passed in). This code only gets called if that dropdown list has a value in it that is selected.
Related
I'm not sure if what I'm trying to do here is possible. I've got a Sheridan SSDB Grid, which is bound to a data control. When I populate the data control, the grid gets filled.
However, I've had to manually add an additional column after populating the grid to display a value which isn't in a database table.
To do all of this, I've written this code:
Dim SQL As String
SQL = My_Query
dtaEmployees.DatabaseName = DB_Period_Name$
dtaEmployees.RecordSource = SQL
dtaEmployees.Refresh
dtaEmployees.Recordset.MoveFirst
grdEmployees.Redraw = True
grdEmployees.Columns.Add (4)
I'm not sure how I can fill this new column in, however. I've got a global variable storing the value that I need, but none of the following lines of code are working
grdEmployees.Columns(4).Value = My_Variable
grdEmployees.Columns(4).Text = My_Variable
How can I set the value for all of the rows in the grid?
EDIT
After following the suggestion in the comments, I've modified my code as follows.
Form load:
Dim dbsPeriod As Database
Dim tdfEmployees As TableDef
Dim fldLoop As Field
Set dbsPeriod = OpenDatabase(DB_Period_Name$)
Set tdfEmployees = dbsPeriod.TableDefs!Ledger
AppendDeleteField tdfEmployees, "APPEND", "Location", dbText, 8
grdEmployees.DataSource = tdfEmployees
AppendDeleteField tdfEmployees, "DELETE", "Location"
dbsPeriod.Close
AppendDeleteField sub:
Private Sub AppendDeleteField(tdfTemp As TableDef, strCommand As String, _
strName As String, _
Optional varType, Optional varSize)
With tdfTemp
If .Updatable = False Then
MsgBox "Failed to initialise grid!"
Exit Sub
End If
If strCommand = "APPEND" Then
.Fields.Append .CreateField(strName, varType, varSize)
Else
If strCommand = "DELETE" Then .Fields.Delete strName
End If
End With
End Sub
With this code, no data is loaded into the grid at all.
You're not loading the data into the RecordSet before you delete the field. You need to get the data (using your SELECT query) into a data structure which the grid can use as the .DataSource
A TableDef is not a data structure, it just allows you to make changes to the database table itself, which is why your code isn't returning any rows.
I am trying to fetch checkboxlist from database (working). There is two checkboxlist on page: first gets fetch when page loads, second gets fetched selected values from first checkboxList. That is also working when I select first time from first checkboxList. But when I select second value from first checkboxList then string gets structured like this
('Value1','Value2 " & vbLf & "')
It should make string like this
('Value1','value2')
Now I don't understand what is this & vbLf &.
Dim joiner As String = ""
Dim whereClause As String = String.Empty
Dim categoryCondition As String = String.Empty
For i = 0 To categoryFilter.Items.Count - 1
If categoryFilter.Items(i).Selected Then
Dim category As String = categoryFilter.Items(i).ToString
categoryCondition = String.Concat(categoryCondition, joiner, String.Format("'{0}'", category))
If joiner = "" Then joiner = ","
End If
Next
Use built-in functionality to get the checked items and concatenate the string with LINQ
Get the checked items from the control (categoryFilter.CheckedItems)
Convert them to String (Cast(Of String)())
Put ' around them individually (Select())
Join them together with , (Aggregate())
Code:
Dim categoryCondition As String = Me.categoryFilter.CheckedItems.
Cast(Of String)().
Select(Function(i1) String.Format("'{0}'", i1)).
Aggregate(Function(i1, i2) String.Format("{0}, {1}", i1, i2))
If the line feed still exists, it's probable that it exists in the CheckedListBox items itself. You said that's coming from your database, so maybe that's (not working).
I keep getting the above error, i have tried setting the dropdownlist to new dropdownlist and then binding after but that then prevents any binding at all. my code:
Private Sub putStudentInstitutionList()
Dim strSql As String
Dim rtnSchoolData As DataSet
strSql = "SELECT schoolID, name FROM tSchool WHERE status = 1 ORDER BY name"
rtnSchoolData = cDB.executeReturn(strSql)
ddlInstitution.Items.Clear()
If rtnSchoolData.Tables(0).Rows.Count > 0 Then
ddlInstitution.AppendDataBoundItems = True
ddlInstitution.DataSource = rtnSchoolData.Tables(0)
ddlInstitution.DataTextField = "name"
ddlInstitution.DataValueField = "schoolID"
ddlInstitution.DataBind()
ddlInstitution.Items.Insert(0, New ListItem("", ""))
ddlInstitution.SelectedIndex = 0
ddlInstitution.Items.Insert(1, New ListItem("All Schools", "ALL"))
End If
End Sub
Any information on this would be greatly appreciated, the issue occurs on postback as it is attempting to re-bind the dropdownlist.
Wrapping it in a condition based on page.postback doesn't work as the dropdownlist is populated on the bases of another dropdownlist so is (in essence) bound on postback
Suppose I have a gridview containing some bound fields and a template field. The template field contains a button (btnUpload).
btnUpload triggers a modalpopupextender to display a panel which contains some controls and a submit button.
What I need to be able to do is to get the value from cell 0 of the row btnUpload was clicked on and pass it to the panel so I can retrieve some data into the panel's controls in the panel_load event based on that value from cell 0 of the gridview.
I thought I could do this by storing the value from cell 0 in a session variable but not sure if this is the 'best' way of doing this?
UPDATE - Using hidden fields on the panel to store row index as suggested by Karl
I have added two hidden fields to the panel and I populate them as below:
If e.CommandName = "Upload" Then
Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim ref As String = CType(row.Cells(0), DataControlFieldCell).Text
Dim dt As String = CType(row.Cells(2), DataControlFieldCell).Text
hfRef.Value = ref
hfDate.Value = dt
End If
This is working and populating the hidden fields OK. However in the btnSubmit_Click event procedure on the panel I am doing the following the variables I am assigning are not getting a value from the hidden fields:
If fuCertificate.HasFile Then
Dim fileName As String = Path.GetFileName(fuCertificate.PostedFile.FileName)
fuCertificate.SaveAs(Server.MapPath("~/CalibrationCerts/" & fileName))
Dim ref As String = hfRef.Value
Dim dt As String = hfDate.Value
Dim dc As New ExternalCalibrationDataContext
Dim thisRecord = (From x In dc.ext_calibration_records
Where x.calibration_no = ref AndAlso
x.calibration_date = dt).ToList()(0)
thisRecord.certificate_no = txtCertNumber.Text
thisRecord.certificate = "~/CalibrationCerts/" & fileName
Else
lblError.Text = "Please select a file to upload"
End If
I would recommend putting a HiddenField control in the panel used by the ModalPopupExtender. It will be easier to work with the HiddenField control than Session, because you will not need to do the casting of the hidden field Value to a string, because it already is a string. All Session objects are stored as Object, so it is necessary to cast to a string when retrieving the value from Session.
Hidden field option:
<asp:Panel>
// Other stuff for your modal popup
<asp:HiddenField id="HiddenField1` runat="server" />
</asp:Panel>
// Set hidden field
HiddenField1.Value = cell0.Text
// Get hidden field
string theValue = HiddenField1.Value
Session option:
// Set the value in Session
Session["Cell0Text"] = cell0.Text
// Retrieve the value from Session
// First check to see if the Session value still exists
if(Session["Cell0Text"] != null)
{
// Now cast the Session object to a string
string theValue = Session["Cell0Text"].ToString()
}
I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?
I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.
This puts the result query in to a DataTable.
DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;
foreach (DataRow dr in dt.Rows)
{
// Do something here IE grab the value of the first column
value = dr[0];
}
Repying to last question in comment:
YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")
I was getting crazy trying to do this simple operation:
retrieving data from sqldatasource and put it into variables that I can manipulate.
At the end, Here the working behind code to do this for VB.NET:
Dim DV As New DataView()
Dim DataTable As New DataTable()
Dim SqlDataSource1 As New SqlDataSource()
Dim VALUE As String
SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * from Table"
DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
DataTable = DV.ToTable()
For Each riga As DataRow In DataTable.Rows
VALUE = riga("table_name").ToString
Next
the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.
ENJOY