By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,162 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,162 IT Pros & Developers. It's quick & easy.

how to avoid error CommandText property has not been initialized properly

P: 3
database is in the sqlYog, while i run this program i am getting the error CommandText property has not been initialized properly..? please help me

Expand|Select|Wrap|Line Numbers
  1. Imports MySql.Data.MySqlClient Imports System.Data
  2.  
  3. Partial Class login Inherits System.Web.UI.Page
  4.  
  5. Public dbconn As New MySqlConnection
  6. Public sql As String
  7. Public dbcomm As New MySqlCommand
  8. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  9.  
  10.     dbconn = New MySqlConnection("data source= localhost; user id=root; password=search; database=bookstore;")
  11.     dbconn.Open()
  12.     dbcomm.Connection = dbconn
  13.  
  14. End Sub
  15.  
  16. Protected Sub btn_login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_login.Click
  17.  
  18.     Try
  19.         dbcomm = New MySqlCommand(sql, dbconn)
  20.  
  21.         sql = "select * from bookstore.lib_user where username=@username  and password=@password"
  22.  
  23.         dbcomm.Parameters.AddWithValue("@username", txt_username.Text)
  24.         dbcomm.Parameters.AddWithValue("@password", txt_password.Text)
  25.  
  26.         Dim dbadpt As New MySqlDataAdapter(dbcomm)
  27.         Dim dt As New DataTable()
  28.         dbadpt.Fill(dt)
  29.         If dt.Rows.Count > 0 Then
  30.             MsgBox("login is suceesfull")
  31.         Else
  32.             Literal1.Text = "invalid login"
  33.         End If
  34.  
  35.     Catch ex As Exception
  36.         MsgBox("read this error: " + ex.Message)
  37.     End Try
  38. End Sub
  39. End Class
Dec 27 '13 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,369
Please use code tags when posting code or formatted data.

On lines 19 and 21, you create your command with a blank variable, then you populate the variable. That's backwards.
Dec 27 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.