書上的範例
檢測資料庫有無資料
如果有顯示資料重複無法輸入
與
用編號與資料庫比對帶出員工姓名
將兩個範例合併
試著用兩個label顯示出來
程式碼如下
Protected Sub TextBox4_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
Dim args1 As New DataSourceSelectArguments
Dim DR1 As SqlDataReader = SqlDataSource1.Select(args1)
CONN1.ConnectionString = "Data Source=YAMAHA;Initial Catalog=DGOT;Persist Security Info=True;User ID=ya;Password=maha"
CONN1.Open()
Dim sql1 As String
sql1 = "SELECT MV002 FROM CMSMV WHERE MV001='" & TXT_ID3 & "'"
Dim SQLDAT1 As SqlCommand = New SqlCommand(sql1, CONN1)
Dim dr As SqlDataReader = SQLDAT1.ExecuteReader()
dr.Read()
Try
LBL_name.Text = dr.Item("MV002")
SQLDAT1.Cancel()
dr.Close()
CONN1.Close()
CONN1.Dispose()
Catch ex As Exception
LBL_name.Text = "無資料"
SQLDAT1.Cancel()
dr.Close()
CONN1.Close()
CONN1.Dispose()
End Try
'偵測資料重複
DR1.Read()
If DR1.HasRows = True Then
Label3.Text = TXT_ID1.Text + "資料重複!無法新增"
TXT_ID1.Text = ""
TXT_ID2.Text = ""
TXT_ID3.Text = ""
TXT_ID1.Focus()
Else
Label3.Text = "可新增"
TXT_DGA1.Focus()
End If
End Sub
卻出現
DataSourceSelectArguments , SqlDataReader = SqlDataSource1.Select(args1) 的錯誤訊息
請各位前輩指導
謝謝。
無法將類型 'System.Data.DataView' 的物件轉換為類型 'System.Data.SqlClient.SqlDataReader'。
錯誤訊息如上,謝謝
我隨便建了程式試Run@@~好像沒問題的~你的SqlDataSource1有設對嗎??
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
<asp:Label ID="LBL_name" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TXT_ID1" runat="server"></asp:TextBox>
<asp:TextBox ID="TXT_ID2" runat="server"></asp:TextBox>
<asp:TextBox ID="TXT_ID3" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TXT_DGA1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
Imports System.Data
Imports System.Data.SqlClient
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub TextBox4_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
Dim CONN1 As New SqlConnection
Dim args1 As New DataSourceSelectArguments
Dim DR1 As SqlDataReader = SqlDataSource1.Select(args1)
CONN1.ConnectionString = "Server=(local);Database=Jerwei;User ID=Jer;Password=Jer@1234;Trusted_Connection=False;Max Pool Size=300;"
CONN1.Open()
Dim sql1 As String
sql1 = "SELECT MV002 FROM CMSMV WHERE MV001='" & TextBox4.Text & "'"
Dim SQLDAT1 As SqlCommand = New SqlCommand(sql1, CONN1)
Dim dr As SqlDataReader = SQLDAT1.ExecuteReader()
dr.Read()
Try
LBL_name.Text = dr.Item("MV002")
SQLDAT1.Cancel()
dr.Close()
CONN1.Close()
CONN1.Dispose()
Catch ex As Exception
LBL_name.Text = "無資料"
TXT_ID2.Text = ex.ToString
SQLDAT1.Cancel()
dr.Close()
CONN1.Close()
CONN1.Dispose()
End Try
'偵測資料重複
'DR1.Read()
'If DR1.HasRows = True Then
' Label3.Text = TXT_ID1.Text + "資料重複!無法新增"
' TXT_ID1.Text = ""
' TXT_ID2.Text = ""
' TXT_ID3.Text = ""
' TXT_ID1.Focus()
'Else
' Label3.Text = "可新增"
' TXT_DGA1.Focus()
'End If
End Sub
End Class
前輩
我以為我修改好了
但是結果還是不對
變成只要在資料庫裏面的(CONA04)都會被判斷成 資料已重複!無法新增
請問我哪裡做錯了嗎
請指導,謝謝..........
Protected Sub TXT_CONA04_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TXT_CONA04.TextChanged
CONN1.ConnectionString = ""
CONN1.Open()
Dim sql2 As String
sql2 = "SELECT CONA01 FROM MDCONA WHERE CONA04 = @CONA04 "
Dim SQLDAT2 As SqlCommand = New SqlCommand(sql2, CONN1)
Cmmd.Parameters.AddWithValue("@CONA04", TXT_CONA02 + TXT_CONA04)
Dim dr2 As SqlDataReader = SQLDAT1.ExecuteReader()
dr2.Read()
'偵測欄位有無重複
dr2.Read()
If dr2.HasRows = True Then
Label1.Text = "資料已重複!無法新增"
TXT_CONA02.Text = ""
TXT_CONA04.Text = ""
TXT_CONA02.Focus()
dr2.Close()
CONN1.Close()
CONN1.Dispose()
Else
Label1.Text = "可新增"
TXT_CONA02.Focus()
dr2.Close()
CONN1.Close()
CONN1.Dispose()
End If
End Sub
你應該改這樣試試~
Protected Sub TXT_CONA04_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TXT_CONA04.TextChanged
CONN1.ConnectionString = ""
CONN1.Open()
Dim sql2 As String
sql2 = "SELECT CONA01 FROM MDCONA WHERE CONA04 = @CONA04 "
Dim SQLDAT2 As SqlCommand = New SqlCommand(sql2, CONN1)
Cmmd.Parameters.AddWithValue("@CONA04", TXT_CONA02 + TXT_CONA04)
Dim dr2 As SqlDataReader = SQLDAT1.ExecuteReader()
'偵測欄位有無重複
If dr2.Read() Then
Label1.Text = "資料已重複!無法新增"
TXT_CONA02.Text = ""
TXT_CONA04.Text = ""
TXT_CONA02.Focus()
dr2.Close()
CONN1.Close()
CONN1.Dispose()
Else
Label1.Text = "可新增"
TXT_CONA02.Focus()
dr2.Close()
CONN1.Close()
CONN1.Dispose()
End If
End Sub
錯誤訊息
物件型別 System.Web.UI.WebControls.TextBox 與已知的管理提供者原始型別之間並無對應。
有檢查欄位資料庫訊息皆是正確,請問要如何處理?
謝謝。
Using Conn As New SqlConnection()
Conn.ConnectionString = ""
Conn.Open()
Dim SqlTxt As String = ""
SqlTxt &= " SELECT CONA02, CONA04 "
SqlTxt &= " FROM MDCONA "
SqlTxt &= " WHERE CONA02=@CONA02 AND CONA04=@CONA04 "
Dim HasData As Boolean = False
Using Cmmd As New SqlCommand(SqlTxt, Conn)
Cmmd.Parameters.AddWithValue("@CONA02", TXT_CONA02)
Cmmd.Parameters.AddWithValue("@CONA04", TXT_CONA04)
Dim Dr2 As SqlDataReader = Cmmd.ExecuteReader
If Dr2.HasRows Then
HasData = True
End If
Dr2.Close()
If HasData Then
Label1.Text = "顯示資料已經存在,無法新增"
Else
Label1.Text = "進行新增"
End If
End Using
End Using
("@CONA02", TXT_CONA02)
("@CONA04", TXT_CONA04)
加上
("@CONA02", TXT_CONA02.text)
("@CONA04", TXT_CONA04.text)
恩~你的錯誤是因為直接用了物件
應該是物件.屬性,把資料正確傳入~