0

使用 asp.net 和 c#

ASP.NET 代码

<asp:Button ID="btnAddNew" runat="server" Text="Add New" 
        onclick="btnAddNew_Click" />
    <div id="divAdd" runat="server" style="display:none" >  
    <table width="100%">
        <tr>
            <td colspan="1">First Name :</td>
            <td colspan="1" align="left"><asp:TextBox ID="txtFName" runat="server" ></asp:TextBox> </td>
        </tr>
        <tr>
            <td colspan="1">Last Name :</td>
            <td colspan="1" align="left"><asp:TextBox ID="txtLName" runat="server" ></asp:TextBox>  </td>
        </tr>
        <tr>
            <td colspan="1">Date Of Birth :</td>
            <td colspan="1" align="left"><asp:TextBox ID="txtDob" runat="server" ></asp:TextBox>  </td>
        </tr>
        <tr>
            <td colspan="1">Age :</td>
            <td colspan="1" align="left" ><asp:TextBox ID="txtAge" runat="server" ></asp:TextBox> </td>
        </tr>
        <tr>

            <td colspan="2" align="center">
                <asp:Button ID="btnSave" runat="server" Text="Save"  OnClientClick=" return validate()" type="submit"   />

                <asp:Button ID="btnClose" runat="server" Text="Close" OnClientClick ="return clear()" type="submit" />
            </td>
        </tr>
    </table>
    </div>

C# 代码

static void Insert()
    {
        try
        {
            string connectionString =
                "server=JEBW1011ZAHID;" +        
                "initial catalog=employee;" + 
                "integrated security = true";             
            using (SqlConnection conn =
                new SqlConnection(connectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(
                    "INSERT INTO [NyhedTB] ([NyhedDato], [NyhedTitel], [NyhedTekst]) " +
                    "VALUES (@NyhedDato, @NyhedTitel, @NyhedTekst)", conn))
                {
                    cmd.Parameters.AddWithValue("@NyhedDato", txtfname.Text);
                    cmd.Parameters.AddWithValue("@NyhedTitel", txtlanme.Text);
                    cmd.Parameters.AddWithValue("@NyhedTekst", txtage.Text);

                    int rows = cmd.ExecuteNonQuery();  // Inserted rows number
                }
            }
        }
        catch (SqlException ex)
        {
            //Log exception
            //Display Error message
        }

    }

显示错误为 txtfname 不存在。

需要代码帮助

4

2 回答 2

6

Insert的方法是静态的。它无权访问任何页面元素。我建议您将这些值作为参数传递给方法,而不是将其与您的 UI 耦合:

static void Insert(string fName, string lName, string age)
{
    try
    {
        string connectionString =
            "server=JEBW1011ZAHID;" +        
            "initial catalog=employee;" + 
            "integrated security = true";             
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(
                "INSERT INTO [NyhedTB] ([NyhedDato], [NyhedTitel], [NyhedTekst]) " +
                "VALUES (@NyhedDato, @NyhedTitel, @NyhedTekst)", conn))
            {
                cmd.Parameters.AddWithValue("@NyhedDato", fName);
                cmd.Parameters.AddWithValue("@NyhedTitel", lName);
                cmd.Parameters.AddWithValue("@NyhedTekst", age);

                int rows = cmd.ExecuteNonQuery();  // Inserted rows number
            }
        }
    }
    catch (SqlException ex)
    {
        //Log exception
        //Display Error message
    }
}

然后当从你后面的代码调用这个方法时,你可以传递你想要的任何值:

protected void SomeButton_Click(object sender, EventArgs e)
{
    Insert(txtfname.Text, txtlanme.Text, txtage.Text);
}

现在,您的Insert方法更可重用,因为它不再与您的 UI 耦合。

于 2013-04-01T08:42:18.530 回答
0

static 在您的方法中删除

放置此代码public void Insert()而不是 static void Insert()

于 2013-04-01T08:51:08.317 回答