|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help with some code?I have designed a simple web page that takes input from a user(from a textbox), then searches for that input in a database, when the Search button is clicked. Assuming the connection is good - help with the error, please? Sqlconnection1 was dragged and dropped on the the design window and the connection was accepted. I assume I can use that instead of manually typing out a whole connection string - right? (instead of having to type in ConnStr="data source=IP"&"database=DB;.....") Here is the code: Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim ConnStr As New SqlConnection ConnStr = SqlConnection1 Dim SQL As String ConnStr.Open() Try SQL = "select * from Database WHERE SearchCol LIKE %' " & Search.Text & "'" Dim MySqlCmd As New SqlCommand(SQL, ConnStr) Dim Reader As SqlDataReader Reader = MySqlCmd.ExecuteReader myDatagrid.DataSource = Reader Finally ConnStr.Close() End Try End Sub 1) There is not option for my Datagrid to bind (no myDatagrid.Databind() option in the drop downs - why not? After the myDatagrid.Datasource there should be the bind - right? 2)This error appears at the ConnStr.Open() line: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll Additional information: System error. 3) [bonus] Note, the connection always asks me for a password - user name and password, when I try to make a new connection or refresh data (via the server tools in VS). How do I make it trusted. The DB (sql server)ir running on a remote machine. Suggestions? Using Visual Studio 2003. Many Thanks! Tmuld Couple things:
1. The code for your Sqlconnection1 is located in the hidden VS.Net generated code section. It would be helpful to see what the connection string it is using is. 2. This is just wrong for several reasons. First you create a new object then you overwrite the object with an object that is already created. Dim ConnStr As New SqlConnection ConnStr = SqlConnection1 Try instead: Dim ConnStr As SqlConnection ConnStr = SqlConnection1 Or just use SqlConnection1 instead of ConnStr 3. Place this code inside your Try so you can see the exception ConnStr.Open() 4. Add a catch to your try so you can get some useful information. This will tell you what is wrong with your connection string. Catch ex as exception debug.writeline(ex.message) Try that and get back to me... Chris Show quoteHide quote "Tmuld" <tmuld***@spliced.com> wrote in message news:1112888971.152729.293940@f14g2000cwb.googlegroups.com... > Hello, > > I have designed a simple web page that takes input from a user(from a > textbox), then searches for that input in a database, when the Search > button is clicked. Assuming the connection is good - help with the > error, please? > > Sqlconnection1 was dragged and dropped on the the design window and the > connection was accepted. I assume I can use that instead of manually > typing out a whole connection string - right? (instead of having to > type in ConnStr="data source=IP"&"database=DB;.....") > > Here is the code: > > Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles btnSearch.Click > > Dim ConnStr As New SqlConnection > ConnStr = SqlConnection1 > Dim SQL As String > ConnStr.Open() > Try > SQL = "select * from Database WHERE SearchCol LIKE %' " & > Search.Text & "'" > Dim MySqlCmd As New SqlCommand(SQL, ConnStr) > Dim Reader As SqlDataReader > Reader = MySqlCmd.ExecuteReader > myDatagrid.DataSource = Reader > Finally > ConnStr.Close() > End Try > > End Sub > > > > 1) There is not option for my Datagrid to bind (no > myDatagrid.Databind() option in the drop downs - why not? After the > myDatagrid.Datasource there should be the bind - right? > > 2)This error appears at the ConnStr.Open() line: > > An unhandled exception of type 'System.Data.SqlClient.SqlException' > occurred in system.data.dll > > Additional information: System error. > > 3) [bonus] Note, the connection always asks me for a password - user > name and password, when I try to make a new connection or refresh data > (via the server tools in VS). How do I make it trusted. The DB (sql > server)ir running on a remote machine. > > Suggestions? > > Using Visual Studio 2003. > > Many Thanks! > > Tmuld > Hello again!
I gave your suggestions a try and replaced ConnStr with SqlConnection1 (it makes sense - it is already there....) Brilliant move on the error messaging - it seem it is failing on my login "login failed for users 'test'". Not very smart on my part.... So I included the password into Sqlconnection1. In trying it again, I now get this error: "Complex DataBinding accepts as a datasource either an IList or IListSource" What is an IList or IListSource? Does this have to do with the lack of datagrid databind()? Again, many thanks, Tmuld.
DataReader in Excel (Better Post than the last)
Update for VS .NET 2003? for loop ending condition Compiler errors due to temp file inconsistencies ?? Remove items from listbox How to use Regex to Parse this String How to get assembly file info? Date validation / verification expand a node of a treeview programmatically Prevent User Entering New Values in ComboBox |
|||||||||||||||||||||||