how to avoid error CommandText property has not been initialized properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hrutu
    New Member
    • Dec 2013
    • 3

    #1

    how to avoid error CommandText property has not been initialized properly

    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

    Code:
    Imports MySql.Data.MySqlClient Imports System.Data
    
    Partial Class login Inherits System.Web.UI.Page
    
    Public dbconn As New MySqlConnection
    Public sql As String
    Public dbcomm As New MySqlCommand
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        dbconn = New MySqlConnection("data source= localhost; user id=root; password=search; database=bookstore;")
        dbconn.Open()
        dbcomm.Connection = dbconn
    
    End Sub
    
    Protected Sub btn_login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_login.Click
    
        Try
            dbcomm = New MySqlCommand(sql, dbconn)
    
            sql = "select * from bookstore.lib_user where username=@username  and password=@password"
    
            dbcomm.Parameters.AddWithValue("@username", txt_username.Text)
            dbcomm.Parameters.AddWithValue("@password", txt_password.Text)
    
            Dim dbadpt As New MySqlDataAdapter(dbcomm)
            Dim dt As New DataTable()
            dbadpt.Fill(dt)
            If dt.Rows.Count > 0 Then
                MsgBox("login is suceesfull")
            Else
                Literal1.Text = "invalid login"
            End If
    
        Catch ex As Exception
            MsgBox("read this error: " + ex.Message)
        End Try
    End Sub
    End Class
    Last edited by Rabbit; Dec 27 '13, 04:51 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    Working...