This is a migrated thread and some comments may be shown as answers.

[Solved] Problem in Insert/Update Record Through In-Line Method by using Code Behind…

1 Answer 119 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ranganath
Top achievements
Rank 1
Ranganath asked on 09 Sep 2009, 01:12 PM

Here is the following Code

<radG:RadGrid ID="RadGrid1" Skin="Lime" runat="server" CssClass="RadGrid" GridLines="None"  AllowPaging="True" PageSize="5" AllowSorting="True" Width="99%" AutoGenerateColumns="False" EnableAJAX="True" ShowStatusBar="true" HorizontalAlign="NotSet" Source="RadGrid1_NeedDataSource"            OnUpdateCommand="RadGrid1_UpdateCommand" OnInsertCommand="RadGrid1_InsertCommand"

OnDeleteCommand="RadGrid1_DeleteCommand">

<PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>

<MasterTableView CommandItemDisplay="Top" GridLines="None" DataKeyNames="CustomerID"

EditMode="InPlace">

<Columns>

<radG:GridEditCommandColumn>

</radG:GridEditCommandColumn>

<radG:GridBoundColumn UniqueName="CustomerID" HeaderText="ID" ReadOnly="true" DataField="CustomerID">

</radG:GridBoundColumn>

<radG:GridBoundColumn UniqueName="CompanyName" HeaderText="CompanyName" DataField="CompanyName">

</radG:GridBoundColumn>

<radG:GridBoundColumn UniqueName="ContactName" HeaderText="ContactName" DataField="ContactName">

</radG:GridBoundColumn>

<radG:GridBoundColumn UniqueName="Address" HeaderText="Address" DataField="Address">

</radG:GridBoundColumn>

<radG:GridButtonColumn CommandName="Delete" Text="Delete">

</radG:GridButtonColumn>

</Columns>

<EditFormSettings EditFormType="Template">

<EditColumn UniqueName="EditCommandColumn1">

</EditColumn>

<FormTemplate>

<table id="Table2" cellspacing="2" cellpadding="1" width="250" border="1" rules="none"

style="border-collapse: collapse">

<tr>

<td>

<table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0">

<tr>

<td>

</td>

<td>

</td>

</tr>

<tr>

<td>

CustomerID:

</td>

<td>

<asp:TextBox ID="txtCustomerID" MaxLength="5" Visible='<% # (Container as GridItem).OwnerTableView.IsItemInserted %>'

runat="server">

</asp:TextBox>

</td>

</tr>

<tr>

<td>

CompanyName:

</td>

<td>

<asp:TextBox ID="txtCompanyName" runat="server" Text='<%# Eval( "CompanyName" ) %>'>

</asp:TextBox>

</td>

</tr>

<tr>

<td>

ContactName:

</td>

<td>

<asp:TextBox ID="txtContactName" runat="server" Text='<%# Eval( "ContactName") %>'

TabIndex="1">

</asp:TextBox>

</td>

</tr>

<tr>

<td>

Address:

</td>

<td>

<asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval( "Address") %>' TabIndex="2">

</asp:TextBox>

</td>

</tr>

</table>

</td>

</tr>

<tr>

<td colspan="2">

<b>Company Info:</b>

</td>

</tr>

<tr>

<td align="right" colspan="2">

<asp:Button ID="btnUpdate" Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Insert" : "Update" %>'

runat="server" CommandName='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "PerformInsert" : "Update" %>'>

</asp:Button>&nbsp;

<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"

CommandName="Cancel"></asp:Button>

</td>

</tr>

</table>

</FormTemplate>

</EditFormSettings>

<ExpandCollapseColumn Visible="False">

<HeaderStyle Width="19px" />

</ExpandCollapseColumn>

<RowIndicatorColumn Visible="False">

<HeaderStyle Width="20px" />

</RowIndicatorColumn>

</MasterTableView>

</radG:RadGrid>

 

Code Behined:-

//Declare a global DataTable dtTable

public static DataTable dtTable;

//Declare a global SqlConnection SqlConnection

//public SqlConnection SqlConnection = new SqlConnection("Data Source=local;Initial Catalog=Northwind;User ID=**");

public SqlConnection SqlConnection = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;uid=sa;pwd=password;");

//Declare a global SqlDataAdapter SqlDataAdapter

public SqlDataAdapter SqlDataAdapter = new SqlDataAdapter();

//Declare a global SqlCommand SqlCommand

public SqlCommand SqlCommand = new SqlCommand();

   

 protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)

    {

//Populate the Radgrid

dtTable = new DataTable();

//Open the SqlConnection

SqlConnection.Open();

try

{

//Select Query to populate the RadGrid with data from table Customers.

string selectQuery = "SELECT CustomerID,CompanyName,ContactName,Address FROM Customers";

SqlDataAdapter.SelectCommand = new SqlCommand(selectQuery, SqlConnection);

SqlDataAdapter.Fill(dtTable);

RadGrid1.DataSource = dtTable;

}

finally

{

//Close the SqlConnection

SqlConnection.Close();

}

       

     

    }

 protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)

    {

        //Get the GridEditableItem of the RadGrid

GridEditableItem editedItem = e.Item as GridEditableItem;

//Get the primary key value using the DataKeyValue.

string CustomerID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["CustomerID"].ToString();

//Access the textbox from the edit form template and store the values in string variables.

string CompanyName= (editedItem.FindControl("txtCompanyName") as TextBox).Text;

string ContactName = (editedItem.FindControl("txtContactName") as TextBox).Text;

string Address = (editedItem.FindControl("txtAddress") as TextBox).Text;

 

try

{

//Open the SqlConnection

SqlConnection.Open();

//Update Query to update the Datatable

string updateQuery = "UPDATE Customers set CompanyName='"+CompanyName+"',ContactName='"+ContactName+"',Address='"+Address+"' where CustomerID='" + CustomerID+"'";

SqlCommand.CommandText = updateQuery;

SqlCommand.Connection = SqlConnection;

SqlCommand.ExecuteNonQuery();

//Close the SqlConnection

SqlConnection.Close();

}

catch (Exception ex)

{

RadGrid1.Controls.Add(new LiteralControl("Unable to update Customers. Reason: " + ex.Message));

e.Canceled = true;

}

    }

 protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)

    {

//Get the GridEditFormInsertItem of the RadGrid

 

GridDataInsertItem insertedItem = (GridDataInsertItem)e.Item;

//GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;

 

 

 

//Access the textbox from the insert form template and store the values in string variables.

string CustomerID = (insertedItem.FindControl("txtCustomerID") as TextBox).Text;

string CompanyName = (insertedItem.FindControl("txtCompanyName") as TextBox).Text;

string ContactName = (insertedItem.FindControl("txtContactName") as TextBox).Text;

string Address = (insertedItem.FindControl("txtAddress") as TextBox).Text;

 

try

{

//Open the SqlConnection

SqlConnection.Open();

//Update Query to insert into  the database

string insertQuery = "INSERT into  Customers(CustomerID,CompanyName,ContactName,Address) values('"+CustomerID+"','"+ CompanyName + "','"+ContactName+"','"+Address+"')";

SqlCommand.CommandText = insertQuery;

SqlCommand.Connection = SqlConnection;

SqlCommand.ExecuteNonQuery();

//Close the SqlConnection

SqlConnection.Close();

}

catch (Exception ex)

{

RadGrid1.Controls.Add(new LiteralControl("Unable to insert Customers. Reason: " + ex.Message));

e.Canceled = true;

}

   

    }

protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)

    {

//Get the GridDataItem of the RadGrid

GridDataItem item = (GridDataItem)e.Item;

//Get the primary key value using the DataKeyValue.

string CustomerID = item.OwnerTableView.DataKeyValues[item.ItemIndex]["CustomerID"].ToString();

try

{

//Open the SqlConnection

SqlConnection.Open();

string deleteQuery = "DELETE from Customers where CustomerID='" + CustomerID + "'";

SqlCommand.CommandText = deleteQuery;

SqlCommand.Connection = SqlConnection;

SqlCommand.ExecuteNonQuery();

//Close the SqlConnection

SqlConnection.Close();

}

catch (Exception ex)

{

RadGrid1.Controls.Add(new LiteralControl("Unable to delete Customers. Reason: " + ex.Message));

e.Canceled = true;

}

    }

 

But I am not able to either Insert/Update Records.

Please Help me in this regards.

I want to Insert/Update Records only in In-Place Mode.

 

At the same time I want to Bind the Combo Box(Which is Grid) from Other table recors.

Please Guide me…..

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 14 Sep 2009, 05:46 AM
Hello Ranganath,

You have declared an editform template, and at the same time have set inplace editing for the control. You can remove the latter setting, so that the custom edit form is rendered, instead of the inline one.
Also, attached to this message, is a small application, which handles a functionality close to the one which you described initially. I hope it helps.

Regards,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Ranganath
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or