i came up with a quite annoying problem in inserting data into the database in vb.net using the textfields. i tried out with many different ways,but could not sort out this problem.

i used datatables,dataadapters,datasets.Plz if anyone can help me out.

The condition is :

i am using four tables. and in one table the primary key which is auto increment is a FK in the table that i actually use for storing the inserted the data in the textfields.


here is where i am getting the problem:


cn.Open()
str = "insert into tbl_emps(empname,empsal,desgid) values('" & t1.Text & "'," & (t2.Text) & "," & (t3.Text) & ")"
da.Fill(ds, "tbl_emp")
dt.TableName = "tbl_emp"
cn.Close()

is it correct.


2nd Type

cn.Open()
str = "insert into tbl_emps(empname,empsal,desgid) values('"
& t1.Text & "'," & (t2.Text) & "," & (t3.Text) & ")"
cmd.commandtext=str
cmd.executenonquery()

The problem that i get is :

ExecuteReader: CommandText property has not been initialized

Add to the code has dim dr as (sql.datareader=cmd.executereader)i hope this statement --works out,,,,

You have to indicate what type of command you are using. This could be got from the commandtype.property. Indicate that it is text and not a storedproc or others. It should work.

Get back if it doesn't
:)

strSql = "insert into new_leaveapplication values('" & Me.ddlLeaveType.SelectedValue.ToString & "'," & Me.txtLeaveRemainig.Text.Trim & ",'" & Me.txtEmpId.Text.Trim & "','" & Me.txtLeaveRequestOn.Text.Trim & "','" & Me.txtLeaveFrom.Text.Trim & "','" & Me.txtLeaveTo.Text.Trim & "','" & Me.txtTotalDays.Text.Trim & "','" & Me.txtLeaveReason.Text.Trim & "'," & Me.txtLeaveStatus.Text.Trim & ")" ddlleaveType is a dropdown please help me out whether its correct query or not...... string or binary data would be truncated. the statement has been terminated i am getting this error

i have two calendar controls i have to find ou total number of days between two dates which date is selected one from one control and another date from another control how to find total days between two using vb.net

`

Imports System.Drawing.Color
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Web.Security
Imports System.Web.UI
Imports System.Configuration
Imports System.Configuration.ConfigurationManager
Partial Class teachermgmt_frmNewTeacherLeaveApplication
    Inherits System.Web.UI.Page
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim strSql As String = ""
    Dim strcon As String = ConfigurationSettings.AppSettings("lildb")
    Dim connString As String = ConnectionStrings.Item("lildb").ToString
    Dim dbConnection As New SqlConnection(connString)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Dim EMPID As Integer
        'Dim EMPERN As String
        'If Me.IsPostBack = False Then
        '    Dim cookie As HttpCookie = Request.Cookies("login")
        '    If cookie Is Nothing Then
        '        cookie = New HttpCookie("login")
        '    End If
        '    If cookie("id") = "" Then
        '        Response.Redirect("../Default.aspx")
        '    End If
        '    If Session("admin_user_id") = "" Then
        '        Response.Redirect("../Default.aspx")
        '    End If
        '    If CheckCurrentPage(Session("admin_role")) = 0 Then
        '        'Response.Redirect("./frmNoRights.aspx")
        '    End If
        'EMPERN = Conversion.Int(Request.QueryString("emp_ern_number").ToString)
        'Session("emp_ern_number") = EMPERN
        'EMPID = Conversion.Int(Request.QueryString("emp_ern_number").ToString)
        'End If

        Me.ImagePrevious.Attributes.Add("onclick", "javascript:window.history.go(-1);return false;")
        Me.ImageNext.Attributes.Add("onclick", "javascript:window.history.go(1);return false;")
        reloadDetails()
    End Sub
    Sub reloadDetails()
        Dim leave_id As Integer = 1
        Dim slNo As Integer = 1
        strSql = "SELECT leave_type from leave_plan order by leave_type"
        If dbConnection.State = ConnectionState.Closed Then
            dbConnection.Open()
        End If
        Dim cmdSQL As New SqlCommand(strSql, dbConnection)
        Dim drSQL As SqlDataReader
        Dim mytxt As String = ""
        Dim myid As Integer = 1

        Try
            drSQL = cmdSQL.ExecuteReader
            ddlLeaveType.Items.Add(New ListItem("--Please Select One--", 0))
            If drSQL.HasRows Then
                While drSQL.Read()
                    ddlLeaveType.Items.Add(New ListItem(drSQL.Item(0).ToString.Trim, drSQL.Item(0).ToString.Trim))
                End While
                Me.DetailTable.Rows.Clear()
            Dim tableHeader As New TableRow

            Dim tableCell1 As New TableCell
            Dim tableCell2 As New TableCell
            Dim tableCell3 As New TableCell
            Dim tableCell4 As New TableCell
            Dim tableCell5 As New TableCell
            Dim tableCell6 As New TableCell
            Dim tableCell7 As New TableCell
            Dim tableCell8 As New TableCell
            Dim tableCell9 As New TableCell
            Dim tableCell10 As New TableCell

            tableCell1.Controls.Add(New LiteralControl("LEAVE TYPE"))
            tableCell1.Width = 40
            tableCell1.VerticalAlign = VerticalAlign.Top
            tableCell1.HorizontalAlign = HorizontalAlign.Center
            tableCell1.BackColor = AliceBlue
            tableCell2.Controls.Add(New LiteralControl("LEAVE REMAINING"))
            tableCell2.Width = 140
            tableCell2.HorizontalAlign = HorizontalAlign.Center
            tableCell2.BackColor = AliceBlue
            tableCell2.Wrap = True
            tableCell2.VerticalAlign = VerticalAlign.Top
            tableCell3.Controls.Add(New LiteralControl("ERN ID"))
            tableCell3.BackColor = AliceBlue
            tableCell3.Width = 50
            tableCell3.HorizontalAlign = HorizontalAlign.Center
            tableCell3.Wrap = False
            tableCell3.VerticalAlign = VerticalAlign.Top
            tableCell4.Controls.Add(New LiteralControl("LEAVE REUEST ON"))
            tableCell4.BackColor = AliceBlue
            tableCell4.Width = 80
            tableCell4.HorizontalAlign = HorizontalAlign.Center
            tableCell4.Wrap = False
            tableCell4.VerticalAlign = VerticalAlign.Top
            tableCell5.Controls.Add(New LiteralControl("LEAVE FROM"))
            tableCell5.BackColor = AliceBlue
            tableCell5.Width = 80
            tableCell5.HorizontalAlign = HorizontalAlign.Center
            tableCell5.Wrap = False
            tableCell5.VerticalAlign = VerticalAlign.Top
            tableCell6.Controls.Add(New LiteralControl("LEAVE TO"))
            tableCell6.BackColor = AliceBlue
            tableCell6.Width = 80
            tableCell6.HorizontalAlign = HorizontalAlign.Center
            tableCell6.Wrap = False
            tableCell6.VerticalAlign = VerticalAlign.Top
            tableCell7.Controls.Add(New LiteralControl("TOTAL DAYS"))
            tableCell7.BackColor = AliceBlue
            tableCell7.Width = 80
            tableCell7.HorizontalAlign = HorizontalAlign.Center
            tableCell7.Wrap = False
            tableCell7.VerticalAlign = VerticalAlign.Top
            tableCell8.Controls.Add(New LiteralControl("LEAVE REASON"))
            tableCell8.BackColor = AliceBlue
            tableCell8.Width = 80
            tableCell8.HorizontalAlign = HorizontalAlign.Center
            tableCell8.Wrap = False
            tableCell8.VerticalAlign = VerticalAlign.Top
            tableCell9.Controls.Add(New LiteralControl("LEAVE STATUS"))
            tableCell9.BackColor = AliceBlue
            tableCell9.Width = 80
            tableCell9.HorizontalAlign = HorizontalAlign.Center
            tableCell9.Wrap = False
            tableCell9.VerticalAlign = VerticalAlign.Top
            tableCell10.Controls.Add(New LiteralControl("QUERY"))
            tableCell10.BackColor = AliceBlue
            tableCell10.Width = 80
            tableCell10.HorizontalAlign = HorizontalAlign.Center
            tableCell10.Wrap = False
            tableCell10.VerticalAlign = VerticalAlign.Top
            tableHeader.Cells.Add(tableCell1)
            tableHeader.Cells.Add(tableCell2)
            tableHeader.Cells.Add(tableCell3)
            tableHeader.Cells.Add(tableCell4)
            tableHeader.Cells.Add(tableCell5)
            tableHeader.Cells.Add(tableCell6)
            tableHeader.Cells.Add(tableCell7)
            tableHeader.Cells.Add(tableCell8)
            tableHeader.Cells.Add(tableCell9)
            tableHeader.Cells.Add(tableCell10)
            Me.DetailTable.Rows.Add(tableHeader)
            While drSQL.Read()
                Dim r As New TableRow
                Dim i As Integer = 1
                Dim c1 As New TableCell
                Dim c2 As New TableCell
                Dim c3 As New TableCell
                Dim c4 As New TableCell
                Dim c5 As New TableCell
                Dim c6 As New TableCell
                Dim c7 As New TableCell
                Dim c8 As New TableCell
                Dim c9 As New TableCell
                Dim c10 As New TableCell

                Dim btnDelete As New Button
                Dim btnEdit As New Button
                btnDelete.ID = "dele" & drSQL.Item(0).ToString
                btnDelete.Text = "Delete"
                btnDelete.Width = 50
                btnDelete.Attributes.Add("onclick", "return ShowConfirmation();")

                AddHandler btnDelete.Click, AddressOf btnLog_Click
                btnEdit.ID = "edit" & drSQL.Item(0).ToString
                btnEdit.Text = "Edit"
                btnEdit.Width = 50
                AddHandler btnEdit.Click, AddressOf btnLog_Click

                c1.Controls.Add(New LiteralControl(drSQL.Item(1).ToString))
                c2.HorizontalAlign = HorizontalAlign.Left
                c1.Wrap = True
                c1.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c1)
                c2.Controls.Add(New LiteralControl(drSQL.Item(2).ToString))
                c2.HorizontalAlign = HorizontalAlign.Left
                c2.Wrap = True
                c2.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c2)
                c3.Controls.Add(New LiteralControl(drSQL.Item(3).ToString))
                c3.HorizontalAlign = HorizontalAlign.Left
                c3.Wrap = True
                c3.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c3)
                c4.Controls.Add(New LiteralControl(drSQL.Item(4).ToString))
                c4.HorizontalAlign = HorizontalAlign.Left
                c4.Wrap = True
                c4.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c4)
                c5.Controls.Add(New LiteralControl(drSQL.Item(5).ToString))
                c5.HorizontalAlign = HorizontalAlign.Left
                c5.Wrap = True
                c5.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c5)
                c6.Controls.Add(New LiteralControl(drSQL.Item(6).ToString))
                c6.HorizontalAlign = HorizontalAlign.Left
                c6.Wrap = True
                c6.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c6)
                c7.Controls.Add(New LiteralControl(drSQL.Item(7).ToString))
                c7.HorizontalAlign = HorizontalAlign.Left
                c7.Wrap = True
                c7.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c7)
                c8.Controls.Add(New LiteralControl(drSQL.Item(8).ToString))
                c8.HorizontalAlign = HorizontalAlign.Left
                c8.Wrap = True
                c8.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c8)
                c9.Controls.Add(New LiteralControl(drSQL.Item(9).ToString))
                c9.HorizontalAlign = HorizontalAlign.Left
                c9.Wrap = True
                c9.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c9)

                c10.Controls.Add(btnEdit)
                c10.Controls.Add(btnDelete)
                c10.HorizontalAlign = HorizontalAlign.Left
                c10.VerticalAlign = VerticalAlign.Top
                c10.Wrap = False
                c10.VerticalAlign = VerticalAlign.Top
                r.Cells.Add(c10)
                Me.DetailTable.Rows.Add(r)
                slNo = slNo + 1
            End While
            Else
            End If

            If slNo > 0 Then
                DetailTable.Visible = True
            End If
        Catch ex As System.Exception
            LabelError.Text = ex.ToString
        End Try

                drSQL.Close()
                cmdSQL.Dispose()
                If dbConnection.State = ConnectionState.Open Then
                    dbConnection.Close()
                End If
    End Sub
    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        Dim myDate As String = ""
        myDate = Calendar1.SelectedDate.ToShortDateString()
        txtLeaveRequestOn.Text = myDate.Replace("-", "/")
        Dim div As System.Web.UI.Control = Page.FindControl("divCalendar")
        If TypeOf div Is HtmlGenericControl Then
            CType(div, HtmlGenericControl).Style.Add("display", "none")
        End If
    End Sub
    Protected Sub Calendar2_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar2.SelectionChanged
        Dim myDate1 As String = ""
        myDate1 = Calendar2.SelectedDate.ToShortDateString()
        txtLeaveFrom.Text = myDate1.Replace("-", "/")
        Dim div As System.Web.UI.Control = Page.FindControl("divCalendar1")
        If TypeOf div Is HtmlGenericControl Then
            CType(div, HtmlGenericControl).Style.Add("display", "none")
        End If
    End Sub
    Protected Sub Calendar3_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar3.SelectionChanged
        Dim myDate2 As String = ""
        myDate2 = Calendar3.SelectedDate.ToShortDateString()
        txtLeaveTo.Text = myDate2.Replace("-", "/")
        Dim div As System.Web.UI.Control = Page.FindControl("divCalendar2")
        If TypeOf div Is HtmlGenericControl Then
            CType(div, HtmlGenericControl).Style.Add("display", "none")
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Me.ddlLeaveType.Items.Clear()
        Me.txtLeaveRemainig.Text = ""
        Me.txtEmpId.Text = ""
        Me.txtLeaveRequestOn.Text = ""
        Me.txtLeaveFrom.Text = ""
        Me.txtLeaveTo.Text = ""
        Me.txtTotalDays.Text = ""
        Me.txtLeaveReason.Text = ""
        Me.txtLeaveStatus.Text = ""
        Me.LabelError.Text = ""
        reloadDetails()
    End Sub
    Protected Sub ImageMainControlPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageMainControlPanel.Click
        'If Session("admin_role") = "STUDENT" Or Session("admin_role") = "TEACHER" Then
        '    Exit Sub
        'End If
        Response.Redirect("../myControlPanel.aspx")
    End Sub
    Protected Sub ImageStudentControlPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageStudentControlPanel.Click
        Response.Redirect("../frmStudentControlPanel.aspx")
    End Sub
    Protected Sub ImageTeacherControlPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageTeacherControlPanel.Click
        Response.Redirect("../frmTeacherControlPanel.aspx")
    End Sub
    Protected Sub ImageFeesControlPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageFeesControlPanel.Click
        Response.Redirect("../feesmgmt/frmSelectStudent.aspx")
    End Sub
    Protected Sub ImageInvoicePanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageInvoicePanel.Click
        Response.Redirect("../reports/frmAllReports.aspx")
    End Sub
    Protected Sub ImageStudentStandardPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageStudentStandardPanel.Click
        Response.Redirect("../studentinfo/frmStudentStandard.aspx")
    End Sub
    Protected Sub ImageEvents_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageEvents.Click
        Response.Redirect("../frmNewEvents.aspx")
    End Sub
    Protected Sub ImageLogOut_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageLogOut.Click
        Response.Redirect("../Default.aspx")
    End Sub
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim success As Integer = 0
        Dim myReason As String = ""
        Dim myStatus As String = ""

        If Session("admin_role") = "STUDENT" Or Session("admin_role") = "TEACHER" Then
            Exit Sub
        End If
        If Me.ddlLeaveType.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please select the Leave Type..."
            Exit Sub
        End If
        If Me.txtLeaveRequestOn.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please select the Date..."
            Exit Sub
        End If
        If Me.txtLeaveFrom.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please select the Date..."
            Exit Sub
        End If
        If Me.txtLeaveTo.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please select the Date..."
            Exit Sub
        End If
        If Me.txtLeaveReason.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please enter the Reason..."
            Exit Sub
        End If
        If Me.txtLeaveStatus.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please enter the Status..."
            Exit Sub
        End If
        If dbConnection.State = ConnectionState.Closed Then
            dbConnection.Open()
        End If

        strSql = "insert into new_leaveapplication values('" & Me.ddlLeaveType.SelectedValue.ToString & "'," & Me.txtLeaveRemainig.Text.Trim & ",'" & Me.txtEmpId.Text.Trim & "','" & Me.txtLeaveRequestOn.Text.Trim & "','" & Me.txtLeaveFrom.Text.Trim & "','" & Me.txtLeaveTo.Text.Trim & "','" & Me.txtTotalDays.Text.Trim & "','" & Me.txtLeaveReason.Text.Trim & "'," & Me.txtLeaveStatus.Text.Trim & ")"

        Dim cmdSQL As New SqlCommand(strSql, dbConnection)
        Try
            success = cmdSQL.ExecuteNonQuery()
            cmdSQL.Dispose()
            If dbConnection.State = ConnectionState.Open Then
                dbConnection.Close()
            End If
        Catch ex As System.Exception
            ' errorrow.Visible = True
            LabelError.Text = ex.ToString
        End Try
        If success = 1 Then
            ' errorrow.Visible = True
            LabelError.Text = "Successfully saved"
        Else
            ' errorrow.Visible = True
            'LabelError.Text = "Not Saved Due To Some Unknown Error"
        End If
        Response.Write(strSql)
        'Response.Redirect("../teachermgmt/SuccessPage.aspx?success=" & success)
    End Sub
    Public Sub btnLog_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim MyId As String
        Dim MyCommand As String
        Dim achievementId As Integer = 0
        MyId = DirectCast(sender, Button).ID
        MyCommand = DirectCast(sender, Button).Text
        MyId = MyId.Substring(4)
        achievementId = Conversion.Val(MyId)
        ddlLeaveType.Text = achievementId
        If MyCommand = "Edit" Then
            EditDetails(achievementId)
        End If
        If MyCommand = "Delete" Then
            DeleteDetails(achievementId)
        End If
        reloadDetails()
        '  Page.Response.Redirect(Page.Request.Url.ToString(), True)
    End Sub
    Public Sub EditDetails(ByVal myId As Integer)
        Dim strSql As String
        Dim dbConnection As New SqlConnection(connString)

        strSql = "SELECT * from new_leaveapplication where leave_id =" & myId

        If dbConnection.State = ConnectionState.Closed Then
            dbConnection.Open()
        End If
        Dim cmdSQL As New SqlCommand(strSql, dbConnection)
        Dim drSQL As SqlDataReader
        drSQL = cmdSQL.ExecuteReader
        If drSQL.HasRows Then
            While drSQL.Read()
                ddlLeaveType.Text = drSQL.Item(1).ToString()
                txtLeaveRemainig.Text = drSQL.Item(2).ToString()
                txtEmpId.Text = drSQL.Item(3).ToString()
                txtLeaveRequestOn.Text = drSQL.Item(4).ToString()
                txtLeaveFrom.Text = drSQL.Item(5).ToString()
                txtLeaveTo.Text = drSQL.Item(6).ToString()
                txtTotalDays.Text = drSQL.Item(7).ToString()
                txtLeaveReason.Text = drSQL.Item(8).ToString()
                txtLeaveStatus.Text = drSQL.Item(9).ToString()


            End While
        Else
        End If
        drSQL.Close()
        cmdSQL.Dispose()
        If dbConnection.State = ConnectionState.Open Then
            dbConnection.Close()
        End If
    End Sub
    Public Sub DeleteDetails(ByVal myId As Integer)
        Dim strSql As String
        Dim dbConnection As New SqlConnection(connString)
        Dim success As Integer
        strSql = "Delete from new_leaveapplication where leave_id =" & myId

        If dbConnection.State = ConnectionState.Closed Then
            dbConnection.Open()
        End If
        Dim cmdSQL As New SqlCommand(strSql, dbConnection)
        Try
            success = cmdSQL.ExecuteNonQuery
        Catch ex As System.Exception
            'errorrow.Visible = True
            LabelError.Text = ex.ToString
        End Try
        cmdSQL.Dispose()
        If dbConnection.State = ConnectionState.Open Then
            dbConnection.Close()
        End If
        If success = 1 Then
            LabelError.Text = "Deletion is Successfull..."
        Else
        End If
    End Sub
End Class

but inserting part is not working its showing this error:string or binary data would be truncated. the statement has been terminated i am getting this error and even have to find i have two calendar controls i have to find ou total number of days between two dates which date is selected one from one control and another date from another control how to find total days between two using vb.net

Greetings, it's easy just make your texarea a server control like this <textarea id="t1" runat ="server" /> and please remove "rows" and "col" and your code should look like this

 str = "insert into tbl_emps(empname,empsal,desgid) values('" 
 & t1.value & "'," & (t2.value) & "," & (t3.value) & ")"

Thanks nduduzo but i am using that col and rows for dynamic display its not related to database its just display what i am fetching from data base........
thanks a lot i will try this ang get back to u in short............

When using text/strings in a sql string, always wrap the values with ' '

Example:

Dim sqls As String = "INSERT INTO table(col1,col2,col3) VALUES ('" & text1.Text & "','" & text2.Text & "','" & text3.Text & "')"

If you are wanting to use a textbox value as a data type other than text, you can aways cast the string to the desired data type.

Example:

Dim sqls As String = "INSERT INTO table(col1,col2,col3) VALUES ('" & text1.Text & "'," & Cdbl(text2.Text) & "," & Cint(text3.Text) & ")"

This way, of course, is less tolerant of mistakes. I suggest casting and storing to a variable before binding to a statement string. I usually try something like:

Private Function TryConvertDecimal(ByVal input As String) As Decimal
    Try
        Dim dec As Decimal = CDec(input)
        Return dec
    Catch ex As Exception
        Return Nothing
    End Try
End Function

I will usually create one check function for each data type I am using. This way I can just check for null values (nothing) like so:

If TryConvertDecimal(text1.Text) IsNot Nothing Then
    'Do work'
Else
    MsgBox("There was a problem converting input data, check value: " & text1.Text)
End If

This will give your application a lot more fault tolerance.

Hope this helps!

BegginnerDev

Thanks BegginnerDev it really helped me out lot..............

`

Protected Sub Calendar2_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar2.SelectionChanged
        Dim myDate1 As String = ""
        myDate1 = Calendar2.SelectedDate.ToShortDateString()
        txtLeaveFrom.Text = myDate1.Replace("-", "/")
        Dim div As System.Web.UI.Control = Page.FindControl("divCalendar1")
        If TypeOf div Is HtmlGenericControl Then
            CType(div, HtmlGenericControl).Style.Add("display", "none")
        End If
    End Sub
    Protected Sub Calendar3_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar3.SelectionChanged
        Dim myDate2 As String = ""
        myDate2 = Calendar3.SelectedDate.ToShortDateString()
        txtLeaveTo.Text = myDate2.Replace("-", "/")
        Dim div As System.Web.UI.Control = Page.FindControl("divCalendar2")
        If TypeOf div Is HtmlGenericControl Then
            CType(div, HtmlGenericControl).Style.Add("display", "none")
        End If
    End Sub`
    I have this two date controls i want to find number of days between two days after selecting dates automatic it should clculate number of days between and display in a text box and make it false so that no one can edit that textbox

"Not Saved Due To Some Unknown Error" getting this error if i insert values to database can u help me please BegginnerDev

Can you please post a code snippet of the area the error is being thrown?

I have never seen that error thrown, possibly a custom error message?

ASP Code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="frmNewTeacherLeaveApplication.aspx.vb" Inherits="teachermgmt_frmNewTeacherLeaveApplication" %>

<!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 id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Welcome to Little Flowers School Official Portal...</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
   <script type="text/javascript" language="javascript">
       function OnClickC() {
           if (divCalendar.style.display == "none")
               divCalendar.style.display = "";
           else
               divCalendar.style.display = "none";
           return;
       }
       function OnClickC1() {
           if (divCalendar1.style.display == "none")
               divCalendar1.style.display = "";
           else
               divCalendar1.style.display = "none";
           return;
       }

       function OnClickC2() {
           if (divCalendar2.style.display == "none")
               divCalendar2.style.display = "";
           else
               divCalendar2.style.display = "none";
           return;
       }

       function ShowConfirmation() {
           if (confirm("Do you confirm?") == true) {
               return true;
           } else {

               return false;
           }


       }

    </script>


 </head>
<body>
    <form id="form1" runat="server">
       <div id="topheader">
        <div class="logo">
        </div>
           <div class="search_area">
            <div class="search_box">
                <label>
                    <input name="textfield" type="text" class="searchtextbox" />
                </label>
            </div>
            <div class="search_go">
                <div align="center">
                    <a href="#" class="go">GO</a></div>
            </div>
            <div class="search_box">
                » <a href="#" class="advancesearch">Advance Search</a></div>
        </div>
    </div>
    <div id="search_strip">
        <div class="freeregistration">
         </div>
    </div>
    <div id="body_area">
        <div class="left">
            <div class="left_menutop">
            </div>
            <div class="left_menu_area">
                <div align="right">
                    <a href="#" class="left_menu">Home</a><br />
                    <a href="../frmAboutUS.aspx" class="left_menu">About Us</a><br />
                    <a href="../frmOurAchievements.aspx" class="left_menu">Our Achievements</a><br />
                    <a href="../frmCurrentEvents.aspx" class="left_menu">Current Events</a><br />
                    <a href="../frmCalenderYear.aspx" class="left_menu">Calendar of Year</a><br />
                    <a href="../frmExamResult.aspx" class="left_menu">Exam Results</a><br />
                    <a href="../frmContactUs.aspx" class="left_menu">Contact us</a> <a href="../frmFAQs.aspx"
                        class="left_menu">Email Us</a>
                    <!-- <a href="feesmgmt/feesCollect.aspx" class="left_menu">Fess</a>-->
                    <br />
                </div>
            </div>
        </div>
        <div class="midarea">
            <div class="head">
             </div>
                <asp:Table ID="Table1" runat="server" Width="820px" BorderWidth="0px" HorizontalAlign="Left"
                    BackImageUrl="../images/controlpanel/bigpanel3.png" Height="600px" Style="margin-left: 0px"
                    CellPadding="4" CellSpacing="4">
                <asp:TableRow VerticalAlign="Top">
                    <asp:TableCell>
                        <asp:Label ID="Label1" runat="server" Text="New Leave Application..." Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="white" Width="200px"></asp:Label>
                    </asp:TableCell>
                </asp:TableRow>

                <asp:TableRow HorizontalAlign ="left" >
                    <asp:TableCell VerticalAlign="Top">
                        <asp:Table ID="Table4" runat="server" CellPadding="3" CellSpacing="3">
                                 <asp:TableRow>
                                 <asp:TableCell VerticalAlign="middle" HorizontalAlign="right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC">Leave Type</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"  > <asp:DropDownList ID="ddlLeaveType" runat="server"  Width="204px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small">
                                </asp:DropDownList> </asp:TableCell>
                      </asp:TableRow>
                      <asp:TableRow>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="1">No. Of Leaves Remaining</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"><asp:TextBox ID="txtLeaveRemainig" runat="server" Width="200px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small"></asp:TextBox>
                                </asp:TableCell>
                           </asp:TableRow>
                           <asp:TableRow> <%--runat="server" ID ="rowERNID" >--%>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="2">Ern Id</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"><asp:TextBox ID="txtEmpId" runat="server" Width="200px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small"></asp:TextBox>
                                </asp:TableCell>
                           </asp:TableRow>
                           <asp:TableRow>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="3">Leave Request On</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"><asp:TextBox ID="txtLeaveRequestOn" runat="server" Width="200px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small" ToolTip ="User Id should be first three character from First Name and first three character from Last name"></asp:TextBox>
                                <input id="Button3" type="button" value="..." onclick="OnClickC()" /><br />
                                            <div id="divCalendar" style="display: none; position: absolute">
                                                <asp:Calendar ID="Calendar1" runat="server" BorderWidth="2px" BackColor="White" Width="200px"
                                                    ForeColor="Black" Height="180px" Font-Size="8" Font-Names="Tahoma" BorderColor="#999999"
                                                    BorderStyle="Outset" DayNameFormat="Short" CellPadding="5" SelectedDate="1/1/0001" VisibleDate="1/1/0001">
                                                    <TodayDayStyle ForeColor="Black" BackColor="#CCCCCC"></TodayDayStyle>
                                                    <SelectorStyle BackColor="#CCCCCC"></SelectorStyle>
                                                    <NextPrevStyle VerticalAlign="Bottom"></NextPrevStyle>
                                                    <DayHeaderStyle Font-Size="7pt" Font-Bold="True" BackColor="#CCCCCC"></DayHeaderStyle>
                                                    <SelectedDayStyle Font-Bold="True" ForeColor="White" BackColor="#666666"></SelectedDayStyle>
                                                    <TitleStyle Font-Bold="True" BorderColor="Black" BackColor="#999999"></TitleStyle>
                                                    <WeekendDayStyle BackColor="#FFFFCC"></WeekendDayStyle>
                                                    <OtherMonthDayStyle ForeColor="#808080"></OtherMonthDayStyle>
                                                </asp:Calendar>
                                            </div>
                                </asp:TableCell>
                                </asp:TableRow>
                            <asp:TableRow>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="4">Leave From</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"><asp:TextBox ID="txtLeaveFrom" runat="server" Width="200px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small" ToolTip ="User Id should be first three character from First Name and first three character from Last name"></asp:TextBox>
                                <input id="Button1" type="button" value="..." onclick="OnClickC1()" /><br />
                                            <div id="divCalendar1" style="display: none; position: absolute">
                                                <asp:Calendar ID="Calendar2" runat="server" BorderWidth="2px" BackColor="White" Width="200px"
                                                    ForeColor="Black" Height="180px" Font-Size="8" Font-Names="Tahoma" BorderColor="#999999"
                                                    BorderStyle="Outset" DayNameFormat="Short" CellPadding="5" SelectedDate="1/1/0001" VisibleDate="1/1/0001">
                                                    <TodayDayStyle ForeColor="Black" BackColor="#CCCCCC"></TodayDayStyle>
                                                    <SelectorStyle BackColor="#CCCCCC"></SelectorStyle>
                                                    <NextPrevStyle VerticalAlign="Bottom"></NextPrevStyle>
                                                    <DayHeaderStyle Font-Size="7pt" Font-Bold="True" BackColor="#CCCCCC"></DayHeaderStyle>
                                                    <SelectedDayStyle Font-Bold="True" ForeColor="White" BackColor="#666666"></SelectedDayStyle>
                                                    <TitleStyle Font-Bold="True" BorderColor="Black" BackColor="#999999"></TitleStyle>
                                                    <WeekendDayStyle BackColor="#FFFFCC"></WeekendDayStyle>
                                                    <OtherMonthDayStyle ForeColor="#808080"></OtherMonthDayStyle>
                                                </asp:Calendar>
                                            </div>
                                </asp:TableCell>
                                </asp:TableRow>
                           <asp:TableRow>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="5">Leave To</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"><asp:TextBox ID="txtLeaveTo" runat="server" Width="200px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small"></asp:TextBox>
                                <input id="Button2" type="button" value="..." onclick="OnClickC2()" /><br />
                                            <div id="divCalendar2" style="display: none; position: absolute">
                                                <asp:Calendar ID="Calendar3" runat="server" BorderWidth="2px" BackColor="White" Width="200px"
                                                    ForeColor="Black" Height="180px" Font-Size="8" Font-Names="Tahoma" BorderColor="#999999"
                                                    BorderStyle="Outset" DayNameFormat="Short" CellPadding="5" SelectedDate="1/1/0001" VisibleDate="1/1/0001">
                                                    <TodayDayStyle ForeColor="Black" BackColor="#CCCCCC"></TodayDayStyle>
                                                    <SelectorStyle BackColor="#CCCCCC"></SelectorStyle>
                                                    <NextPrevStyle VerticalAlign="Bottom"></NextPrevStyle>
                                                    <DayHeaderStyle Font-Size="7pt" Font-Bold="True" BackColor="#CCCCCC"></DayHeaderStyle>
                                                    <SelectedDayStyle Font-Bold="True" ForeColor="White" BackColor="#666666"></SelectedDayStyle>
                                                    <TitleStyle Font-Bold="True" BorderColor="Black" BackColor="#999999"></TitleStyle>
                                                    <WeekendDayStyle BackColor="#FFFFCC"></WeekendDayStyle>
                                                    <OtherMonthDayStyle ForeColor="#808080"></OtherMonthDayStyle>
                                                </asp:Calendar>
                                            </div>
                                </asp:TableCell>
                      </asp:TableRow>
                    <asp:TableRow>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="6">Total Days</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"  ><asp:TextBox ID="txtTotalDays" runat="server" Width="200px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small"></asp:TextBox></asp:TableCell>
                      </asp:TableRow>
                      <asp:TableRow>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="7">Leave Reasons</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"  ><asp:TextBox ID="txtLeaveReason" runat="server" Width="200px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small"></asp:TextBox></asp:TableCell>
                      </asp:TableRow>
                      <asp:TableRow>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="8">Leave Status</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"  ><asp:TextBox ID="txtLeaveStatus" runat="server" Width="200px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small"></asp:TextBox></asp:TableCell>
                      </asp:TableRow>
                      <asp:TableRow Height="0px" HorizontalAlign="Center" ID="errorrow" Visible ="true" >
                                        <asp:TableCell HorizontalAlign="Center" VerticalAlign="Middle" Wrap="false" ColumnSpan="3">
                                            <asp:Label ID="LabelError" runat="server" Text="" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small" ForeColor ="red" ></asp:Label>
                                         </asp:TableCell>

                                    </asp:TableRow>
                      <asp:TableRow >
                      <asp:TableCell ></asp:TableCell>
                       <asp:TableCell VerticalAlign="Middle" HorizontalAlign="center" ></asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left" >
                                 <asp:Button ID="btnSubmit" runat="server" Text="Submit" Font-Bold ="true" Font-Names ="Tahoma" Width ="70px"></asp:Button>  
                                         <asp:Button ID="btnReset" runat="server" Text="Reset" Font-Bold ="true" Font-Names ="Tahoma" Width ="70px"></asp:Button>  
                                         <asp:Button ID="btnCancel" runat="server" Text="Cancel" Font-Bold ="true" Font-Names ="Tahoma" Width ="70px"></asp:Button>
                                 </asp:TableCell>
                       </asp:TableRow>

                    </asp:Table>


                    </asp:TableCell></asp:TableRow>
                    <asp:TableRow ID="TableRow1" runat ="server" ><asp:TableCell ID="TableCell1" runat ="server" HorizontalAlign ="Center"  >
                         <asp:Panel ID="Panel1" runat="server" Width="780px" Height="150px" ScrollBars="Vertical">
                         <asp:Table ID="DetailTable" runat="server" CellPadding="1" CellSpacing="1"
                                    ForeColor="Black" Visible ="false" Width ="455px"  >
                                    <asp:TableRow Height="10px">
                                        <asp:TableCell HorizontalAlign="Center" Wrap="false" BackColor="AliceBlue" Width ="40px"><b>LEAVE TYPE</b></asp:TableCell>
                                        <asp:TableCell HorizontalAlign="Center" Wrap="false" BackColor="AliceBlue"  Width ="150px"><b>LEAVE REMAINING</b></asp:TableCell>
                                       <asp:TableCell HorizontalAlign="Left" Wrap="false" BackColor="AliceBlue" Width ="50px"><b>ERN ID</b></asp:TableCell>
                                       <asp:TableCell HorizontalAlign="Left" Wrap="false" BackColor="AliceBlue" Width ="50px"><b>LEAVE REQUEST ON</b></asp:TableCell>
                                       <asp:TableCell HorizontalAlign="Left" Wrap="false" BackColor="AliceBlue" Width ="50px"><b>LEAVE FROM</b></asp:TableCell>
                                       <asp:TableCell HorizontalAlign="Left" Wrap="false" BackColor="AliceBlue" Width ="50px"><b>LEAVE TO</b></asp:TableCell>
                                       <asp:TableCell HorizontalAlign="Left" Wrap="false" BackColor="AliceBlue" Width ="50px"><b>TOTAL DAYS</b></asp:TableCell>
                                       <asp:TableCell HorizontalAlign="Left" Wrap="false" BackColor="AliceBlue" Width ="50px"><b>LEAVE REASON</b></asp:TableCell>
                                       <asp:TableCell HorizontalAlign="Left" Wrap="false" BackColor="AliceBlue" Width ="50px"><b>LEAVE STATUS</b></asp:TableCell>
                                        <asp:TableCell HorizontalAlign="Center" Wrap="false" BackColor="AliceBlue" Width="60px" 
                                            ToolTip="Please select one option to delete, edit "><b>QUERY</b></asp:TableCell>
                                        </asp:TableRow>
                                </asp:Table>
                        </asp:Panel>
                        </asp:TableCell></asp:TableRow>


                        <asp:TableRow ID="TableRow2" runat ="server" Height ="40px">
                        <asp:TableCell VerticalAlign ="Top" HorizontalAlign ="Center" >
                            <asp:Table ID="Table3" runat="server" CellPadding="0" CellSpacing="0"  Width ="450px">
                                <asp:TableRow VerticalAlign ="Bottom" HorizontalAlign ="Center"    >
                                    <asp:TableCell>
                                        <asp:ImageButton ID="ImageMainControlPanel" runat="server" ImageUrl="../images/shortcuts/wall_brick.png"
                                            ToolTip="Main Control Panel" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageStudentControlPanel" runat="server" ImageUrl="../images/shortcuts/student_panel.png"
                                            ToolTip="Student Control Panel" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageTeacherControlPanel" runat="server" ImageUrl="../images/shortcuts/door_in.png"
                                            ToolTip="Teacher Control Panel" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageFeesControlPanel" runat="server" ImageUrl="../images/shortcuts/fees_panel.png"
                                            ToolTip="Fees Control Panel" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageStudentStandardPanel" runat="server" ImageUrl="../images/shortcuts/student_satndard.png"
                                            ToolTip="Student Standard" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageInvoicePanel" runat="server" ImageUrl="../images/shortcuts/balance_sheet.png"
                                            ToolTip="Invoice" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageEvents" runat="server" ImageUrl="../images/shortcuts/trophy.png"
                                            ToolTip="Events" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageSearchPanel" runat="server"  Visible="false" ImageUrl="../images/shortcuts/findcam.png"
                                            ToolTip="Search" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImagePrevious" runat="server" ImageUrl="../images/shortcuts/arrow_left.png"
                                            ToolTip="Previous Page" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageNext" runat="server" ImageUrl="../images/shortcuts/arrow_right.png"
                                            ToolTip="Next Page" ></asp:ImageButton></asp:TableCell><asp:TableCell>
                                        <asp:ImageButton ID="ImageLogOut" runat="server" ImageUrl="../images/shortcuts/lock_unlock.png"
                                            ToolTip="Sign Out" PostBackUrl="~/Default.aspx" ></asp:ImageButton>
                                     </asp:TableCell>
                                </asp:TableRow>
                            </asp:Table>
                        </asp:TableCell>
                    </asp:TableRow> 
            </asp:Table>
        </div>
    </div>
    <div id="fotter">
        <div class="fotter_links">
            <div align="center">
                <a href="#" class="fotterlink">Home</a> | <a href="../frmAboutUS.aspx" class="fotterlink">
                    About Us</a> | <a href="../frmOurAchievements.aspx" class="fotterlink">Our Achievements</a>
                | <a href="../frmCurrentEvents.aspx" class="fotterlink">Current Events</a> | <a href="../frmCalenderYear.aspx"
                    class="fotterlink">Calendar of Year</a> | <a href="../frmExamResult.aspx" class="fotterlink">Exam Results
                </a>| <a href="../frmContactUs.aspx" class="fotterlink">Contact us</a> | <a href="../frmFAQs.aspx"
                    class="fotterlink">Email Us</a></div>
        </div>
        <div class="fotter_copyrights">
            <div align="center">
                © Copyright Little Flowers School, KGF, India. All Rights Reserved</div>
        </div>
        <div class="fotter_validation">
            <div align="center">
                <a href="#" target="_blank" class="xhtml">XHTML</a> <a href="#" target="_blank" class="css">
                    CSS</a><br />
            </div>
        </div>
        <div class="fotter_designed">
            <div align="center">
                Designed By : Vishleshak.com</div>
        </div>
    </div>
    </form>
</body>
</html>

VB Code
Imports System.Drawing.Color
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Web.Security
Imports System.Web.UI
Imports System.Configuration
Imports System.Configuration.ConfigurationManager
Partial Class teachermgmt_frmNewTeacherLeaveApplication
    Inherits System.Web.UI.Page
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim strSql As String = ""
    Dim strcon As String = ConfigurationSettings.AppSettings("lildb")
    Dim connString As String = ConnectionStrings.Item("lildb").ToString
    Dim dbConnection As New SqlConnection(connString)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Dim EMPID As Integer
        'Dim EMPERN As String
        'If Me.IsPostBack = False Then
        '    Dim cookie As HttpCookie = Request.Cookies("login")
        '    If cookie Is Nothing Then
        '        cookie = New HttpCookie("login")
        '    End If
        '    If cookie("id") = "" Then
        '        Response.Redirect("../Default.aspx")
        '    End If
        '    If Session("admin_user_id") = "" Then
        '        Response.Redirect("../Default.aspx")
        '    End If
        '    If CheckCurrentPage(Session("admin_role")) = 0 Then
        '        'Response.Redirect("./frmNoRights.aspx")
        '    End If
        'EMPERN = Conversion.Int(Request.QueryString("emp_ern_number").ToString)
        'Session("emp_ern_number") = EMPERN
        'EMPID = Conversion.Int(Request.QueryString("emp_ern_number").ToString)
        'End If

        Me.ImagePrevious.Attributes.Add("onclick", "javascript:window.history.go(-1);return false;")
        Me.ImageNext.Attributes.Add("onclick", "javascript:window.history.go(1);return false;")
        reloadDetails()
    End Sub
    Sub reloadDetails()
        Dim LeaveId As Integer = 1
        Dim xcolor As Integer
        strSql = "SELECT leave_type from leave_plan order by leave_type"
        If dbConnection.State = ConnectionState.Closed Then
            dbConnection.Open()
        End If
        Dim cmdSQL As New SqlCommand(strSql, dbConnection)
        Dim drSQL As SqlDataReader
        'Dim mytxt As String = ""
        'Dim myid As Integer = 1

        Try
            drSQL = cmdSQL.ExecuteReader
            ddlLeaveType.Items.Add(New ListItem("--Please Select One--", 0))
            If drSQL.HasRows Then
                While drSQL.Read()
                    ddlLeaveType.Items.Add(New ListItem(drSQL.Item(0).ToString.Trim, drSQL.Item(0).ToString.Trim))

                End While
                Me.DetailTable.Rows.Clear()
            Dim tableHeader As New TableRow

            Dim tableCell1 As New TableCell
            Dim tableCell2 As New TableCell
            Dim tableCell3 As New TableCell
            Dim tableCell4 As New TableCell
            Dim tableCell5 As New TableCell
            Dim tableCell6 As New TableCell
            Dim tableCell7 As New TableCell
            Dim tableCell8 As New TableCell
            Dim tableCell9 As New TableCell
            Dim tableCell10 As New TableCell

            tableCell1.Controls.Add(New LiteralControl("LEAVE TYPE"))
            tableCell1.Width = 40
            tableCell1.VerticalAlign = VerticalAlign.Top
            tableCell1.HorizontalAlign = HorizontalAlign.Center
            tableCell1.BackColor = AliceBlue
            tableCell2.Controls.Add(New LiteralControl("LEAVE REMAINING"))
            tableCell2.Width = 140
            tableCell2.HorizontalAlign = HorizontalAlign.Center
            tableCell2.BackColor = AliceBlue
            tableCell2.Wrap = True
            tableCell2.VerticalAlign = VerticalAlign.Top
            tableCell3.Controls.Add(New LiteralControl("ERN ID"))
            tableCell3.BackColor = AliceBlue
            tableCell3.Width = 50
            tableCell3.HorizontalAlign = HorizontalAlign.Center
            tableCell3.Wrap = False
            tableCell3.VerticalAlign = VerticalAlign.Top
            tableCell4.Controls.Add(New LiteralControl("LEAVE REUEST ON"))
            tableCell4.BackColor = AliceBlue
            tableCell4.Width = 80
            tableCell4.HorizontalAlign = HorizontalAlign.Center
            tableCell4.Wrap = False
            tableCell4.VerticalAlign = VerticalAlign.Top
            tableCell5.Controls.Add(New LiteralControl("LEAVE FROM"))
            tableCell5.BackColor = AliceBlue
            tableCell5.Width = 80
            tableCell5.HorizontalAlign = HorizontalAlign.Center
            tableCell5.Wrap = False
            tableCell5.VerticalAlign = VerticalAlign.Top
            tableCell6.Controls.Add(New LiteralControl("LEAVE TO"))
            tableCell6.BackColor = AliceBlue
            tableCell6.Width = 80
            tableCell6.HorizontalAlign = HorizontalAlign.Center
            tableCell6.Wrap = False
            tableCell6.VerticalAlign = VerticalAlign.Top
            tableCell7.Controls.Add(New LiteralControl("TOTAL DAYS"))
            tableCell7.BackColor = AliceBlue
            tableCell7.Width = 80
            tableCell7.HorizontalAlign = HorizontalAlign.Center
            tableCell7.Wrap = False
            tableCell7.VerticalAlign = VerticalAlign.Top
            tableCell8.Controls.Add(New LiteralControl("LEAVE REASON"))
            tableCell8.BackColor = AliceBlue
            tableCell8.Width = 80
            tableCell8.HorizontalAlign = HorizontalAlign.Center
            tableCell8.Wrap = False
            tableCell8.VerticalAlign = VerticalAlign.Top
            tableCell9.Controls.Add(New LiteralControl("LEAVE STATUS"))
            tableCell9.BackColor = AliceBlue
            tableCell9.Width = 80
            tableCell9.HorizontalAlign = HorizontalAlign.Center
            tableCell9.Wrap = False
            tableCell9.VerticalAlign = VerticalAlign.Top
            tableCell10.Controls.Add(New LiteralControl("QUERY"))
            tableCell10.BackColor = AliceBlue
            tableCell10.Width = 80
            tableCell10.HorizontalAlign = HorizontalAlign.Center
            tableCell10.Wrap = False
            tableCell10.VerticalAlign = VerticalAlign.Top
            tableHeader.Cells.Add(tableCell1)
            tableHeader.Cells.Add(tableCell2)
            tableHeader.Cells.Add(tableCell3)
            tableHeader.Cells.Add(tableCell4)
            tableHeader.Cells.Add(tableCell5)
            tableHeader.Cells.Add(tableCell6)
            tableHeader.Cells.Add(tableCell7)
            tableHeader.Cells.Add(tableCell8)
            tableHeader.Cells.Add(tableCell9)
            tableHeader.Cells.Add(tableCell10)
            Me.DetailTable.Rows.Add(tableHeader)
            While drSQL.Read()
                Dim r As New TableRow
                Dim i As Integer = 1
                Dim c1 As New TableCell
                Dim c2 As New TableCell
                Dim c3 As New TableCell
                Dim c4 As New TableCell
                Dim c5 As New TableCell
                Dim c6 As New TableCell
                Dim c7 As New TableCell
                Dim c8 As New TableCell
                Dim c9 As New TableCell
                    Dim c10 As New TableCell
                    Dim c11 As New TableCell

                Dim btnDelete As New Button
                Dim btnEdit As New Button
                btnDelete.ID = "dele" & drSQL.Item(0).ToString
                btnDelete.Text = "Delete"
                btnDelete.Width = 50
                btnDelete.Attributes.Add("onclick", "return ShowConfirmation();")

                AddHandler btnDelete.Click, AddressOf btnLog_Click
                btnEdit.ID = "edit" & drSQL.Item(0).ToString
                btnEdit.Text = "Edit"
                btnEdit.Width = 50
                AddHandler btnEdit.Click, AddressOf btnLog_Click



                    c1.Controls.Add(New LiteralControl(LeaveId))
                    r.Cells.Add(c1)

                    c2.Controls.Add(New LiteralControl(drSQL.Item(1).ToString))
                    c2.HorizontalAlign = HorizontalAlign.Left
                    c2.Wrap = True
                    c2.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c2)
                    c3.Controls.Add(New LiteralControl(drSQL.Item(2).ToString))
                    c3.HorizontalAlign = HorizontalAlign.Left
                    c3.Wrap = True
                    c3.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c3)
                    c4.Controls.Add(New LiteralControl(drSQL.Item(3).ToString))
                    c4.HorizontalAlign = HorizontalAlign.Left
                    c4.Wrap = True
                    c4.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c4)
                    c5.Controls.Add(New LiteralControl(drSQL.Item(4).ToString))
                    c5.HorizontalAlign = HorizontalAlign.Left
                    c5.Wrap = True
                    c5.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c5)
                    c6.Controls.Add(New LiteralControl(drSQL.Item(5).ToString))
                    c6.HorizontalAlign = HorizontalAlign.Left
                    c6.Wrap = True
                    c6.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c6)
                    c7.Controls.Add(New LiteralControl(drSQL.Item(6).ToString))
                    c7.HorizontalAlign = HorizontalAlign.Left
                    c7.Wrap = True
                    c7.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c7)
                    c8.Controls.Add(New LiteralControl(drSQL.Item(7).ToString))
                    c8.HorizontalAlign = HorizontalAlign.Left
                    c8.Wrap = True
                    c8.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c8)
                    c9.Controls.Add(New LiteralControl(drSQL.Item(8).ToString))
                    c9.HorizontalAlign = HorizontalAlign.Left
                    c9.Wrap = True
                    c9.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c9)
                    c10.Controls.Add(New LiteralControl(drSQL.Item(9).ToString))
                    c10.HorizontalAlign = HorizontalAlign.Left
                    c10.Wrap = True
                    c10.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c10)

                    c11.Controls.Add(btnEdit)
                    c11.Controls.Add(btnDelete)
                    c11.HorizontalAlign = HorizontalAlign.Left
                    c11.VerticalAlign = VerticalAlign.Top
                    c11.Wrap = False
                    c11.VerticalAlign = VerticalAlign.Top
                    r.Cells.Add(c11)
                    xcolor = LeaveId Mod 2
                    If xcolor = 0 Then
                        r.BackColor = Drawing.Color.MintCream
                    End If
                    r.ForeColor = BlueViolet
                Me.DetailTable.Rows.Add(r)
                    LeaveId = LeaveId + 1
            End While
            Else
            End If

            If LeaveId > 0 Then
                DetailTable.Visible = True
            End If
            drSQL.Close()
            cmdSQL.Dispose()

        Catch ex As System.Exception
            LabelError.Text = ex.ToString
        End Try

        If dbConnection.State = ConnectionState.Open Then
            dbConnection.Close()
        End If

    End Sub
    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        Dim myDate As String = ""
        myDate = Calendar1.SelectedDate.ToShortDateString()
        txtLeaveRequestOn.Text = myDate.Replace("-", "/")
        Dim div As System.Web.UI.Control = Page.FindControl("divCalendar")
        If TypeOf div Is HtmlGenericControl Then
            CType(div, HtmlGenericControl).Style.Add("display", "none")
        End If
    End Sub
    Protected Sub Calendar2_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar2.SelectionChanged
        Dim myDate1 As String = ""
        myDate1 = Calendar2.SelectedDate.ToShortDateString()
        txtLeaveFrom.Text = myDate1.Replace("-", "/")
        Dim div As System.Web.UI.Control = Page.FindControl("divCalendar1")
        If TypeOf div Is HtmlGenericControl Then
            CType(div, HtmlGenericControl).Style.Add("display", "none")
        End If
    End Sub
    Protected Sub Calendar3_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar3.SelectionChanged
        Dim myDate2 As String = ""
        myDate2 = Calendar3.SelectedDate.ToShortDateString()
        txtLeaveTo.Text = myDate2.Replace("-", "/")
        Dim div As System.Web.UI.Control = Page.FindControl("divCalendar2")
        If TypeOf div Is HtmlGenericControl Then
            CType(div, HtmlGenericControl).Style.Add("display", "none")
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Me.ddlLeaveType.Items.Clear()
        Me.txtLeaveRemainig.Text = ""
        Me.txtEmpId.Text = ""
        Me.txtLeaveRequestOn.Text = ""
        Me.txtLeaveFrom.Text = ""
        Me.txtLeaveTo.Text = ""
        Me.txtTotalDays.Text = ""
        Me.txtLeaveReason.Text = ""
        Me.txtLeaveStatus.Text = ""
        Me.LabelError.Text = ""
        reloadDetails()
    End Sub
    Protected Sub ImageMainControlPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageMainControlPanel.Click
        'If Session("admin_role") = "STUDENT" Or Session("admin_role") = "TEACHER" Then
        '    Exit Sub
        'End If
        Response.Redirect("../myControlPanel.aspx")
    End Sub
    Protected Sub ImageStudentControlPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageStudentControlPanel.Click
        Response.Redirect("../frmStudentControlPanel.aspx")
    End Sub
    Protected Sub ImageTeacherControlPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageTeacherControlPanel.Click
        Response.Redirect("../frmTeacherControlPanel.aspx")
    End Sub
    Protected Sub ImageFeesControlPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageFeesControlPanel.Click
        Response.Redirect("../feesmgmt/frmSelectStudent.aspx")
    End Sub
    Protected Sub ImageInvoicePanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageInvoicePanel.Click
        Response.Redirect("../reports/frmAllReports.aspx")
    End Sub
    Protected Sub ImageStudentStandardPanel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageStudentStandardPanel.Click
        Response.Redirect("../studentinfo/frmStudentStandard.aspx")
    End Sub
    Protected Sub ImageEvents_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageEvents.Click
        Response.Redirect("../frmNewEvents.aspx")
    End Sub
    Protected Sub ImageLogOut_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageLogOut.Click
        Response.Redirect("../Default.aspx")
    End Sub
    Public Sub btnLog_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim MyId As String
        Dim MyCommand As String
        Dim achievementId As Integer = 0
        MyId = DirectCast(sender, Button).ID
        MyCommand = DirectCast(sender, Button).Text
        MyId = MyId.Substring(4)
        achievementId = Conversion.Val(MyId)
        ddlLeaveType.Text = achievementId
        If MyCommand = "Edit" Then
            EditDetails(achievementId)
        End If
        If MyCommand = "Delete" Then
            DeleteDetails(achievementId)
        End If
        reloadDetails()
        '  Page.Response.Redirect(Page.Request.Url.ToString(), True)
    End Sub
    Public Sub EditDetails(ByVal myId As Integer)
        Dim strSql As String
        Dim dbConnection As New SqlConnection(connString)

        strSql = "SELECT * from new_leaveapplication where leave_id =" & myId

        If dbConnection.State = ConnectionState.Closed Then
            dbConnection.Open()
        End If
        Dim cmdSQL As New SqlCommand(strSql, dbConnection)
        Dim drSQL As SqlDataReader
        drSQL = cmdSQL.ExecuteReader
        If drSQL.HasRows Then
            While drSQL.Read()
                ddlLeaveType.Text = drSQL.Item(1).ToString()
                txtLeaveRemainig.Text = drSQL.Item(2).ToString()
                txtEmpId.Text = drSQL.Item(3).ToString()
                txtLeaveRequestOn.Text = drSQL.Item(4).ToString()
                txtLeaveFrom.Text = drSQL.Item(5).ToString()
                txtLeaveTo.Text = drSQL.Item(6).ToString()
                txtTotalDays.Text = drSQL.Item(7).ToString()
                txtLeaveReason.Text = drSQL.Item(8).ToString()
                txtLeaveStatus.Text = drSQL.Item(9).ToString()


            End While
        Else
        End If
        drSQL.Close()
        cmdSQL.Dispose()
        If dbConnection.State = ConnectionState.Open Then
            dbConnection.Close()
        End If
    End Sub
    Public Sub DeleteDetails(ByVal myId As Integer)
        Dim strSql As String
        Dim dbConnection As New SqlConnection(connString)
        Dim success As Integer
        strSql = "Delete from new_leaveapplication where leave_id =" & myId

        If dbConnection.State = ConnectionState.Closed Then
            dbConnection.Open()
        End If
        Dim cmdSQL As New SqlCommand(strSql, dbConnection)
        Try
            success = cmdSQL.ExecuteNonQuery
        Catch ex As System.Exception
            'errorrow.Visible = True
            LabelError.Text = ex.ToString
        End Try
        cmdSQL.Dispose()
        If dbConnection.State = ConnectionState.Open Then
            dbConnection.Close()
        End If
        If success = 1 Then
            LabelError.Text = "Deletion is Successfull..."
        Else
        End If
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim success As Integer = 0
        Dim transInsert As SqlTransaction

        **strSql = "insert into new_leaveapplication(leave_type,no_days_remaining,emp_id,leave_request_on,leave_from,leave_to,total_days,leave_reason,leave_status) values('" & Me.ddlLeaveType.SelectedValue & "','" & Me.txtLeaveRemainig.Text.Trim & "','" & Me.txtEmpId.Text.Trim & "','" & Me.txtLeaveRequestOn.Text.Trim & "','" & Me.txtLeaveFrom.Text.Trim & "','" & Me.txtLeaveTo.Text.Trim & "','" & Me.txtTotalDays.Text.Trim & "','" & Me.txtLeaveReason.Text.Trim & "','" & Me.txtLeaveStatus.Text.Trim & "')"**

        'If Session("admin_role") = "STUDENT" Or Session("admin_role") = "TEACHER" Then
        '    Exit Sub
        'End If
        If Me.ddlLeaveType.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please select the Leave Type..."
            Exit Sub
        End If
        If Me.txtLeaveRequestOn.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please select the Date..."
            Exit Sub
        End If
        If Me.txtLeaveFrom.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please select the Date..."
            Exit Sub
        End If
        If Me.txtLeaveTo.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please select the Date..."
            Exit Sub
        End If
        If Me.txtLeaveReason.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please enter the Reason..."
            Exit Sub
        End If
        If Me.txtLeaveStatus.Text.Trim = "" Then
            ' errorrow.Visible = True
            LabelError.Text = "*Please enter the Status..."
            Exit Sub
        End If

        Try
            If CommonMethods.dbConnection.State = ConnectionState.Closed Then
                CommonMethods.dbConnection.Open()
            End If

            transInsert = CommonMethods.dbConnection.BeginTransaction(IsolationLevel.ReadCommitted)

            Dim cmdSQL As New SqlCommand(strSql, CommonMethods.dbConnection, transInsert)

            success = cmdSQL.ExecuteNonQuery()

            transInsert.Commit()
            transInsert.Dispose()
            cmdSQL.Dispose()

        Catch ex As System.Exception
            errorrow.Visible = True
            LabelError.Text = ex.ToString
        End Try
        If success = 1 Then
            errorrow.Visible = True
            LabelError.Text = "Successfully saved"
        Else
            errorrow.Visible = True
            LabelError.Text = "Not Saved Due To Some Unknown Error"
        End If
        Response.Write(strSql)
        If CommonMethods.dbConnection.State = ConnectionState.Open Then
            CommonMethods.dbConnection.Close()
        End If
        reloadDetails()
        'Response.Redirect("../teachermgmt/SuccessPage.aspx?success=" & success)
    End Sub
End Class

here is the full code html and vb code so that its easy to find what i am exactly doing here i got problem with inserting part ehich i have marked in bold

Here is the file i am sending u which show u what error i am getting exactly

If your server is not set to allow execnonquery to return values, you could be getting a null value back from that.

You can try ExecuteScalar also to see if that returns the rows affected.

I too have had issues with ENQ not returning values.

success = cmdSQL.ExecuteNonQuery()

Change Textbox Value which depend on dropdown Based On Selected Dropdown Value using vb.net

function getDDVal(dd, txt) {

           var IndexVal = dd.selectedIndex;
           txt.value = dd.options[IndexVal].value;

       }

       <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"  > <asp:DropDownList ID="ddlLeaveType" runat="server" onchange="GetDDVal(this,txtLeaveRemaining)" "Width="204px" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small">
                                <asp:ListItem Selected ="True">Please select the leave---</asp:ListItem> 
                                </asp:DropDownList></asp:TableCell>


                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Right"  Font-Size="Small" Font-Bold="True"
                            Font-Italic="False" ForeColor="#3366CC" TabIndex="1">No. Of Leaves Remaining</asp:TableCell>
                                 <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Center">:</asp:TableCell>
                                <asp:TableCell VerticalAlign="Middle" HorizontalAlign="Left"><asp:TextBox ID="txtLeaveRemainig" runat="server" Width="200px" Enabled="false" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="Small" ></asp:TextBox>
                                </asp:TableCell>

I have a dropdown list and one text box i have to get value of text box from database on selecting value from dropdown it should display the value related to that value selected from dropdown list above function is showing the selected value from dropdown itself

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.