|
|
|
Hello All,
Could anyone please let me know, if we have any library or any easy going way, in order to work with USB2 flashdrive... All I need to do is detect a USB2 flash-drive, and then save a file in it.
I am looking for any driver, or library or any other way, So that I dont have to go deeper inside USB protocol.
I can use VB.NET or c# if necessary.
thanks in advance
Hrishi
|
|
|
|
|
Have you looked at this article[^]?
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
Hello,
So I have some code to change the color of a cell on a datagridview.
If e.Value.ToString.Length > 0 Then<br />
e.CellStyle.BackColor = Color.Blue<br />
e.CellStyle.ForeColor = Color.White<br />
End If
Where is the best place to put this, cell formatting or cell painting. Or is there a better place?
Thanks!
Rudy
|
|
|
|
|
|
Cellformatting is where you want it.
You were close. Just add another line under your code to change the color:
e.FormattingApplied = True
|
|
|
|
|
Hello,
I am currently trying to sort on a GridView after it has been populated with records from a SQL Server Express database.
I click on a column to sort it and I get the following error:
"DataTable must be set prior to using DataView."
The entirety of my code is as follows:
<script runat="server" >
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim sqlConn As New SqlConnection
Dim sqlCmd As New SqlClient.SqlCommand
Dim sqlReader As SqlDataReader
'If no values are supplied in the textbox, throw an error message.
If TextBox2.Text = "" Then
MsgBox("A centre code needs to be provided...")
End If
If TextBox2.Text <> "" Then
'Telling the system the location of the database.
sqlConn.ConnectionString = "server=ServerName;Initial Catalog=dbName;Trusted_Connection=yes"
'Here we are opening the connection to the database.
sqlConn.Open()
'This is to say that sqlCmd is a stored procedure.
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
'This is creating the command to execute the stored procedure based on the information given in the connection string.
sqlCmd = sqlConn.CreateCommand
'The command is triggered to execute the stored procedure which grabs all information for the specific centre.
sqlCmd.CommandText = "exec ProcedureName'" & TextBox2.Text & "' "
'This will read the rows in the database.
sqlReader = sqlCmd.ExecuteReader()
'If there are rows of data that match are criteria
If (sqlReader.HasRows) Then
'The rows of data are grabbed for the specific centre from the database using the data reader.
GridView2.DataSource = sqlReader
GridView2.DataBind()
Else
MsgBox("The centre code provided does not exist...")
End If
'This is closing the connection to the database once we have finished with it.
sqlConn.Close()
'This is to clear the list of items out of the GridView box.
End If
End Sub
Protected Sub gvSorting_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
Dim dtSortView As New DataTable
dtSortView = GridView2.DataSource
If dtSortView Is Nothing Then
Dim dvSortedView As New DataView(dtSortView)
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection)
GridView2.DataSource = dvSortedView
GridView2.DataBind()
End If
End Sub
Public Function getSortDirectionString(ByVal SortDirection As SortDirection) As String
Dim newSortDirection As String = String.Empty
If (SortDirection = SortDirection.Ascending) Then
newSortDirection = "ASC"
Else
newSortDirection = "DESC"
'Return newSortDirection
End If
Return newSortDirection
End Function
</script>
If anybody can give me any hints and tips on why this is happening, I would be extremely grateful.
From my understanding its telling me that there is no records in the GridView to sort, but why?
Thanks in advance,
Dan
|
|
|
|
|
The problem is, you set the DataSource to a DataReader and trying to cast it back to a DataTable . Use a DataTable as the DataSource for the GridView and your code should work.
|
|
|
|
|
Instead of using, sqlReader = sqlCmd.ExecuteReader()
Use this:
Dim DA As SQLDataAdapter
DA = New SqlDataAdapter(SQLcmd)
DA.Fill(DT)
Gridview2.Datasource = DT
When you use a dataadapter to "Fill" a datatable, it will fetch all the rows into the datatable, so you must be careful that you are not fetching thousands of rows of data, your memory and performance will suffer tremendously.
With the above changes, your grid should sort when you click on a column heading.
david
|
|
|
|
|
Hello David,
Thank you very much.
Quick question though - what is DT?
Is that DataTable?
Thanks,
Dan
|
|
|
|
|
Oops. I forgot to declare the datatable.
<br />
Dim DA As SQLDataAdapter<br />
Dim DT As New DataTable ' I forgot this line before<br />
<br />
DA = New SqlDataAdapter(SQLcmd)<br />
DA.Fill(DT)<br />
<br />
Gridview2.Datasource = DT<br />
<br />
Give me a vote of 5 if this works for you.
|
|
|
|
|
Hello David,
I have made the changes you suggested, but am still getting the same error:
Here is the code now:
<script runat="server" >
Public Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim sqlConn As New SqlConnection
Dim sqlCmd As New SqlClient.SqlCommand
'Dim sqlReader As SqlDataReader
Dim sqlAdapter As New SqlDataAdapter
Dim sqlDataTable As New DataTable
'If no values are supplied in the textbox, throw an error message.
If TextBox2.Text = "" Then
MsgBox("A centre code needs to be provided...")
End If
If TextBox2.Text <> "" Then
'Telling the system the location of the database.
sqlConn.ConnectionString = "server=ServerName;Initial Catalog=dbName;Trusted_Connection=yes"
'Here we are opening the connection to the database.
sqlConn.Open()
'This is to say that sqlCmd is a stored procedure.
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
'This is creating the command to execute the stored procedure based on the information given in the connection string.
sqlCmd = sqlConn.CreateCommand
'The command is triggered to execute the stored procedure which grabs all information for the specific centre.
sqlCmd.CommandText = "exec ProcedureName'" & TextBox2.Text & "' "
'This will read the rows in the database.
'sqlReader = sqlCmd.ExecuteReader()
'If there are rows of data that match are criteria
'If (sqlReader.HasRows) Then
'The rows of data are grabbed for the specific centre from the database using the data reader.
'GridView2.DataSource = sqlReader
'GridView2.DataBind()
sqlAdapter = New SqlDataAdapter(sqlCmd)
sqlAdapter.Fill(sqlDataTable)
GridView2.DataSource = sqlDataTable
GridView2.DataBind()
Else
MsgBox("The centre code provided does not exist...")
'End If
'This is closing the connection to the database once we have finished with it.
'sqlConn.Close()
End If
End Sub
Public Sub gvSorting_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
Dim dt = TryCast(Session("TaskTable"), DataTable)
Dim dtSortView As New DataTable
dtSortView = GridView2.DataSource
If dtSortView Is Nothing Then
Dim dvSortedView As New DataView(dtSortView)
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection)
GridView2.DataSource = dvSortedView
GridView2.DataBind()
End If
End Sub
Public Function getSortDirectionString(ByVal SortDirection As SortDirection) As String
Dim newSortDirection As String = String.Empty
If (SortDirection = SortDirection.Ascending) Then
newSortDirection = "ASC"
Else
newSortDirection = "DESC"
'Return newSortDirection
End If
Return newSortDirection
End Function
</script>
Any ideas at all?
Is there code still in there that needs to be taken out?
Or have I missed a bit of code?
Thanks for your help,
Dan
|
|
|
|
|
Declare the DataTable at the class level and use it wherever required.
|
|
|
|
|
Here is some code I just tested this morning and when I click on the column headings the grid does the sorting.
<br />
Dim SQLconn As SqlConnection<br />
Dim DA As New SqlDataAdapter<br />
Dim DT As New DataTable<br />
<br />
SQLconn = New SqlConnection(GetConnString())<br />
<br />
DA.SelectCommand = New SqlCommand("EXEC z_select_test", SQLconn)<br />
<br />
DA.Fill(DT)<br />
<br />
Me.DataGridView1.DataSource = DT<br />
You may want to un-hook your gvSorting_Sorting and getSortDirectionString routines because they are not necessary.
give it a shot.
BTW: I'm based on the East Coast of the United States, that's why it takes so long for me to respond to you.
|
|
|
|
|
Hello David,
I have done what you have advised, and am now getting a different error:
"ExecuteReader: CommandText property has not been initialized"
The code is:
Public Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim sqlConn As New SqlConnection
Dim sqlCmd As New SqlClient.SqlCommand
'Dim sqlReader As SqlDataReader
Dim sqlAdapter As New SqlDataAdapter
Dim sqlDataTable As New DataTable
'If no values are supplied in the textbox, throw an error message.
If TextBox2.Text = "" Then
MsgBox("A centre code needs to be provided...")
End If
If TextBox2.Text <> "" Then
'Telling the system the location of the database.
sqlConn.ConnectionString = "server=ServerName;Initial Catalog=KenyaKCSE;Trusted_Connection=yes"
'Here we are opening the connection to the database.
sqlConn.Open()
'This is to say that sqlCmd is a stored procedure.
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
'This is creating the command to execute the stored procedure based on the information given in the connection string.
sqlCmd = sqlConn.CreateCommand
'The command is triggered to execute the stored procedure which grabs all information for the specific centre.
sqlAdapter.SelectCommand = New SqlCommand("exec ProcName'" & TextBox2.Text & "' ")
'This will read the rows in the database.
'sqlReader = sqlCmd.ExecuteReader()
'If there are rows of data that match are criteria
'If (sqlReader.HasRows) Then
'The rows of data are grabbed for the specific centre from the database using the data reader.
'GridView2.DataSource = sqlReader
'GridView2.DataBind()
sqlAdapter = New SqlDataAdapter(sqlCmd)
sqlAdapter.Fill(sqlDataTable)
GridView2.DataSource = sqlDataTable
'GridView2.DataSource = sqlDataTable
GridView2.DataBind()
Else
MsgBox("The centre code provided does not exist...")
'End If
'This is closing the connection to the database once we have finished with it.
'sqlConn.Close()
End If
End Sub
Is there anything else that I am doing wrong?
I really appreciate all of your help.
Regards,
Dan
|
|
|
|
|
Couple of things I would try.
1) Comment out: sqlConn.Open
2) Comment out: sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
3) Comment out: sqlCmd = sqlConn.CreateCommand
4) Add the sqlConn as shown below:
sqlAdapter.SelectCommand = New SqlCommand("exec ProcName'" & TextBox2.Text & "'",sqlConn)
5) Comment out: sqlAdapter = New SqlDataAdapter(sqlCmd)
Then I believe your code will match my example more closely.
Remember to Un-Hook those other routines like I mentioned in an earlier post.
david
|
|
|
|
|
Hello David,
I did what you suggested, and now when I click on a column, I get the following error page:
"The GridView 'GridView2' fired event Sorting which wasn't handled."
My code (after being edited):
Public Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim sqlConn As New SqlConnection
Dim sqlCmd As New SqlClient.SqlCommand
'Dim sqlReader As SqlDataReader
Dim sqlAdapter As New SqlDataAdapter
Dim sqlDataTable As New DataTable
'If no values are supplied in the textbox, throw an error message.
If TextBox2.Text = "" Then
MsgBox("A centre code needs to be provided...")
End If
If TextBox2.Text <> "" Then
'Telling the system the location of the database.
sqlConn.ConnectionString = "server=servername;Initial Catalog=dbName;Trusted_Connection=yes"
'Here we are opening the connection to the database.
'sqlConn.Open()
'This is to say that sqlCmd is a stored procedure.
'sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
'This is creating the command to execute the stored procedure based on the information given in the connection string.
'sqlCmd = sqlConn.CreateCommand
'The command is triggered to execute the stored procedure which grabs all information for the specific centre.
sqlAdapter.SelectCommand = New SqlCommand("exec ProcName'" & TextBox2.Text & "' ", sqlConn)
'This will read the rows in the database.
'sqlReader = sqlCmd.ExecuteReader()
'If there are rows of data that match are criteria
'If (sqlReader.HasRows) Then
'The rows of data are grabbed for the specific centre from the database using the data reader.
'GridView2.DataSource = sqlReader
'GridView2.DataBind()
'sqlAdapter = New SqlDataAdapter(sqlCmd)
sqlAdapter.Fill(sqlDataTable)
GridView2.DataSource = sqlDataTable
'GridView2.DataSource = sqlDataTable
GridView2.DataBind()
Else
MsgBox("The centre code provided does not exist...")
'End If
'This is closing the connection to the database once we have finished with it.
'sqlConn.Close()
End If
End Sub
I create my GridView as thus:
<asp:GridView ID="GridView2" runat="server" Height="143px" AllowSorting="true"
I just cant understand what could be going wrong??
I really appreciate your help but do you know anything else that could poss be wrong?
Thanks,
Dan
|
|
|
|
|
Holy cow.
I must be totally sleep deprived.
You were talking about ASP.NET and I was testing with VB.NET. I believe you need to put back those routines for gvSorting and SortDirection.
This is the VB forum. Right? Not the ASP.NET forum. You can see my source of confusion.
Re-hook those routines and give it a shot.
Sorry, my bad.
|
|
|
|
|
Hi David,
Sorry for the confusion caused.
I made the changes you suggested and am now getting the following error:
"DataTable must be set prior to using DataView."
The error points to this line:
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection)
My code is now as follows:
Public Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim sqlConn As New SqlConnection
Dim sqlCmd As New SqlClient.SqlCommand
'Dim sqlReader As SqlDataReader
Dim sqlAdapter As New SqlDataAdapter
Dim sqlDataTable As New DataTable
'If no values are supplied in the textbox, throw an error message.
If TextBox2.Text = "" Then
MsgBox("A centre code needs to be provided...")
End If
If TextBox2.Text <> "" Then
'Telling the system the location of the database.
sqlConn.ConnectionString = "server=serverName;Initial Catalog=dbName;Trusted_Connection=yes"
'Here we are opening the connection to the database.
'sqlConn.Open()
'This is to say that sqlCmd is a stored procedure.
'sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
'This is creating the command to execute the stored procedure based on the information given in the connection string.
'sqlCmd = sqlConn.CreateCommand
'The command is triggered to execute the stored procedure which grabs all information for the specific centre.
sqlAdapter.SelectCommand = New SqlCommand("exec ProcName'" & TextBox2.Text & "' ", sqlConn)
'This will read the rows in the database.
'sqlReader = sqlCmd.ExecuteReader()
'If there are rows of data that match are criteria
'If (sqlReader.HasRows) Then
'The rows of data are grabbed for the specific centre from the database using the data reader.
'GridView2.DataSource = sqlReader
'GridView2.DataBind()
'sqlAdapter = New SqlDataAdapter(sqlCmd)
sqlAdapter.Fill(sqlDataTable)
GridView2.DataSource = sqlDataTable
'GridView2.DataSource = sqlDataTable
GridView2.DataBind()
Else
MsgBox("The centre code provided does not exist...")
'End If
'This is closing the connection to the database once we have finished with it.
'sqlConn.Close()
End If
End Sub
Public Sub gvSorting_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
Dim dt = TryCast(Session("TaskTable"), DataTable)
Dim dtSortView As New DataTable
dtSortView = GridView2.DataSource
If dtSortView Is Nothing Then
Dim dvSortedView As New DataView(dtSortView)
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection)
GridView2.DataSource = dvSortedView
GridView2.DataBind()
End If
End Sub
Public Function getSortDirectionString(ByVal SortDirection As SortDirection) As String
Dim newSortDirection As String = String.Empty
If (SortDirection = SortDirection.Ascending) Then
newSortDirection = "ASC"
Else
newSortDirection = "DESC"
'Return newSortDirection
End If
Return newSortDirection
End Function
Any more advice at all?
Many thanks,
Dan
|
|
|
|
|
In the section of code that loads the datatable to the grid, I had to store the datatable in a session variable.
sqlAdapter.Fill(sqlDataTable)
If Not (Session("TaskTable") Is Nothing) Then
Session.Remove("TaskTable")
End If
Session("TaskTable") = sqlDataTable
GridView2.DataSource = sqlDataTable
Here's the code for the "Sorting" event of the grid.
Protected Sub GridView2_Sorting(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView2.Sorting
Dim dt As DataTable
dt = TryCast(Session("TaskTable"), DataTable)
Dim dv As New DataView(dt)
If ViewState("SortDirection") Is Nothing Then
ViewState("SortDirection") = "ASC"
End If
If ViewState("SortDirection") = "ASC" Then
dv.Sort = e.SortExpression & " " & "DESC"
ViewState("SortDirection") = "DESC"
Else
dv.Sort = e.SortExpression & " " & "ASC"
ViewState("SortDirection") = "ASC"
End If
GridView2.DataSource = dv
GridView2.DataBind()
End Sub
Hopefully this will work for you, but I don't know if I would use this type of processing for large datatables and many users. Storing the datatable in a session variable take server memory and each person would have a copy of the datatable.
You could build a more sophisticated "GetData" routine and have the stored procedure return the data sorted for you, then re-bind it to the Gridview.
Hope this works for you.
|
|
|
|
|
Hello Shameel,
Thank you, how would I do that?
Thanks,
Dan
|
|
|
|
|
Follow David Mujica's instructions.
|
|
|
|
|
I did do a search but cannot find any sample. I am looking for a sample like this.
1. Search c: for say *.pdf files.
2. Show files in a grid with name of file, size, path.
3. Then I can delete all or just choose the files I want to delete.
|
|
|
|
|
Break the problem down into smaller problems:
1: get a list of files: http://www.developerfusion.com/code/3681/list-files-in-a-directory/[^]
2: Select the file you want in the list or datagrid (google will help you)
3: Perform the delete file on the selected record. (again google will do most of this for you).
I don't speak Idiot - please talk slowly and clearly
'This space for rent'
Driven to the arms of Heineken by the wife
|
|
|
|