使用此站点,我试图通过gridview行上的选择在数据库中插入一行。除了从gridview收集主键之外,我已经完成了所有工作。我已经尝试了所有我找不到的东西。到目前为止,我对这种语言并不满意,但我的代码如下:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["sdsMicrosoftDB"].ConnectionString;
OleDbConnection currentConnection = new OleDbConnection(connectionString);
string eventID = GridView1.SelectedValue.ToString();
try
{
string eid = Session["eid"].ToString();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "INSERT INTO tblVolunteerRoster (eventID,empID) VALUES (@eventID,@empID)";
cmd.CommandType = CommandType.Text;
cmd.Connection = currentConnection;
cmd.Parameters.AddWithValue("@eventID", eventID);
cmd.Parameters.AddWithValue("@empID", eid);
currentConnection.Open();
cmd.ExecuteNonQuery();
}
特定于该gridview的代码是:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" CellPadding="4" ForeColor="#333333"
GridLines="None" HorizontalAlign="Center"
onrowcommand="GridView1_RowCommand" DataKeyNames="eventID" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField ApplyFormatInEditMode="True" DataField="eventID"
HeaderText="Event ID" SortExpression="eventID" />
<asp:BoundField DataField="eventStartDate" HeaderText="Start Date"
SortExpression="eventStartDate" DataFormatString="{0:MMM dd yyyy}"
ApplyFormatInEditMode="True" />
<asp:BoundField DataField="eventDescription" HeaderText="Description"
SortExpression="eventDescription" />
<asp:BoundField DataField="eventEstHours" HeaderText="Est. Hours"
SortExpression="eventEstHours" />
<asp:BoundField DataField="eventLocation" HeaderText="Location"
SortExpression="eventLocation" />
<asp:BoundField DataField="eventWorkersNeeded" HeaderText="Workers Needed"
SortExpression="eventWorkersNeeded" />
</Columns>
</asp:GridView>
有什么想法吗?我已经把头撞到了一堵墙上试图自己解决这个问题。提前谢谢!