如何将数据集的值从dal文件传递到bal文件,然后以vb.net wpf格式以orifinag形式显示 [英] How to pass values of dataset to bal file from dal file and then display in orifinag form in vb.net wpf form

查看:127
本文介绍了如何将数据集的值从dal文件传递到bal文件,然后以vb.net wpf格式以orifinag形式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DAL文件:



公共函数select_employee_details(ByVal empcode As String)作为数据集





Dim cmd As New SqlClient.SqlCommand

Dim tbl As New DataTable

Dim da As New SqlClient.SqlDataAdapter

Dim reader As SqlClient.SqlDataReader

Dim ds As DataSet

试试



cn.Open()

Dim sql_select As String

sql_select =select * from EmMaster emMaster_EmpCode ='+ empcode +'

cmd = New SqlClient.SqlCommand (sql_select,cn)

reader = cmd.ExecuteReader

da.Fill(ds,EmMaster)

cn.Close()

Catch ex As Exception

MessageBox.Show(找不到相关数据)

结束尝试

返回ds

结束Func



BAL文件:



公共函数select_employee_details(ByVal empcode As String)As DataSet

Dim ds As DataSet

Dim dalhba As new hbaDAL

dalhba.select_employee_details(empcode)





返回ds

结束功能



主要表格:



公共函数select_employee_details()

Dim ds As DataSet

Dim hbabal As new hbaBAL

hbabal.select_employee_details(txtEmpCode .Text.ToString)

txtEmpCode.Text = ds.Tables()

txtName.Text = ds.Tables(EmMaster)



结束功能



我尝试过:



我已经尝试了上面的代码,但它显示了一个空引用。我必须做什么修改

DAL File :

Public Function select_employee_details(ByVal empcode As String) As DataSet


Dim cmd As New SqlClient.SqlCommand
Dim tbl As New DataTable
Dim da As New SqlClient.SqlDataAdapter
Dim reader As SqlClient.SqlDataReader
Dim ds As DataSet
Try

cn.Open()
Dim sql_select As String
sql_select = "select * from EmMaster where EmMaster_EmpCode ='" + empcode + "'"
cmd = New SqlClient.SqlCommand(sql_select, cn)
reader = cmd.ExecuteReader
da.Fill(ds, "EmMaster")
cn.Close()
Catch ex As Exception
MessageBox.Show("No Relevant Data Found")
End Try
Return ds
End Function

BAL File :

Public Function select_employee_details(ByVal empcode As String) As DataSet
Dim ds As DataSet
Dim dalhba As New hbaDAL
dalhba.select_employee_details(empcode)


Return ds
End Function

Main form :

Public Function select_employee_details()
Dim ds As DataSet
Dim hbabal As New hbaBAL
hbabal.select_employee_details(txtEmpCode.Text.ToString)
txtEmpCode.Text = ds.Tables()
txtName.Text = ds.Tables("EmMaster")

End Function

What I have tried:

I have tried the above code but it shows a null reference.what modifications i have to made

推荐答案

你需要调试代码并找出什么是null以及为什么。我们不能为你做那件事。我们无法以任何方式查看您的屏幕或访问您的开发环境。我甚至会告诉我们你收到错误的哪一行。



另一个问题是你的代码有几个问题。我会帮你指出一对可能对你有用的东西。



首先,你永远不应该为你的SQL使用字符串连接。这让你对各种攻击持开放态度。你应该研究和理解参数化的sql。这个链接是一个很好的起点。

[ ^ ]



另一个问题是你使用了Try Catch块。对于一个你确实发现错误,但你没有做任何事情。所以你不知道如果发生了什么错误。你真的不应该在你的数据层中使用MessageBox。



看来你有一个SQLConnection对象,cn,你没有显示它的定义。你使用了有效的连接字符串吗?由于您吞下了错误,因此不会显示此类错误。



最后,我将指出您没有正确访问DataSet。您正尝试在此行中设置文本框的值,
You need to debug your code and find out what is null and why. We cannot do that for you. We cannot see your screen or access you development environment in any way. I would even help to tell us which line you received the error on.

Another problem is you have several issues with your code. I will help you point out a couple that might help you.

First, you should never use string concatenation for your SQL. That leaves you open to all kinds of attacks. You should research and understand parameterized sql. This link is a good place to start.
[^]

Another issue is your use of a Try Catch block. For one you do catch an error, but you do nothing with it. So you have no idea what error occurs if one does. And you really should not be using a MessageBox in your data layer.

Which it appears you have a SQLConnection object, cn, you do not show where it is defined. Did you use a valid connection string? Errors like this will not show since you have swallowed the error.

Lastly I will point out that you are not accessing the DataSet properly. You are trying to set the value of a textbox in this line,
txtEmpCode.Text = ds.Tables()





这不是正确的方法。要设置文本框的值,您需要执行以下操作:





That is the not the correct way. To set the value of the of the textbox you need to do something like

txtEmpCode.Text = ds.Table("TableName").Row(ROWINDEX)("ColumnName").ToString()


这篇关于如何将数据集的值从dal文件传递到bal文件,然后以vb.net wpf格式以orifinag形式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆