1

我尝试在我的数据库中插入数据,但我的 SQLCommand.ExecuteNonQuery 引发 SQLException 错误“将数据类型 varchar 转换为 int”。

这是我的 aspx.cs

    public void FillDropdownList()
    {
        if (!IsPostBack)
        {
            String[] Semesters = { "DIS R", "DIS M", "DDOA", "DECO R", "DECO L", "CRIA" };
            for (int i = 0; i < Semesters.Length; i++)
            {
                ddSemester.Items.Add(Semesters[i].ToString());
            }
            String[] Periodes = { "2013 P2", "2014 P1", "2014 P2" };
            for (int i = 0; i < Periodes.Length; i++)
            {
                ddPeriode.Items.Add(Periodes[i].ToString());
            }

        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string Connectionstring = @"Data Source=mossserver1;Initial Catalog=Sharepoint;Integrated Security=SSPI;";
        SqlConnection Datacon = new SqlConnection(Connectionstring);
        SqlCommand Datacommand = new SqlCommand("sprocSemesterkeuze");
        Datacommand.CommandType = CommandType.StoredProcedure;
        Datacommand.Connection = Datacon;

        //de komende statements herhalen tot je alle parameters hebt ingevoerd
        string leerlingnaam = getStringAfterSlash(SPContext.Current.Site.RootWeb.CurrentUser.Name);
        SqlParameter llnaam = new SqlParameter();
        llnaam.ParameterName = "@leerlingnaam"; //bv @artikelnr, maar dat ligt aan wat je in de stored procedure hebt
        llnaam.SqlDbType = SqlDbType.VarChar;  //kan ook een int zijn of whatever je in de database hebt gedefinieerd
        llnaam.Value = (string)leerlingnaam;
        llnaam.Direction = ParameterDirection.Input;
        Datacommand.Parameters.Add(llnaam);

        SqlParameter semesterkeuze = new SqlParameter();
        semesterkeuze.ParameterName = "@semesterkeuze"; //bv @artikelnr, maar dat ligt aan wat je in de stored procedure hebt
        semesterkeuze.SqlDbType = SqlDbType.VarChar;  //kan ook een int zijn of whatever je in de database hebt gedefinieerd
        semesterkeuze.Value = (string)ddSemester.Text;
        semesterkeuze.Direction = ParameterDirection.Input;
        Datacommand.Parameters.Add(semesterkeuze);

        SqlParameter slber = new SqlParameter();
        slber.ParameterName = "@slber"; //bv @artikelnr, maar dat ligt aan wat je in de stored procedure hebt
        slber.SqlDbType = SqlDbType.VarChar;  //kan ook een int zijn of whatever je in de database hebt gedefinieerd
        slber.Value = (string)tbSlber.Text;
        slber.Direction = ParameterDirection.Input;
        Datacommand.Parameters.Add(slber);

        SqlParameter periode = new SqlParameter();
        periode.ParameterName = "@periode"; //bv @artikelnr, maar dat ligt aan wat je in de stored procedure hebt
        periode.SqlDbType = SqlDbType.VarChar;  //kan ook een int zijn of whatever je in de database hebt gedefinieerd
        periode.Value = (string)ddPeriode.Text;
        periode.Direction = ParameterDirection.Input;
        Datacommand.Parameters.Add(periode);

        Datacon.Open();

        Datacommand.ExecuteNonQuery();
        Datacommand.Dispose();
        Datacon.Dispose();



    }

    protected string getStringAfterSlash(string input)
    {
        ArrayList matches = new ArrayList();
        foreach (Match match in Regex.Matches(input, "\\\\"))
        {
            matches.Add(match.Index);
        }
        if (matches.Count > 0)
        {
            return input.Substring((int)matches[matches.Count - 1] + 1);
        }
        return input;
    }

}

}

这是我的 aspx 文件

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StudentSemesterKeuzeUserControl.ascx.cs" Inherits="StudentSemesterkeuze.StudentSemesterKeuze.StudentSemesterKeuzeUserControl" %>
<asp:Label ID="lblSemester" runat="server" Text="Kies Semester"  Width = " 100px"></asp:Label>
<asp:DropDownList ID="ddSemester" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="lblPeriode" runat="server" Text="Kies Periode" Width = " 100px"></asp:Label>
<asp:DropDownList ID="ddPeriode" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" Text="SLB'er Code"></asp:Label>
<asp:TextBox ID="tbSlber" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Stuur semesterkeuze" 
    onclick="Button1_Click" />

最后但并非最不重要的是,我的存储过程

create procedure sprocSemesterkeuze
@leerlingnaam VARCHAR(255),
@semesterkeuze VARCHAR(255),
@slber          VARCHAR(255),
@periode        VARCHAR(255)
as
begin

Insert into Semesterkeuze(leerlingnummer,semesterkeuze,[status],slber,datum,opmerkingen,periode)
values((select leerlingnummer from ToetsCijfers where leerlingnaam = @leerlingnaam),@semesterkeuze, 'Pending',@slber,CURRENT_TIMESTAMP,null,@periode)


end

我在 Datacommand.ExecuteNonQuery() 处得到异常;在我的 aspx.cs 文件中。有人可以帮忙吗?

这就是我的桌子的样子puu.sh/54Ca5.png

4

2 回答 2

1

错误似乎很简单。您的 Semesterkeuze 数据库中确实有一个 INT 列,您正在尝试使用以下 select 语句获取要插入的值:

(从 ToetsCijfers 中选择 leerlingnummer,其中 leerlingnaam = @leerlingnaam)

检查 ToetsCijfers 的表定义,似乎 leerlingnummer 不是那里的 INT,而是 varchar。

于 2013-10-31T13:29:59.893 回答
0

我找到了解决方案。在我的选择语句中(从 ToetsCijfers 中选择 leerlingnummer,其中 leerlingnaam = @leerlingnaam)我添加了一个 group by 子句(group by leerlingnummer)并且它起作用了。

但我不知道为什么。谁能告诉我?

于 2013-10-31T13:54:32.280 回答