Is it possible to select from selected checklist SQL Server - asp.net

I have a query select and i need is to select the data in selected checkbox list. Is it possible?
This is my query: Im tring to use -IN- but i think this is not the solution
SELECT [scod], [ecode], [scty] ,[sonm] FROM [Z_ALI].[dbo].[M_STORE] WHERE [scod] IN ('FROMTHECHECKLIST')
I' trying to do like this:
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim CheckRow As CheckBox = (TryCast(row.Cells(1).FindControl("chckSelector"), CheckBox))
If CheckRow.Checked Then
Dim scode As String = TryCast(row.Cells(2).FindControl("lblsstorecode"), Label).Text
Dim sql As String = "SELECT [scod], [ecode], [scty] ,[sonm] FROM [Z_ALI].[dbo].[M_STORE] WHERE [scod] IN ('+scode+')"
Using cmd As New SqlCommand(sql)
Using dtViewing As Data.DataTable = code.GetData(cmd, "NSP", code.HRIS, "SELECT")
If code.err = String.Empty Then
GridView4.DataSource = dtViewing
GridView4.DataBind()
Else
code.setStatus(code.err, Me.Page, "")
End If
End Using
End Using
End If
End If
Next
For example this is the selected list from checkboxlist
Checklist
-|351530928006|
0|351530928007|
0|351530928008|
0|351530928009|
-|3515309280010|
zero is the selected
- is not selected
Is it possible to get or iterate all the list from the query.

SELECT scod,ecode,scty,sonm
FROM M_STORE
WHERE scod IN (Checklist(0), Checklist(1), Checklist(2)...)
or have your checklist in sql table and do this
SELECT scod,ecode,scty,sonm
FROM M_STORE
WHERE scod IN (SELECT scod FROM ChecklistTable)

Related

Change Grid view value based on some condition

I have a Grid view control with a few columns that is populated from a form that the user fills in. In the form some values are not mandatory and if the user does not select them, then it is displayed as "select" in the Grid view. How do i go about changing that value to something for relative like "none" when i load the Grid view. In other words , if that column contains "select" then change it to "None"
This is my code behind to populate my Gridview
Private Sub LoadGridAll()
txtSearchEmployee.Text = ""
Try
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("XXX").ConnectionString)
Using cmd As New SqlCommand("SELECT EmployeeCodes.Code, EmployeeCodes.FirstName , EmployeeCodes.LastName , EmployeeCodes.EmployeeID , CostCentre , ExaminationType.ExaminationTypeName , PhysicalExam.PhysicalExamName , ExaminationType.ExaminationTypeName , PhysicalExam.PhysicalExamName , Audiogram.AudiogramName , AudiogramRec.AudiogramRecName,LungFunction.LungFunctionName,ChestResults,ECGResult,DrugScreeningResult,BloodGlucoseResult,GGTResult,LeftEyeDayNight,RightEyeDayNight,LeftEyeCorrDayNight,RightEyeCorrDayNight,VisualFieldLeftDayNight,VisualFieldRightDayNight,ColourVisionDayNight,DeptPerceptionDayNight,OptometristYesNo,Outcome.Name , OutcomeRecommendations.OutcomeRecommendationsName,OtherProblems,Notes,DateTested,NextDueDate FROM MedicalResults,EmployeeCodes,ExaminationType,PhysicalExam,Audiogram,AudiogramRec,LungFunction,Outcome,OutcomeRecommendations WHERE MedicalResults.EmployeeID = EmployeeCodes.EmployeeID AND ExaminationType.ExaminationTypeID = MedicalResults.ExaminationTypeID AND PhysicalExam.PhysicalExamID = MedicalResults.PhysicalExamType AND Audiogram.AudiogramID = MedicalResults.AudiogramID AND MedicalResults.AudiogramRecID = AudiogramRec.AudiogramRecID AND LungFunction.LungFunctionID = MedicalResults.LungFunctionID AND OutcomeRecommendations.OutcomeRecommendationsID = MedicalResults.OutcomeRecommendationsID AND Outcome.OutcomeID = MedicalResults.OutcomeID ", conn)
conn.Open()
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
For Each row As DataRow In dt.Rows
Dim strDetail As Object
strDetail = row.Item(10)
If strDetail = "Select" Then
strDetail = "None"
End If
Next row
GridViewAll.DataSource = dt
GridViewAll.DataBind()
conn.Close()
End Using
End Using
Catch ex As Exception
End Try
End Sub
First of all, I wouldn't save "Select" to DB, it would make more sense to save a null value (DBNull.Value). Then in your select statement you could use a function IsNull(yourField,'None'). Edit: if you have Null values in grid, you could also use DataGridView's property:
Me.GridViewAll.RowsDefaultCellStyle.NullValue = "None"
However, if you want to do it like you've shown, you're very close - you just need to save the new String value to DataTable:
row.Item(10) = "None" 'strDetail

Read multiple items from list box to query information from a database

Ok so what I'm trying to do is allow the users of my web form in asp.net to select multiple assets from a list box. When they select these assets and press the select button it fires the following code which should run through the selected indices, query the DB for the description and asset tag's, and populate the Description and Asset tag boxes with those values. Working through this I've been able to get it to read the values from the database and populate the fields but it would only populate the value from the first selected item and it would just insert the same value equal to the number of times equal to the number of items selected in the inbox. I tried my current code to run through the indices a little better but it seems to be failing the If statement and I just can't figure out why. Any help is appreciated, thanks everyone!
Dim i As Integer
For i = 0 To lstAssets.GetSelectedIndices.Count
If lstAssets.Items(i).Selected = True Then
Dim str2 As String = lstAssets.Items(i).Value.ToString
Dim cs As New OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings("CSIWebUpd").ConnectionString)
Dim ds As String = cs.DataSource
Dim cn As OracleConnection = New OracleConnection("user id=webupd;password=webupd;data source=" + ds)
Dim sql As String = "SELECT description, tag FROM assets WHERE id = :id"
Dim cmd As New OracleCommand(sql, cn)
cmd.Parameters.Add(":id", OracleDbType.Varchar2).Value = str2
cmd.CommandType = CommandType.Text
cmd.BindByName = True
cn.Open()
Dim dr As OracleDataReader = cmd.ExecuteReader()
cmd.ExecuteNonQuery()
dr.Read()
desc = dr.GetString(0).ToString
ass = dr.GetString(1).ToString
If txtDescription.Text = Nothing Then
txtDescription.Text = dr.GetString(0).ToString
Else
txtDescription.Text = txtDescription.Text + Chr(13) + Chr(10) + dr.GetString(0).ToString
End If
If txtAsset.Text = Nothing Then
txtAsset.Text = dr.GetString(1).ToString
Else
txtAsset.Text = txtAsset.Text + Chr(13) + Chr(10) + dr.GetString(1).ToString
End If
cmd.Dispose()
cn.Close()
End If
Next i
UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional
UpdatePanel1.Update()
I would work with the SelectedIndices values in this way
For Each i in lstAssets.GetSelectedIndices
Now i contains the index of the items selected not the offset of the GetSelectedIndices array
The GetSelectedIndices returns an array where every element of the array is the index in the Items collection where you have an item selected.
An example could explain better:
Supposing the the items selected are 1, 5 6 (Count=3), your code set the value of i to 0,1,2 and then uses that value to lookup the ID in the items collection, but the following check on lstAssets.Items(i).Selected found just the item at index 1 as selected. Instead using the for each approach you get the values 1,5,6.

ORA-01036: illegal variable name/number error on parameter in SELECT

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.

SqlDataSource.Select()? How do I use this? (ASP.net)

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

Asp.net Grid View

In my Grid View ,When a button is clicked,i want to insert that row in to database and at the same time make the row invisible in the Grid View.
I can insert in to database but can't make the inserted row invisible.
Dim PayID As Integer = (e.CommandArgument)
Dim EmpID As Integer = (e.CommandArgument)
Dim EID As Integer = CType(Dg1.DataKeys(EmpID).Values("EmpID"), Integer)
Dim PID As Integer = CType(Dg1.DataKeys(PayID).Values("PayID"), Integer)
cmd.CommandText = "Insert into EmployDetails(EmpID,PayID,PayDate)
Values(" & EID & " ," & PID & ",GetDate())"
cmd.ExecuteNonQuery()
Thank You
You might need to bind the GridView to a DataView instead of a DataSet. Then when you add a row update the DataView to exclude the new row.
After insering your row to db, you should remove the related record from datasource (datatable, dataview) and rebind your gridview.
EDIT :
After your Insert operation :
1. take your remove the related row from your datasource :
Dim insertedRows As DataRow() = myDataTable.Select("ID = " & id)
For Each dr As DataRow In insertedRows
myDataTable.Rows.Remove(dr)
Next
2. And after that re-bind your gridview :
gridView.DataSource = myDataTable
gridView.DataBind()
NOTE : I use a converter to convert codes C# to VB. Hope it's ok.

Resources