ASP.NET DevExpress Gridview CRUD操作

时间:2017-06-25 10:38:23

标签: c# asp.net gridview devexpress

我无法进行CRUD操作。请让我帮忙使用dev express Gridview。我无法输入编辑表单并添加新表单。

我的[ASPx]代码

<%@ Register assembly="DevExpress.Web.v17.1, Version=17.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web" tagprefix="dx" %>


<asp:Content runat="server" ID="BodyContent1" ContentPlaceHolderID="ContentPlaceHolder1">

<div>
    <dx:ASPxLabel ID="lblTest" runat="server" Text="Select test to configure" ForeColor="Black" Font-     Bold="true"/>
    <dx:ASPxComboBox ID="ddlService" runat="server" AutoPostBack="true" ValueType="System.String" OnSelectedIndexChanged="ddlService_SelectedIndexChanged">
    </dx:ASPxComboBox>
</div>

<div>
     <dx:ASPxLabel ID="lblMessage" runat="server" Text="" ForeColor="Red" Font-Bold="true" Visible="false" />    

    <dx:ASPxGridView ID="GridView1" runat="server" ClientInstanceName="GridView1"
        KeyFieldName="TestConfigId" AutoGenerateColumns="False" Width="850px" 

        OnRowInserting="Gridview1_RowInserting" 
        OnRowUpdating="Gridview1_RowUpdating" 
        OnRowDeleting="Gridview1_RowDeleting">

        <Columns>                
            <dx:GridViewDataTextColumn Caption="TestConfigId" FieldName="TestConfigId" Name="TestConfigId" Visible="true">
           <dx:GridViewDataTextColumn Caption="Main Test" FieldName="MainTest" Name="MainTest" Settings-AllowSort="False" VisibleIndex="2" >
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Sub Test" FieldName="SubTest" Name="SubTest" Settings-AllowSort="False" VisibleIndex="3" >
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Test Code" FieldName="TestCode" Name="TestCode" VisibleIndex="5">

             <dx:GridViewCommandColumn ShowNewButton ="true" ShowEditButton="true" ShowDeleteButton="true" VisibleIndex="0" Width="100" FixedStyle="Left">


            </dx:GridViewCommandColumn>

        </Columns>

        <Settings HorizontalScrollBarMode="Visible"/>
        <SettingsBehavior AutoExpandAllGroups="true"/>
        <SettingsBehavior AllowFocusedRow="True"/>
        <SettingsResizing ColumnResizeMode="NextColumn"/>

        <Styles>
            <FixedColumn BackColor="LightYellow"></FixedColumn>
        </Styles>

        <Settings ShowStatusBar="Visible" />
        <SettingsEditing Mode="PopupEditForm" />
    </dx:ASPxGridView>
    <dx:ASPxPopupControl ID="popupSample" runat="server" ShowCloseButton="true" ShowHeader="true" 
    PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter">
    </dx:ASPxPopupControl>
</div>

</asp:Content>

我的代码隐藏文件是

using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using DevExpress.Web;
using DevExpress.Web.Data;
using System.ComponentModel;



public partial class Forms_path_test_config : System.Web.UI.Page
{
int _serviceId;
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    BindParentServiceDropdown();
}

public void BindParentServiceDropdown()
{

    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "get_services";
    cmd.Connection = con;

    try
    {
        con.Open();
        ddlService.DataSource = cmd.ExecuteReader();
        ddlService.TextField = "ServiceName";
        ddlService.ValueField = "ServiceID";
        ddlService.DataBind();
    }

    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }

}



protected void ddlService_SelectedIndexChanged(object sender, EventArgs e)
{
    _serviceId = Convert.ToInt32(ddlService.Value);

    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "get_config";
    cmd.Parameters.Add("@ServiceId", SqlDbType.Int).Value = _serviceId;
    cmd.Connection = con;

    try
    {
        con.Open();
        GridView1.SettingsText.EmptyDataRow = "No Records Found";
        GridView1.DataSource = cmd.ExecuteReader();

        GridView1.Visible = true;
        GridView1.DataBind();   

    }

    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        con.Close();
        con.Dispose();
    }

}



private void BindGrid()
{

    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "get_config";
    cmd.Parameters.Add("@ServiceId", SqlDbType.Int).Value = _serviceId;
    cmd.Connection = con;

    try
    {
        con.Open();
        GridView1.SettingsText.EmptyDataRow = "No Records Found";
        GridView1.DataSource = cmd.ExecuteReader();

        GridView1.Visible = true;
        GridView1.DataBind();

    }

    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        con.Close();
        con.Dispose();
    }
}

protected void Gridview1_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
{

    _serviceId = Convert.ToInt32(ddlService.Value);


    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "update_test_configuration";
    cmd.Parameters.Add("@TestConfigId", SqlDbType.Int).Value = Convert.ToInt32(e.NewValues["TestConfigId"]);
    cmd.Parameters.Add("@ServiceId", SqlDbType.Int).Value = Convert.ToInt32(_serviceId);
    cmd.Parameters.Add("@ParentTest", SqlDbType.NVarChar).Value = ddlService.SelectedItem.Text.Trim();
    cmd.Parameters.Add("@MainTest", SqlDbType.NVarChar).Value = e.NewValues["MainTest"].ToString();
    cmd.Parameters.Add("@SubTest", SqlDbType.NVarChar).Value = e.NewValues["SubTest"].ToString();
    cmd.Parameters.Add("@TestCode", SqlDbType.NVarChar).Value = e.NewValues["TestCode"].ToString();
    cmd.Connection = con;

    try
    {
        con.Open();
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            e.Cancel = true;
            GridView1.CancelEdit();
        }
        lblMessage.Visible = true;
        lblMessage.Text = "Record(s) Saved";
        BindGrid();

    }

    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        con.Close();
        con.Dispose();
    }
}

protected void Gridview1_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
{
    try
    {
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "delete_test_config";
        cmd.Parameters.Add("@TestConfigID", SqlDbType.Int).Value =         Convert.ToInt32(e.NewValues["TestConfigId"]) ;
        cmd.Connection = con;
        con.Close();
        lblMessage.Visible = true;
        lblMessage.Text = "Record(s) Deleted";
    }
    catch (Exception ex)
    {
        lblMessage.Visible = true;
        lblMessage.Text = ex.Message;
    }

    BindGrid();
}



protected void Gridview1_RowInserting(object sender, ASPxDataInsertingEventArgs e)
{
    _serviceId = Convert.ToInt32(ddlService.Value);

    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "insert_test_configuration";
    cmd.Parameters.Add("@ServiceId", SqlDbType.Int).Value = Convert.ToInt32(_serviceId);
    cmd.Parameters.Add("@ParentTest", SqlDbType.NVarChar).Value = ddlService.SelectedItem.Text.Trim();
    cmd.Parameters.Add("@MainTest", SqlDbType.NVarChar).Value = e.NewValues["MainTest"].ToString();
    cmd.Parameters.Add("@SubTest", SqlDbType.NVarChar).Value = e.NewValues["SubTest"].ToString();
    cmd.Parameters.Add("@TestCode", SqlDbType.NVarChar).Value = e.NewValues["TestCode"].ToString();

    cmd.Connection = con;

    try
    {
        con.Open();
        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            e.Cancel = true;
            GridView1.CancelEdit();
        }
        lblMessage.Visible = true;
        lblMessage.Text = "Record(s) Saved";
        BindGrid();
    }

    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        con.Close();
        con.Dispose();
    }   

}


protected void CancelEditing(CancelEventArgs e)
{
    e.Cancel = true;
    GridView1.CancelEdit();
}

请帮帮我。当我试图点击更新错误时说&#34;对象引用未设置为对象的实例&#34;。

0 个答案:

没有答案