Where i made the mistake? [Resolved]

Posted by Thiru under ASP.NET on 8/24/2011 | Points: 10 | Views : 1219 | Status : [Member] | Replies : 6
Where i made the mistake?

 Dim mycon As MySqlConnection
Dim cmd As MySqlCommand
Dim rdr As MySqlDataReader
Dim cncls As ConnClass = New ConnClass
Dim cnt As Integer
Dim qry, ph As String
Call cncls.fndbmah()
mycon = cncls.mysqlconn
mycon.Open()
Try
cmd = New MySqlCommand("select count(*) as cnt from tblcust where id='" & ViewState("sel") & "'", mycon)
cnt = cmd.ExecuteScalar
If cnt = 1 Then
qry = "update tblcust set name='" & txtnm.Text & "',mobile='" & txtmb.Text & "',city='" & txtcity.Text & "',e_mail='" & txtemail.Text & "' where id='" & ViewState("sel") & "'"
Else
ph = Server.MapPath("./") & System.IO.Path.GetFileName(FileUpload1.FileName)
qry = "insert into tblcust(id,name,mobile,city,e_mail,photo) values('" & txtid.Text & "','" & txtnm.Text & "','" & txtmb.Text & "','" & txtcity.Text & "','" & txtemail.Text & "','" & ph & "')"
End If
cmd = New MySqlCommand(qry, mycon)
rdr = cmd.ExecuteNonQuery
Catch ex As Exception
MsgBox(Err.Description)
Finally
mycon.Close()
End Try



Error:
Compiler Error Message: BC30311: Value of type 'Integer' cannot be converted to 'MySql.Data.MySqlClient.MySqlDataReader'.
note: error indicating the line: rdr = cmd.ExecuteNonQuery




Responses

Posted by: Ndebata on: 8/24/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
cmd.ExecuteNonQuery will return an integer and in your case rdr type is SqlDataReader, so it is throwing exception ,
try assign it to an integer
int NumofrowAffected =cmd.ExecuteNonQuery();


Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 8/25/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
cmd.ExecuteNonQuery always returns a no. of rows affected. so it alwz be a integer. so u need to store result into integer type variable

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 8/25/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 50

Up
0
Down

Resolved
Your code is ok , but you tried to return a reader from the executenoquery(). The ExecuteNonquery() does not return data , it will only return 1 or 0 if i a m not mistaken , that indiacates it what you were doing was successfully or not and you used the executeScalar correctly , because it returns the fist row and the first value in the first column. So you code should be this , i removed the part that accepts the return from the executenonquery



    Dim mycon As MySqlConnection


Dim cmd As MySqlCommand

Dim rdr As MySqlDataReader

Dim cncls As ConnClass = New ConnClass

Dim cnt As Integer

Dim qry, ph As String

Call cncls.fndbmah()

mycon = cncls.mysqlconn


Try


mycon.Open()

cmd = New MySqlCommand("select count(*) as cnt from tblcust where id='" & ViewState("sel") & "'", mycon)

cnt = cmd.ExecuteScalar


If cnt = 1 Then

qry = "update tblcust set name='" & txtnm.Text & "',mobile='" & txtmb.Text & "',city='" & txtcity.Text & "',e_mail='" & txtemail.Text & "' where id='" & ViewState("sel") & "'"

Else

ph = Server.MapPath("./") & System.IO.Path.GetFileName(FileUpload1.FileName)

qry = "insert into tblcust(id,name,mobile,city,e_mail,photo) values('" & txtid.Text & "','" & txtnm.Text & "','" & txtmb.Text & "','" & txtcity.Text & "','" & txtemail.Text & "','" & ph & "')"

End If

cmd = New MySqlCommand(qry, mycon)

cmd.ExecuteNonQuery

Catch ex As Exception

MsgBox(Err.Description)

Finally

mycon.Close()

End Try
End Class


Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 8/25/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
@ Vuyiswamb :
no its not return 0 & 1, its alwz returns no. of rows affected in DB. so if cmd.executenonquery is successful thn its value is alwz grtr thn 0

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 8/25/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks to all - i marked all yours as answer.

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 8/25/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
@Rickeybglr

You are Right , pardon me for that.

Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response