Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从不同于CodeBehind的类更新SQL数据库-';由于其保护级别,无法访问_C#_Webforms - Fatal编程技术网

C# 从不同于CodeBehind的类更新SQL数据库-';由于其保护级别,无法访问

C# 从不同于CodeBehind的类更新SQL数据库-';由于其保护级别,无法访问,c#,webforms,C#,Webforms,ASPX 类将数据添加到数据库 public partial class TestSID : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public TextBox MakePublicTxt { get { return this.RegisteringAiD; } } protected void Sub

ASPX

类将数据添加到数据库

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

    public TextBox MakePublicTxt
    {
        get { return this.RegisteringAiD; }
    }

    protected void SubmitBtn_Click(object sender, EventArgs e)
    {
        SubmitBtn.Enabled = true;
        App_Code.SQLDBSubmits.SIDSubmit();
        SubmitBtn.Enabled = false;
    }       
}
因为我使用的是一个单独的类文件,而不是后面的代码来执行SQL更新,所以出现以下错误:

编译器错误消息:CS0122:“TestSID.RegisteringId”由于其保护级别而不可访问

我已经研究了好几天了,似乎还没弄明白。

试试这个:

static class SQLDBSubmits
{
    internal static void SIDSubmit()
    {
        SqlConnection con = SQLDBAccess.DefaultConnection();
        SqlCommand cmd = new SqlCommand("INSERT INTO TestSID(@RegisteringAiD)", con);
        cmd.Parameters.Clear();

        cmd.Parameters.AddWithValue("RegisteringAiD", Pages.TestSID.RegisteringAiD.Text);
    }
}
和代码隐藏

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" 
         AutoEventWireup="true" CodeBehind="TestSID.aspx.cs" 
         Inherits="AdminConsole.Pages.TestSID" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <asp:TextBox ID="RegisteringAiD" runat="server" CssClass="form-control" 
             Wrap="False" OnTextChanged="RegisteringAiD_TextChanged"></asp:TextBox>
static class SQLDBSubmits
{
    internal static void SIDSubmit(string aid)
    {
        SqlConnection con = SQLDBAccess.DefaultConnection();
        SqlCommand cmd = new SqlCommand("INSERT INTO TestSID(@RegisteringAiD)", con);
        cmd.Parameters.Clear();

        cmd.Parameters.AddWithValue("RegisteringAiD", aid);
    }
}

SIDSubmit
需要一个接受注册ID的参数。SQL是无效的,它至少应该有单词
value
,理想情况下还有列名
插入TestSID(RegisteringAiD)值(@RegisteringAiD)
您还需要打开连接并执行命令,并且连接和命令应使用blocksASPX!=WebForms?这很有效。谢谢你的回答。如果对你有效,请不要忘记接受。
 App_Code.SQLDBSubmits.SIDSubmit(MakePublicTxt.Text);