473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

xml to gridview then save to database

6
Hi,

I never worked before with XML, so i don't know if what i'm trying to do is the best way or not.

My XML is very complex, so i work with XPath to get to the elements i am interested from the file, as you can see in my .cs code, in XMLDataSource.

What i made until now and works fine is, my gridview populated with the desired data from xml.

I wish to import my data from gridview to a table in database.Below you can find all my code (sql,cs,aspx)

If i execute the SP in SSMS it work fine.
When i click the Import btn, i get no error message, but the import isn't done.

Why is there anything in gvr.Cells[2].Text; ?

Is there anothor way to do this. Any advice is welcome. Thanks.

"Default.cs"

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// GridView_De1_Components.DataBind();
}
}

protected void btn_ImportComponent_Click(object sender, EventArgs e)
{
// int returnValue = 0;

SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = ConfigurationManager.ConnectionStrings["ComponentLogin"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insert_Components";

foreach (GridViewRow gvr in GridView_De1_Components.Rows)
{
SqlParameter pp = new SqlParameter();
pp.ParameterName = "@ID_sql";
pp.Value = gvr.Cells[0].Text;
cmd.Parameters.Add(pp);

pp = new SqlParameter();
pp.ParameterName = "@ComponentNo_sql";
pp.Value = gvr.Cells[1].Text;
cmd.Parameters.Add(pp);

pp = new SqlParameter();
pp.ParameterName = "@Lenght_sql";
pp.Value = gvr.Cells[2].Text;
cmd.Parameters.Add(pp);

pp = new SqlParameter();
pp.ParameterName = "@ISMultiComponent_sql";
pp.Value = gvr.Cells[3].Text;
cmd.Parameters.Add(pp);

//pp = new SqlParameter();
//pp.ParameterName = "@ModuleID_sql";
//pp.Value = gvr.Cells[4].Text;
//cmd.Parameters.Add(pp);

//SqlParameter parameterReturnValue = new SqlParameter("ReturnValue", SqlDbType.Int);
//parameterReturnValue.Direction = ParameterDirection.ReturnValue;
//cmd.Parameters.Add(parameterReturnValue);

try
{
cnn.Open();
cmd.ExecuteNonQuery();
// returnValue = (int)cmd.Parameters["ReturnValue"].Value;
cnn.Close();
}
catch (Exception ex)
{
//MessageBox.Show("DataBase connection error saving Components. Please try again!");
throw ex;

}
}//foreach
}

"Default.aspx"

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventComponentup="true"
CodeBehind="Default.aspx.cs" Inherits="WebWebApp._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:XmlDataSource ID="sourceDe1" runat="server" DataFile = "De_1.xml"
XPath = "/ProductDocument/Product/Components/Component"> </asp:XmlDataSource>
<asp:Button ID="btn_Import" runat="server" Text="ImportComponent" />
<asp:GridView ID="GridView_De1_Components" runat="server" DataSourceID = "sourceDe1"
AutoGenerateColumns = "false" Height="141px" Width="712px">
<Columns>
<asp:TemplateField HeaderText = "ID">
<ItemTemplate>
<%# XPath("./@ID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "ComponentNo">
<ItemTemplate>
<%# XPath("./@ComponentNo")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Length">
<ItemTemplate>
<%# XPath("./@Length")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "IsMultiComponent">
<ItemTemplate>
<%# XPath("./@IsMultiComponent")%>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText = "Modules">
<ItemTemplate>
<asp:GridView ID="GridView_De1_Components_Modules" AutoGenerateColumns = "false"
DataSource = '<%# XPathSelect("./ComponentModuleRefs/ComponentModuleRef") %>'

runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# XPath("./@ModuleID")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>

</asp:Content>


"Store Procedure: insert_Components"

USE [ProductsDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[insert_Components]
-- Add the parameters for the stored procedure here
@ID_sql varchar(30),
@ComponentNo_sql varchar(30),
@Lenght_sql varchar(30),
@ISMultiComponent_sql varchar(30)
--@ModuleID_sql nvarchar(50)

AS
BEGIN
SET NOCOUNT ON;

insert into Components
(ID,ComponentNo,Lenght,ISMultiComponent)
values (@ID_sql,@ComponentNo_sql,@Lenght_sql,@ISMultiComp onent_sql)

--return scope_identity()
END

"Script for DB table: Components"

USE [ProductsDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Components](
[ID] [varchar](30) NULL,
[ComponentNo] [varchar](30) NULL,
[Lenght] [varchar](30) NULL,
[ISMultiComponent] [varchar](30) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
Jul 13 '12 #1
1 1703
sasha3
6
Sometimes i get the error: (i don't know why sometimes i don't get this error).

"Procedure or function ... has too many arguments specified."

I added in the for loop from cs code:

cmd.Parameters.Clear();

so know it save to my database blank rows. Can anyone help me?
Jul 13 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: hazz | last post by:
What do I do to capture the checkbox values as listed below in order to update the database table. There will be an ID field also contained in the row to use in the update query. What would I put...
3
by: Bren | last post by:
Hi All VS2005 Gridview control with vb I am populating a gridview of company staff. One of the columns is a hyperlink to a SMS texting facility I have so secrataries can text the managers...
2
by: etam | last post by:
Hi, what is the best way to show data which is generated dynamically (not from database) in a GridView? Thanks in advance, Etam.
2
by: gomzi | last post by:
Hi, I would like to know as to how I could accomplish the task of automatically updating my users gridview when I insert a new row in my database. i.e. Suppose I insert a new row in a database,...
1
by: senthilcyr | last post by:
hi, How to delete a row from gridview without using database. Thanks, Senthilvelan.N
0
by: sbandalli | last post by:
Hi, I have a datagridview1, which has a comboboxcolumn categoryname(which is binded to a database table Category ) listCol.DataSource = cDS.Tables; datagridview1 also has two textbox...
0
by: Kalkin | last post by:
Hi i am having trouble with the following problem any help would be appreciated:: The gridview starts off with empty text boxes for input along with already populated dropdowns to choose from for...
4
by: abdulaziz68 | last post by:
I have gridview in asp.net ( c# ) , and i have to column in gridview 1st column it's a number and 2nd column it's a color , and I use SQL server database and I used sqldatasource in asp.net .. The...
0
by: MiziaQ | last post by:
I am connecting to a database table to pass values to textboxes in a gridview. I get the following error: Object reference not set to an instance of an object. Thanks in advance for your help!...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.