Get DataRow from the DropDownList - asp.net

I doesn't know how to convert the dropdownlist selected item to datarow
sDropDownList.DataSource = GetTable();
sDropDownList.DataTextField = "Name";
sDropDownList.DataValueField = "ID";
I tried to get the datarow from the dropdownlist like this.. .
DataRow row = (sDropDownList.SelectedItem as DataRowView).Row;
But i get an error .. . errors like Can't convert listitems to datarows

It's not a DataRow on postback. A DropDownList's item has a Text and a Value since it's a ListItem. If you need the other informations from the record later (on postback) you have to query the database again.
Actually you have all you need since you've set the DataValueField to the ID-column.
int id = int.Parse(sDropDownList.SelectedValue);
// now query the database to get the whole record or the information you need

Related

Passing column value from specific row index of a GridView

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()
}

Binding repeater to a record?

I need to bind repeater with some structure that has only a record. Data table is already populated. I tried to bind it with a datarow or datarowview but it do not allow to do. Any suggestion please ?
Can I use Linq query for that ? If so can you please guide me the query for selecting first row of data table ?
Try this
Dim drList As New List(Of DataRow)
drList.Add(myDataTable.Rows(0)) 'add the first row in the datatable
myRepeater.DataSource = drList

Select Row from GridView

how i can select Row from GridView without DataKey ?
I have a Grid view with data source from List and i need when
press select select Get the selected row ?
List<string> RequestedMovie = new List<string>();
GridView3.Datasource = RequestedMovie;
You can directly get the selected row by using the following line of code
GridViewRow objRow = GridView3.SelectedRow;
..
..
I hope that will resolve your problem.

Format BoundField Data Types Dynamically

I'm using a CheckBoxList to define which columns are displayed in a GridView. I populate the CheckBoxList using a query like
select column_name, data_type from information_schema.columns
where table_name = 'myTable'
After the user has chosen the columns (with various Data Types in them) they wish to display, they press a button and the following snippet of VB code generates the GridView.
For Each item As ListItem In chooseColsList.Items
If item.Selected Then
Dim bf As New BoundField()
'PRODUCES BUGGY RESULTS BECAUSE APPLIED TO ALL BoundFields
bf.DataFormatString = "{0:dd-MMM-yyyy}"
bf.ApplyFormatInEditMode = True
bf.DataField = item.Value
bf.HeaderText = item.Value
bf.SortExpression = item.Value
statusReportGrid.Columns.Add(bf)
End If
Next
What I want to do is to apply the DataFormatString ONLY to the columns with a 'date' Data Type in them. However, I have found no way of programmatically determining the Type of the data in the column being bound, no any way of passing this info to the Control. This information exists in the information_schema, as shown in my query, but I don't know how to extract that out and use it to dynamically setup my BoundFields. It is important to note that I'm using an SqlDataSource.
I've experimented with just about every possible solution I can think of and end up here.
Thanks in advance for your help, it is VERY appreciated :)
If you set your check box list like this:
<asp:checkboxlist id="list" runat="server"
DataTextField="column_name" DataValueField="data_type" DataSourceID="YourDSID" />
You should be able to iterate through the items and check the value of the current Item like so:
For Each item As ListItem In chooseColsList.Items
If item.Selected Then
Dim bf As New BoundField()
If StrComp(item.Value,"date") = 0 Then 'Or however datetime is returned
bf.DataFormatString = "{0:dd-MMM-yyyy}"
bf.ApplyFormatInEditMode = True
End If
bf.DataField = item.Text
bf.HeaderText = item.Text
bf.SortExpression = item.Text
statusReportGrid.Columns.Add(bf)
End If
Next

Error in DropDownList using ASP.NET

I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains some records of a table in the data base.
So I did this :
DDL.DataSource = myDataReader
DDL.DataBind()
But it's giving me (5 records) "the number of records of the table" but like this :
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
You should set DataTextField and DataValueField, otherwise data binding will perform .ToString() on every row and put it as item:
DDL.DataSource = myDataReader;
DDL.DataTextField = "[Text column name]";
DDL.DataValueField = "[Value column name]";
DDL.DataBind();
you have to set the text and the key fields of the ddl before you databind
DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":
The code : ddl.datasource=reader is just setting the content present in reader (array of columns of table) as the main source of data.Now as ddl show only a single column in it so u need to write a piece of code which tells ddl that which column it has to display.So you will write: ddl.textfield="column name which you want to show";
and ddl.valuefield="column name which you want as a reference to pass to database";

Resources