我收到此错误“错误将数据类型nvarchar转换为datetime。 "

问题描述:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="SalesOrder.aspx.cs" Inherits="SalesOrder" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

    <script type="text/javascript" language="javascript">
        function GetTotal() {
            var val1 = document.getElementById('<%=txtq.ClientID%>').value;
            var val2 = document.getElementById('<%=txtp.ClientID%>').value;
            if (val1 != "" && val2 != "") {
                document.getElementById('<%=txttotal.ClientID%>').value = val1 * val2;
            }

        }

    </script>

    <style type="text/css">
        .style9 {
            height: 34px;
        }
    </style>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
    <asp:Panel ID="Panel1" runat="server">
        <br />
        <asp:HyperLink ID="HyperLink12" runat="server" NavigateUrl="~/Customer.aspx">Customer Details</asp:HyperLink>
        <br />
        <br />
        <br />
        <asp:HyperLink ID="HyperLink13" runat="server" NavigateUrl="~/SalesOrder.aspx">Sales Order</asp:HyperLink>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
    </asp:Panel>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    <h1 style="font-family: Andalus; font-size: x-large; color: #FF00FF;" >Sales Order</h1>
    <table>

<tr>
<td align="right">Customer ID</td>
<td>
    <asp:DropDownList ID="ddlcid" runat="server" DataSourceID="SqlDataSource1"

        DataTextField="Customer_Id" DataValueField="Customer_Id" AppendDataBoundItems="true">
        <asp:ListItem>---select Customer id---</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"

        ConnectionString="<%$ ConnectionStrings:sqlserverConnectionString %>"

        SelectCommand="SELECT [Customer_Id] FROM [CustomerTable_]">
    </asp:SqlDataSource>
    </td>
</tr>
<tr>
<td align="right">Product Id</td>
<td>
    <asp:DropDownList ID="ddlpid" runat="server" DataSourceID="SqlDataSource2"

        DataTextField="Productcode" DataValueField="Productcode"

        AppendDataBoundItems="true"

        onselectedindexchanged="ddlpid_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem>---select Product Id---</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"

        ConnectionString="<%$ ConnectionStrings:sqlserverConnectionString %>"

        SelectCommand="SELECT [Productcode] FROM [Products]"></asp:SqlDataSource>
    </td>
</tr>
<tr>
<td align="right">Price</td>
<td>
    <asp:TextBox ID="txtp" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
<td align="right">Quantity</td>
<td>
    <asp:TextBox ID="txtq" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

        ErrorMessage="*" ForeColor="Red" ControlToValidate="txtq"></asp:RequiredFieldValidator>
    </td>
</tr>

<tr>
<td align="right" class="style9">Total</td>
<td class="style9">
    <asp:TextBox ID="txttotal" runat="server"></asp:TextBox>
    <asp:Button ID="add" runat="server" Text="add" OnClientClick="return GetTotal();" />
&nbsp;</td>
</tr>
<tr>
<td align="right">Date Of Delivery</td>
<td>

    <asp:TextBox ID="txtdate" runat="server"></asp:TextBox>
  <asp:CalendarExtender ID="calender1" runat="server" TargetControlID="txtdate"

        Format="dd/MM/yyyy"></asp:CalendarExtender>


    </td>
</tr>
<tr>
<td colspan="2" align="center">

    <asp:Button ID="btnsubmit" runat="server" Text="Submit"

        onclick="btnsubmit_Click" />

</td>
</tr>
</table>
    <asp:Label ID="Label1" runat="server" ></asp:Label>

</asp:Content>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class SalesOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

       // ScriptManager.RegisterClientScriptBlock(this,this.GetType(),"hi","GetTotal()",true);
    }
    

   
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        if (txtq.Text == txtq.Text)
        {
            SqlConnection con = new SqlConnection("Data Source=CKRBTECH\\SQLEXPRESS;Initial Catalog=sqlserver;Integrated Security=True");

            SqlCommand cmd = new SqlCommand("sp_quantity", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@cid", ddlcid.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@prodid", ddlpid.SelectedItem.Value.ToString());
            cmd.Parameters.AddWithValue("@price", txtp.Text.ToString());
            cmd.Parameters.Add("@qty", txtq.Text.ToString());
            cmd.Parameters.AddWithValue("@total", txttotal.Text.ToString());
            cmd.Parameters.AddWithValue("@date", txtdate.Text.ToString());
            cmd.Parameters.Add("@error", SqlDbType.VarChar, 500);
            cmd.Parameters["@error"].Direction = ParameterDirection.Output;
            con.Open();
            cmd.ExecuteNonQuery();
            string message = (string)cmd.Parameters["@error"].Value;

            Label1.Text=message;
            con.Close();
            //Response.Write("data inserted successfully");
        }
        //else
        //{
          //  Response.Write("doesn");
        //}
       // Response.Redirect("~/Sales.aspx");
       
    }

    protected void ddlpid_SelectedIndexChanged(object sender, EventArgs e)
    {
        showdata(ddlpid.SelectedItem.Value);
    }
    private void showdata(string id)
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection("Data Source=CKRBTECH\\SQLEXPRESS;Initial Catalog=sqlserver;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select Price from Products where Productcode=@prodid", con);
        cmd.Parameters.AddWithValue("@prodid", ddlpid.SelectedItem.Value);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            txtp.Text = dt.Rows[0]["Price"].ToString();
        }


        con.Close();

    }
    private void showdata()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection("Data Source=CKRBTECH\\SQLEXPRESS;Initial Catalog=sqlserver;Integrated Security=True");
      SqlCommand  cmd = new SqlCommand("select quantity from Products where Productcode=@prodcode", con);
      cmd.Parameters.AddWithValue("@prodcode",ddlpid.SelectedItem.Value);
      con.Open();
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      da.Fill(dt);
      if (dt.Rows.Count > 0)
      {
          txtq.Text = dt.Rows[0]["quantity"].ToString();
      }
      con.Close();

    }


    
}

ConnectionStrings:sqlserverConnectionString %>\"

SelectCommand=\"SELECT [Customer_Id] FROM [CustomerTable_]\">
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td align=\"right\">Product Id</td>
<td>
<asp:DropDownList ID=\"ddlpid\" runat=\"server\" DataSourceID=\"SqlDataSource2\"

DataTextField=\"Productcode\" DataValu eField=\"Productcode\"

AppendDataBoundItems=\"true\"

onselectedindexchanged=\"ddlpid_SelectedIndexChanged\" AutoPostBack=\"True\">
<asp:ListItem>---select Product Id---</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID=\"SqlDataSource2\" runat=\"server\"

ConnectionString=\"<%
ConnectionStrings:sqlserverConnectionString %>" SelectCommand="SELECT [Customer_Id] FROM [CustomerTable_]"> </asp:SqlDataSource> </td> </tr> <tr> <td align="right">Product Id</td> <td> <asp:DropDownList ID="ddlpid" runat="server" DataSourceID="SqlDataSource2" DataTextField="Productcode" DataValueField="Productcode" AppendDataBoundItems="true" onselectedindexchanged="ddlpid_SelectedIndexChanged" AutoPostBack="True"> <asp:ListItem>---select Product Id---</asp:ListItem> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%


ConnectionStrings:sqlserverConnectionString %>\"

SelectCommand=\"SELECT [Productcode] FROM [Products]\"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td align=\"right\">Price</td>
<td>
<asp:TextBox ID=\"txtp\" runat=\"server\"></asp:TextBo x>
</td>
</tr>
<tr>
<td align=\"right\">Quantity</td>
<td>
<asp:TextBox ID=\"txtq\" runat=\"server\"></asp:TextBox>
<asp:RequiredFieldValidator ID=\"RequiredFieldValidator1\" runat=\"server\"
an>
ErrorMessage=\"*\" ForeColor=\"Red\" ControlToValidate=\"txtq\"></asp:RequiredFieldValidator>
</td>
</tr>

<tr>
<td align=\"right\" class=\"style9\">Total</td>
<td class=\"style9\">
<asp:TextBox ID=\"txttotal\" runat=\"server\"></asp:TextBox>
<asp:Button ID=\"add\"pan> runat=\"server\" Text=\"add\" OnClientClick=\"return GetTotal();\" />
&nbsp;</td>
</tr>
<tr>
<td align=\"right\">Date Of Delivery</td>
<td>

<asp:TextBox ID=\"txtdate\" runat=\"server\">an></asp:TextBox>
<asp:CalendarExtender ID=\"calender1\" runat=\"server\" TargetControlID=\"txtdate\"

Format=\"dd/MM/yyyy\"></asp:CalendarExtender>


</td>
</tr>
<tr>
<td colspan=\"2\" align=\"center\">

<asp:Button ID=\"btnsubmit\" runat=\"server\" Text=\"Submit\"

onclick=\"btnsubmit_Click\" />

</td>
</tr>
</table>
<asp:Label ID=\"Label1\" runat=\"server\" ></asp:Label>

</asp:Content>
ConnectionStrings:sqlserverConnectionString %>" SelectCommand="SELECT [Productcode] FROM [Products]"></asp:SqlDataSource> </td> </tr> <tr> <td align="right">Price</td> <td> <asp:TextBox ID="txtp" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right">Quantity</td> <td> <asp:TextBox ID="txtq" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ForeColor="Red" ControlToValidate="txtq"></asp:RequiredFieldValidator> </td> </tr> <tr> <td align="right" class="style9">Total</td> <td class="style9"> <asp:TextBox ID="txttotal" runat="server"></asp:TextBox> <asp:Button ID="add" runat="server" Text="add" OnClientClick="return GetTotal();" /> &nbsp;</td> </tr> <tr> <td align="right">Date Of Delivery</td> <td> <asp:TextBox ID="txtdate" runat="server"></asp:TextBox> <asp:CalendarExtender ID="calender1" runat="server" TargetControlID="txtdate" Format="dd/MM/yyyy"></asp:CalendarExtender> </td> </tr> <tr> <td colspan="2" align="center"> <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" /> </td> </tr> </table> <asp:Label ID="Label1" runat="server" ></asp:Label> </asp:Content>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class SalesOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

       // ScriptManager.RegisterClientScriptBlock(this,this.GetType(),"hi","GetTotal()",true);
    }
    

   
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        if (txtq.Text == txtq.Text)
        {
            SqlConnection con = new SqlConnection("Data Source=CKRBTECH\\SQLEXPRESS;Initial Catalog=sqlserver;Integrated Security=True");

            SqlCommand cmd = new SqlCommand("sp_quantity", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@cid", ddlcid.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@prodid", ddlpid.SelectedItem.Value.ToString());
            cmd.Parameters.AddWithValue("@price", txtp.Text.ToString());
            cmd.Parameters.Add("@qty", txtq.Text.ToString());
            cmd.Parameters.AddWithValue("@total", txttotal.Text.ToString());
            cmd.Parameters.AddWithValue("@date", txtdate.Text.ToString());
            cmd.Parameters.Add("@error", SqlDbType.VarChar, 500);
            cmd.Parameters["@error"].Direction = ParameterDirection.Output;
            con.Open();
            cmd.ExecuteNonQuery();
            string message = (string)cmd.Parameters["@error"].Value;

            Label1.Text=message;
            con.Close();
            //Response.Write("data inserted successfully");
        }
        //else
        //{
          //  Response.Write("doesn");
        //}
       // Response.Redirect("~/Sales.aspx");
       
    }

    protected void ddlpid_SelectedIndexChanged(object sender, EventArgs e)
    {
        showdata(ddlpid.SelectedItem.Value);
    }
    private void showdata(string id)
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection("Data Source=CKRBTECH\\SQLEXPRESS;Initial Catalog=sqlserver;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select Price from Products where Productcode=@prodid", con);
        cmd.Parameters.AddWithValue("@prodid", ddlpid.SelectedItem.Value);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            txtp.Text = dt.Rows[0]["Price"].ToString();
        }


        con.Close();

    }
    private void showdata()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection("Data Source=CKRBTECH\\SQLEXPRESS;Initial Catalog=sqlserver;Integrated Security=True");
      SqlCommand  cmd = new SqlCommand("select quantity from Products where Productcode=@prodcode", con);
      cmd.Parameters.AddWithValue("@prodcode",ddlpid.SelectedItem.Value);
      con.Open();
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      da.Fill(dt);
      if (dt.Rows.Count > 0)
      {
          txtq.Text = dt.Rows[0]["quantity"].ToString();
      }
      con.Close();

    }


    
}


The value of txtdate is not a valid date or not in a format SQL can understand. If it is valid for .NET than instead of passing it as string, convert it to DateTime and pass it as object. Let ADO.NET handle it for you...
The value of txtdate is not a valid date or not in a format SQL can understand. If it is valid for .NET than instead of passing it as string, convert it to DateTime and pass it as object. Let ADO.NET handle it for you...