本文介绍了日期时间选择器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好,我是编程的初学者,我想问一下如何制作这个程序,我现在正在制作。 目前我的大学作业我有人被要求使用visual studio 2012和microsoft sql server 2012申请在中学注册新生。 这是我目前正在处理的代码: 表格: 公开 类 Form1 私有 Sub Form1_Load(发件人作为 对象,e As EventArgs)句柄 MyBase .Load ClassKoneksi.namadatabase = KPIRWAN ClassSiswa.namadatabase = KPIRWAN Dim dsdata 作为 新 DataSet Dim sql 作为 字符串 sql = select * from datasiswa dsdata = ClassSiswa.displayData(ClassSiswa.opencon,sql, Datasiswa) DataGridView1.DataSource = dsdata DataGridView1.DataMember = datasiswa DataGridView1。 ReadOnly = True ClassSiswa.closecon() 结束 Sub 私有 Sub btnKeluar_Click(sender 作为 对象,e 作为 EventArgs)句柄 btnKeluar.Click 我 .Close() 结束 Sub 私有 Sub btnSimpan_Click(发件人作为 对象,e 作为 EventArgs)句柄 btnSimpan.Click 如果 Len(Trim(txt_nis) .Text))= 0 或 Len(Trim(txt_nisn.Text))= 0 或 Len(Trim(txt_namasiswa.Text))= 0 或 Len(Trim(cmb_kelaminsiswa.Text)) )= 0 或 Len(Trim(txt_kotalahir.Text))= 0 或 Len(修剪(DTP_siswa.Value))= 0 或 Len(Trim(cmb_agamasiswa.Text))= 0 或 Len(Trim(txt_beratsiswa.Text))= 0 或 Len(Trim(txt_tinggisiswa.Text)) )= 0 然后 MsgBox( Data Belum Lengkap) 退出 Sub 结束 如果 ClassSiswa.Nis = txt_nis.Text ClassSiswa.Nisn = txt_nisn.Text ClassSiswa.Nama_Siswa = txt_namasiswa.Text ClassSiswa .Jenis_Kelamin = cmb_kelaminsiswa.Text ClassSiswa.Kota_Lahir = txt_kotalahir.Text ClassSiswa.Tanggal_Lahir = DTP_siswa.Value ClassSiswa.Agama = cmb_agamasiswa.Text ClassSiswa.Berat_Badan = txt_beratsiswa.Text ClassSiswa.Tinggi_Badan = txt_tinggisiswa.Text ClassSiswa.RekamData(ClassSiswa.opencon) MsgBox( 数据siswa Berhasil Disimpan) 结束 Sub 私有 Sub DateTimePicker_ValueChanged( ByVal sender As 对象, ByVal e As EventArgs)句柄 DTP_siswa.ValueChanged DTP_siswa.Format = DateTimePickerFormat.Custom DTP_siswa.ShowUpDown = True DTP_siswa.Value = 日期 .Now.ToString( dd-MM-yyyy) 结束 Sub 结束 类 我的问题是: 当我用f5运行程序并将日期时间选择器更改为程序停止的另一个日期时,并在上面的阻塞行中说InvalidCastException未处理。我不知道错误在哪里,我一直在努力解决它,但它没有用。 是表格编码,类编码中的错误,还是SQL服务器? ------------------------------------- ---------------------------------------------- 这里是该类的编码: 类连接: 公共类ClassKoneksi 私有共享CN为SqlClient.SqlConnection 私有共享_NamaDatabase为字符串 公共共享属性namadatabase为字符串获取返回_NamaDatabase 结束获取 Set(value As String) _NamaDatabase = value 结束集结束物业 公共共享函数opencon()As SqlClient.SqlConnection CN = New SqlClient.SqlConnection CN.ConnectionString =Data Source = RICKY-PC\LENOVOSQL; Initial Catalog = KPIRWAN; INTE grated Security = True CN.Open()返回CN 结束函数 公共共享子closecon() CN.Close() CN.ConnectionString = Nothing End Sub 班级学生: 公共类ClassSiswa 继承ClassKoneksi 私有共享_Nis,_Nisn,_Berat_Badan,_Tinggi_Badan为整数$ b $ b私有共享_Nama_Siswa,_ _ Jenis_Kelamin,_Kota_Lahir,_Agama为字符串私有共享_Tanggal_Lahir作为日期公共共享属性Nis()作为整数获取返回_Nis 结束获取设置(ByVal值为整数) _Nis = value 结束集结束属性公共共享属性Nisn()为整数获取返回_Nisn 结束获取设置(ByVal值为整数) _Nisn = value 结束集结束支柱erty 公共共享属性Berat_Badan()As Integer 获取返回_Berat_Badan 结束获取设置(ByVal值为整数) _Berat_Badan = value 结束集结束物业公共共享物业Tinggi_Badan()作为整数获取返回_Tinggi_Badan 结束获取设定(ByVal值为整数) _Tinggi_Badan = value 结束集结束物业公共共享物业Nama_Siswa()As String 获取返回_Nama_Siswa 结束获取 Set(ByVal value As String) _Nama_Siswa = value End Set End Property Public Shared Property Jenis_Kelamin()As String Get Return _Jenis_Kelamin 结束获取设置(ByVal值为字符串) _Jenis_Kelamin = value 结束集结束房产公共共享房产Kota_Lahir()字符串获取返回_Kota_Lahir 结束获取设置(ByVal值为字符串) _Kota_Lahir =价值结束集结束物业公共共享物业Tanggal_Lahir()作为日期获取返回_Tanggal_Lahir 结束获取 Set( ByVal值As Date) _Tanggal_Lahir = value 结束集结束物业公共共享物业Agama()As String 获取返回_Agama End Get Set(ByVal value As String) _Agama = value End Set End Property Public Shared Sub RekamData(ByVal _Cn As SqlClient.SqlConnection) Dim sql As SqlClient.SqlCommand sql = New SqlClient.SqlCommand sql.Connection = _Cn sql.CommandType = CommandType .Text sql.CommandText =插入datasiswa值('& Nis& ','& Nisn& ','& Nama_Siswa& ','& Jenis_Kelamin& ','& Kota_Lahir& ','& Tanggal_Lahir& ','& Agama& ','& Berat_Badan& ','& Tinggi_Badan& ') sql.ExecuteNonQuery() End Sub 公共共享函数displayData(ByVal _cn As SqlClient.SqlConnection,ByVal _sql As String,ByVal _datasiswa As String)As DataSet Dim _da As New SqlClient.SqlDataAdapter Dim _dsdata As New DataSet Dim sql As New SqlClient.SqlCommand _dsdata.Clear() sql.Connection = _cn sql.CommandType = CommandType.Text sql.CommandText = _sql _da.SelectCommand = sql _da.Fill(_dsdata,_datasiswa)返回_dsdata 结束函数结束班级 我很抱歉给您带来不便,但我真的很紧张过去几天试图解决这个问题。解决方案 你为什么要这样做? DTP_siswa.Value = 日期 .Now.ToString( dd-MM-yyyy) 假设DTP_siswa是你的DateTimePicker,那么Value属性是一个DateTime值,但Date.Now是一个VB6函数,它不会返回.Net上下文中特别有用的东西。 为什么要将日期值格式化为字符串以设置值一个日期时间?如果你这样做,那么系统必须解析它以将其转换为真正的DateTime。 试试这个: DTP_siswa.Value = DateTime.Now 然后查看其余的代码。 DateTimePicker 的值属性为 DateTime type。 那你为什么要给它分配一个字符串呢?这是第一个问题。 你永远不应该用字符串处理日期时间。只有在想要将日期时间呈现给用户时才使用日期时间的字符串表示。在内部,存储和计算应该坚持 DateTime 类型。 然后,还有另一个问题:当 ValueChanged 事件被触发,您只是尝试用当前日期/时间( DateTime.Now )替换所选值。你确定它真的是你想要做的吗?如果只是用当前时间替换选定的值,那么拥有DateTimePicker有什么意义呢?您应该在DateTime变量中获取所选值并尝试对其执行某些操作。 Hello, i am a beginner at programming and i want to ask how to make this program i am making right now work.Currently for my college assignment i was asked to make a application to register new students at a middle school using visual studio 2012 and microsoft sql server 2012.this is the code i'm currently working on: form:Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ClassKoneksi.namadatabase = "KPIRWAN" ClassSiswa.namadatabase = "KPIRWAN" Dim dsdata As New DataSet Dim sql As String sql = "select*from datasiswa" dsdata = ClassSiswa.displayData(ClassSiswa.opencon, sql, "Datasiswa") DataGridView1.DataSource = dsdata DataGridView1.DataMember = "datasiswa" DataGridView1.ReadOnly = True ClassSiswa.closecon() End Sub Private Sub btnKeluar_Click(sender As Object, e As EventArgs) Handles btnKeluar.Click Me.Close() End Sub Private Sub btnSimpan_Click(sender As Object, e As EventArgs) Handles btnSimpan.Click If Len(Trim(txt_nis.Text)) = 0 Or Len(Trim(txt_nisn.Text)) = 0 Or Len(Trim(txt_namasiswa.Text)) = 0 Or Len(Trim(cmb_kelaminsiswa.Text)) = 0 Or Len(Trim(txt_kotalahir.Text)) = 0 Or Len(Trim(DTP_siswa.Value)) = 0 Or Len(Trim(cmb_agamasiswa.Text)) = 0 Or Len(Trim(txt_beratsiswa.Text)) = 0 Or Len(Trim(txt_tinggisiswa.Text)) = 0 Then MsgBox("Data Belum Lengkap") Exit Sub End If ClassSiswa.Nis = txt_nis.Text ClassSiswa.Nisn = txt_nisn.Text ClassSiswa.Nama_Siswa = txt_namasiswa.Text ClassSiswa.Jenis_Kelamin = cmb_kelaminsiswa.Text ClassSiswa.Kota_Lahir = txt_kotalahir.Text ClassSiswa.Tanggal_Lahir = DTP_siswa.Value ClassSiswa.Agama = cmb_agamasiswa.Text ClassSiswa.Berat_Badan = txt_beratsiswa.Text ClassSiswa.Tinggi_Badan = txt_tinggisiswa.Text ClassSiswa.RekamData(ClassSiswa.opencon) MsgBox("Data siswa Berhasil Disimpan") End Sub Private Sub DateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DTP_siswa.ValueChanged DTP_siswa.Format = DateTimePickerFormat.Custom DTP_siswa.ShowUpDown = True DTP_siswa.Value = Date.Now.ToString("dd-MM-yyyy") End SubEnd Classmy question is:when i was running the program with f5 and changing the date time picker to another date the program stopped and said "InvalidCastException was unhandled" in the blocked line above. I don't know where the error is, i keep trying to fix it but it didn't work.is the error in the form coding, class coding, or the SQL server?-----------------------------------------------------------------------------------here are the coding of the class:class connection:Public Class ClassKoneksi Private Shared CN As SqlClient.SqlConnection Private Shared _NamaDatabase As String Public Shared Property namadatabase As String Get Return _NamaDatabase End Get Set(value As String) _NamaDatabase = value End Set End Property Public Shared Function opencon() As SqlClient.SqlConnection CN = New SqlClient.SqlConnection CN.ConnectionString = "Data Source=RICKY-PC\LENOVOSQL;Initial Catalog=KPIRWAN;Integrated Security=True " CN.Open() Return CN End Function Public Shared Sub closecon() CN.Close() CN.ConnectionString = Nothing End Subclass student:Public Class ClassSiswa Inherits ClassKoneksi Private Shared _Nis, _Nisn, _Berat_Badan, _Tinggi_Badan As Integer Private Shared _Nama_Siswa, _Jenis_Kelamin, _Kota_Lahir, _Agama As String Private Shared _Tanggal_Lahir As Date Public Shared Property Nis() As Integer Get Return _Nis End Get Set(ByVal value As Integer) _Nis = value End Set End Property Public Shared Property Nisn() As Integer Get Return _Nisn End Get Set(ByVal value As Integer) _Nisn = value End Set End Property Public Shared Property Berat_Badan() As Integer Get Return _Berat_Badan End Get Set(ByVal value As Integer) _Berat_Badan = value End Set End Property Public Shared Property Tinggi_Badan() As Integer Get Return _Tinggi_Badan End Get Set(ByVal value As Integer) _Tinggi_Badan = value End Set End Property Public Shared Property Nama_Siswa() As String Get Return _Nama_Siswa End Get Set(ByVal value As String) _Nama_Siswa = value End Set End Property Public Shared Property Jenis_Kelamin() As String Get Return _Jenis_Kelamin End Get Set(ByVal value As String) _Jenis_Kelamin = value End Set End Property Public Shared Property Kota_Lahir() As String Get Return _Kota_Lahir End Get Set(ByVal value As String) _Kota_Lahir = value End Set End Property Public Shared Property Tanggal_Lahir() As Date Get Return _Tanggal_Lahir End Get Set(ByVal value As Date) _Tanggal_Lahir = value End Set End Property Public Shared Property Agama() As String Get Return _Agama End Get Set(ByVal value As String) _Agama = value End Set End Property Public Shared Sub RekamData(ByVal _Cn As SqlClient.SqlConnection) Dim sql As SqlClient.SqlCommand sql = New SqlClient.SqlCommand sql.Connection = _Cn sql.CommandType = CommandType.Text sql.CommandText = "Insert into datasiswa values('" & Nis & "','" & Nisn & "','" & Nama_Siswa & "','" & Jenis_Kelamin & "','" & Kota_Lahir & "','" & Tanggal_Lahir & "','" & Agama & "','" & Berat_Badan & "','" & Tinggi_Badan & "')" sql.ExecuteNonQuery() End Sub Public Shared Function displayData(ByVal _cn As SqlClient.SqlConnection, ByVal _sql As String, ByVal _datasiswa As String) As DataSet Dim _da As New SqlClient.SqlDataAdapter Dim _dsdata As New DataSet Dim sql As New SqlClient.SqlCommand _dsdata.Clear() sql.Connection = _cn sql.CommandType = CommandType.Text sql.CommandText = _sql _da.SelectCommand = sql _da.Fill(_dsdata, _datasiswa) Return _dsdata End FunctionEnd Classi'm sorry for the inconvenience but,i'm really stressed out for the past few days trying to figure this thing out. 解决方案 Why are you doing this?DTP_siswa.Value = Date.Now.ToString("dd-MM-yyyy")Assuming that DTP_siswa is your DateTimePicker, then the Value property is a DateTime value, but Date.Now is a VB6 function, which doesn't return anything particularly useful in a .Net context.And why format a date value as a string in order to set the value of a DateTime at all? If you do, the system then had to parse that to convert it to a "real" DateTime anyway.Try this:DTP_siswa.Value = DateTime.NowAnd then look at the rest of your code.DateTimePicker's Value property is of DateTime type.So why are you trying to assign it a string? That's the first problem.You should never handle datetimes with strings. Only use a string representation of the datetime when you want to present it to the user. Internally, storage and computations should stick to DateTime type.Then, there is another problem: when the ValueChanged event is fired, you just try to replace the selected value with the current date/time (DateTime.Now). Are you sure it is really what you want to do? What is the point of having a DateTimePicker if you just replace the selected value with the current time? You should get the selected value in a DateTime variable and try to do something with it. 这篇关于日期时间选择器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 03:18