将动态TextBox值从GridView保存到数据库

问题描述:

我在下面的代码中得到了预期的类委托枚举接口或结构错误。我尝试了几种不同的方法,但我无法让它工作。

我已经包含了我的连接字符串在web配置中设置。你可以提供的任何信息都非常有帮助。





I am getting expected class delegate enums interface or struct error in the below code .I have tried a few different ways but i cannot get it to work .
I have included my connection string that was set up in the web config .any bit of info you can give would be very helpful.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Text;
using System.Data.OleDb;

 namespace WebApplication8
{
    public partial class WebForm2 : System.Web.UI.Page
    {


       }
}
//connection string in web.config
//<configuration>
  //<connectionstrings>
   // <add name="ApplicationServices" connectionstring="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true">
    //  providerName="System.Data.SqlClient" />
    //<add name="DtbConnectionString" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dtb.accdb">
    //  providerName="System.Data.OleDb" />
 // </add></add></connectionstrings>


 private string GetConnectionString()
    
        
{  
      return 
System.Configuration.ConfigurationManager.ConnectionStrings["DtbConnectionString"].ConnectionString;
    }



 

    private void InsertRecords(StringCollection sc)
    {
        oledbConnection conn = new oledbConnection(GetConnectionString());
        StringBuilder sb = new StringBuilder(string.Empty);
        string[] splitItems = null;
        foreach (string item in sc)
        {
 
            const string sqlStatement = "INSERT INTO SampleTable (Column1,Column2,Column3) VALUES";
            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());
                sb.AppendFormat("{0}('{1}','{2}','{3}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2]);
            }
 
        }
 
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
 
           //Display a popup which indicates that the record was successfully inserted
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
 
        }
        catch (System.Data.oledb.oledbException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
 
        }
        finally
        {
            conn.Close();
        }
    }
 

 
protected void Button1_Click(object sender, EventArgs e)
{
        int rowIndex = 0;
        StringCollection sc = new StringCollection();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
 
                    //get the values from the TextBoxes
                    //then add it to the collections with a comma "," as the delimited values
                    sc.Add(box1.Text + "," + box2.Text + "," + box3.Text);
                    rowIndex++;
                }
                //Call the method for executing inserts
                InsertRecords(sc);
            }
        }
}  </configuration>

从您的代码中可以明显看出您收到此错误,因为你把所有方法都放在了类 WebForm2 之外。将所有方法放在类定义中或只复制以下代码。



It is evident from your code that you are getting this error because you are putting all your methods outside the class WebForm2. Put all the methods inside the class definition or just copy the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Text;
using System.Data.OleDb;
 
 namespace WebApplication8
{
    public partial class WebForm2 : System.Web.UI.Page
    {
 private string GetConnectionString()
    
        
{  
      return 
System.Configuration.ConfigurationManager.ConnectionStrings["DtbConnectionString"].ConnectionString;
    }
 

 
 
 
    private void InsertRecords(StringCollection sc)
    {
        oledbConnection conn = new oledbConnection(GetConnectionString());
        StringBuilder sb = new StringBuilder(string.Empty);
        string[] splitItems = null;
        foreach (string item in sc)
        {
 
            const string sqlStatement = "INSERT INTO SampleTable (Column1,Column2,Column3) VALUES";
            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());
                sb.AppendFormat("{0}('{1}','{2}','{3}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2]);
            }
 
        }
 
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
 
           //Display a popup which indicates that the record was successfully inserted
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
 
        }
        catch (System.Data.oledb.oledbException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
 
        }
        finally
        {
            conn.Close();
        }
    }
 
 
 
protected void Button1_Click(object sender, EventArgs e)
{
        int rowIndex = 0;
        StringCollection sc = new StringCollection();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
 
                    //get the values from the TextBoxes
                    //then add it to the collections with a comma "," as the delimited values
                    sc.Add(box1.Text + "," + box2.Text + "," + box3.Text);
                    rowIndex++;
                }
                //Call the method for executing inserts
                InsertRecords(sc);
            }
        }
}
}





希望这有帮助。

一切顺利。



Hope this helps.
All the best.