文本框如何在具有相同ID的所有不同行中的按钮单击上保存多个值 [英] How can a Text box save multiple values on a button click in all different rows with same id

查看:59
本文介绍了文本框如何在具有相同ID的所有不同行中的按钮单击上保存多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文本框中,用户可以输入多个值。在按钮上单击我想在数据库中保存这些值,但在不同的行中具有相同的id。(逐个)。



我试过但它们存放在同一行。



有人可以帮助我。

In a text box a user can eneter multiple values.On a button click I want to save these
values in the database but with the same id in different rows.(one by one).

I tried but they are getting stored in the same line.

Can someone help me please.

推荐答案

- 这里我假设您的目标表是

--CREATE TABLE ActualTable(ID int,name varchar(100))



- 创建将处理多个名称并保存在实际表中的proc



CREATE PROC proc_ins_names_from_collection

@strNameString VARCHAR(400)

- 这将是包含所有名称的字符串(我假设它们是分开的,camma)

AS

BEGIN



- 逻辑开始将字符串拆分成行

DECLARE @strNameTable TABLE(name VARCHAR (400))



DECLARE @allnames VARCHAR(400),

@currentindex INT,

@currentName VARCHAR(400),

@ currentID INT;



IF RIGHT(RTRIM(@strNameString),1)<> '',''

SELECT @strNameString = @strNameString +'',''

SELECT @currentindex = patindex(''%,%'',@ strNameString)

WHILE @currentindex<> 0

BEGIN

SELECT @currentName = left(@strNameString,(@ currentindex-1))

INSERT INTO @strNameTable(name)VALUES (CAST(@currentName AS VARCHAR(400)))

SELECT @strNameString = stuff(@strNameString,1,@ currentindex,'''')

SELECT @currentindex = patindex(''%,%'',@ strNameString)

END



- 逻辑结束将字符串拆分为行,这时你在临时表中得到所有名称为行

- SELECT * FROM @strNameString



- 获取最大id和添加一个来创建新ID,它将以所有名称保存

SELECT @currentID = MAX(isnull(ID,0))来自ActualTable

SET @currentID = @currentID + 1;



- 在实际表中插入所有具有相同ID的名称

INSERT INTO ActualTable(id,name)

SELECT @currentID,name FROM @strNameTable



END





- 将此测试为belo w $ / $
- 执行proc_ins_names''Prashant,Sonewane''

- SELECT * FROM ActualTable
-- Here i assume is your destination table is
--CREATE TABLE ActualTable (ID int, name varchar(100))

--Create the proc which will process multiple names and save in actual table

CREATE PROC proc_ins_names_from_collection
@strNameString VARCHAR(400)
-- this will be string having all names (I assume they are seperated by , camma)
AS
BEGIN

-- Logic start to split the string in rows
DECLARE @strNameTable TABLE (name VARCHAR(400))

DECLARE @allnames VARCHAR(400),
@currentindex INT,
@currentName VARCHAR(400),
@currentID INT;

IF RIGHT(RTRIM(@strNameString),1) <> '',''
SELECT @strNameString = @strNameString + '',''
SELECT @currentindex = patindex(''%,%'' , @strNameString)
WHILE @currentindex <> 0
BEGIN
SELECT @currentName = left(@strNameString, (@currentindex-1))
INSERT INTO @strNameTable(name) VALUES( CAST (@currentName AS VARCHAR(400)))
SELECT @strNameString = stuff(@strNameString, 1, @currentindex, '''')
SELECT @currentindex = patindex(''%,%'' , @strNameString)
END

-- Logic ends to split the string in row, By this time you get all names in temp table as rows
-- SELECT * FROM @strNameString

--Get the max id and add one to creat new id which will be saved with all names
SELECT @currentID = MAX(isnull(ID,0)) from ActualTable
SET @currentID = @currentID + 1;

-- Insert all names with same id in your actual table
INSERT INTO ActualTable(id,name)
SELECT @currentID, name FROM @strNameTable

END


-- Test this as below
-- EXEC proc_ins_names ''Prashant,Sonewane''
-- SELECT * FROM ActualTable


嗨苏曼女士,在这里,我为您做了一个简单的例子。

根据您的要求,您希望将同一文本框中的多个值插入到具有相同Id的数据库中的不同行。因为您正在重复ID,所以您必须从页面级别插入Id。

请按照以下步骤操作: -

I.选择一个文本框和一个按钮您的页面: -



Hi Ms. Suman, Here I made a simple example for you.
As per your requirement you want to insert multiple value from same textbox to the different row in database with same Id. Because you are repeating the Id so you have to insert Id from your page level.
Please follow the Steps given below:-
I. Take one textbox and one button on your page :-

 <asp:textbox id="txtName" runat="server" xmlns:asp="#unknown"></asp:textbox>
<asp:button id="btnSave" runat="server" text="Save" onclick="btnSave_Click" xmlns:asp="#unknown" />





2。创建一个表在数据库中: -



2.Create a Table in Database :-

create table Test
(
 Id int,
 UserName varchar(500)
)





3.写一个插入过程的程序值



3.Write a Procedure to Insert the value

CREATE PROCEDURE sp_SaveValue (
	 @aintId INT
	,@astrUserName VARCHAR(500)
	)
AS
BEGIN

   CREATE TABLE #tempTable ( Id INT ,PID VARCHAR(50))
   INSERT INTO #tempTable (id, PID )
   SELECT Row AS ID ,Value AS SetValue FROM dbo.fn_SplitForOrdering(@astrUserName, ',')
   INSERT INTO Test (Id, UserName) SELECT @aintId,PID FROM #tempTable

END





你可以在这里看到我正在使用一个函数来分割用逗号分隔的所有值。

这里是我正在使用的函数: -



You can see here I am using a function to split all the values separated by comma.
and here is the function I am using :-

CREATE FUNCTION dbo.fn_SplitForOrdering (
	@InputText VARCHAR(4000)
	,@Delimiter VARCHAR(10)
	)
RETURNS @Array TABLE (
	Row INT
	,Value VARCHAR(4000)
	)
AS
-----------------------------------------------------------  
-- returns a varchar rowset from a delimited string --  
-----------------------------------------------------------  
BEGIN
	DECLARE @Pos INT
	DECLARE @End INT
	DECLARE @TextLength INT
	DECLARE @DelimLength INT
	DECLARE @Row INT

	SET @TextLength = DataLength(@InputText)

	-- Exit function if no text is passed in  
	IF @TextLength = 0
		RETURN

	SET @Pos = 1
	SET @DelimLength = DataLength(@Delimiter)
	SET @Row = 1

	IF @DelimLength = 0
	BEGIN
		WHILE @Pos <= @TextLength
		BEGIN
			INSERT @Array (
				Row
				,Value
				)
			VALUES (
				@Row
				,SubString(@InputText, @Pos, 1)
				)

			SET @Pos = @Pos + 1
			SET @Row = @Row + 1
		END
	END
	ELSE
	BEGIN
		SET @InputText = @InputText + @Delimiter
		SET @End = CharIndex(@Delimiter, @InputText)

		WHILE @End > 0
		BEGIN
			INSERT @Array (
				Row
				,Value
				)
			VALUES (
				@Row
				,SubString(@InputText, @Pos, @End - @Pos)
				)

			SET @Pos = @End + @DelimLength
			SET @End = CharIndex(@Delimiter, @InputText, @Pos)
			SET @Row = @Row + 1
		END
	END

	RETURN
END





现在终于,点击按钮点击下面的代码: -



Now at last, on button click write following code :-

string strValue = txtName.Text; //Make sure you enter your value separated by comma.
       //int id = 2; //take your id from anywhere you want.

       int id = GetMaxId(); //see this method is returning a new Id.//[Edit]
       SqlConnection con = new SqlConnection(yourconnectionstring);
       SqlCommand cmd = new SqlCommand("sp_SaveValue", con);
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.AddWithValue("@aintId", id);
       cmd.Parameters.AddWithValue("@astrUserName", strValue);
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();





那就是它,希望你能得到它。



我猜你自己不会做任何事情。无论如何。要在每个按钮中输入不同的ID,请执行以下操作: -

编写一个方法,从表中获取最大Id,然后将其增加1,然后插入到表中: -



That''s It, Hope you got it.

I guess You won''t do anything by your self. Any ways. to enter different id in each button click do the following things:-
Write a method that will fetch the maximum Id from your table then increase it by 1 and then insert to your table :-

private int GetMaxId()
  {
      int intId = 0;
      SqlConnection con = new SqlConnection(strCon);
      SqlCommand cmd = new SqlCommand("select Max(Id) from Test", con);
      con.Open();
      SqlDataReader dr = cmd.ExecuteReader();
      if (dr.Read())
      {
          intId = int.Parse(dr[0].ToString());
      }
      return intId + 1;
  }


HI SUMAN



获取身份证session并将会话值传递给数据库。你可以得到你的结果。



Rgds

Jagadesh
HI SUMAN

Get the ID in a session and pass the session value to the database. You can get your result.

Rgds
Jagadesh


这篇关于文本框如何在具有相同ID的所有不同行中的按钮单击上保存多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆