2010-02-18 4 views
1

데이터베이스에서 오는 행이 많고 쿼리 문자열에 의존하기 때문에 .aspx.vb에서 htmltable을 만들었습니다. 그 부분은 괜찮아. 그러나 누군가가 셀 중 하나를 변경하면 어떻게 데이터를 다시 데이터베이스에 저장합니까?제출을 클릭하면 데이터베이스에 htmltablecell 데이터를 추가하는 방법

코드 - 누군가가 텍스트 상자에 가격을 변경하는 경우

Dim tr As New HtmlTableRow 
Dim td As New HtmlTableCell 
td = New HtmlTableCell 
Dim txtbox1 As New TextBox 
txtbox1.ID = "price_" & rd("id") 
txtbox1.Text = rd("price") 
td.Controls.Add(txtbox1) 
tr.Cells.Add(td) 
tb1.Rows.Add(tr) 

지금, 어떻게 그것을 데이터베이스에 저장합니까?

Protected Sub submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles submit.Click 
    Dim sqlcnn As SqlConnection = Nothing, sqlstr As String = "", sqlcmd As SqlCommand 
    sqlstr = "UPDATE ???" 
    sqlcmd = New SqlCommand(sqlstr, sqlcnn) 
    sqlcmd.ExecuteScalar() 
End Sub 

자세히 답변 해주세요 - 버튼을 제출하기위한 이 코드입니다.

+0

누구? 이 질문은 어렵습니까? – iuers

+0

변수 "rd"는 무엇을 나타 냅니까? 또한 페이지에서 코드의 첫 번째 스 니펫이 어디에 있습니까? Page_Load? –

+0

rd가 sqldatareader입니다. 첫 번째 스 니펫은 page_load – iuers

답변

1

기본 제공되는 표 컨트롤을 사용하는 것이 좋습니다. 다음은 연습 번호 Walkthrough: Editing and Inserting Data in Web Pages with the DetailsView Web Server Control 입니다. Walkthrough: Basic Data Access in Web Pages으로 시작하는 것이 좋습니다.

HTML로 전달하려면 다음과 같이 입력 한 내용을 토대로 한 방법을 사용하십시오. 업데이트 버튼을 추가하고 해당 이벤트에서이 코드를 사용해보십시오.

'Create an update Command with the apropriate paramaters' 
Dim sql = ("UPDATE SO_Prices SET Price = @Price WHERE ID = @ID") 
Dim Cmd As New SqlCommand(sql, connection) 
Cmd.Parameters.Add("@Price", SqlDbType.SmallMoney) 
Cmd.Parameters.Add("@ID", SqlDbType.Int) 

connection.Open() 

'Loop through the fields returned' 
For Each Key As String In Request.Form.Keys 

    'Make sure you only process fields you want' 
    If Key.StartsWith("price") Then 

     'Pull the ID out of the field name by splitting on the underscore' 
     ' then choosing the second item in the array' 
     Dim ID As String = Key.Split("_")(1) 

     'Update the paramaters for the update query' 
     Cmd.Parameters("@ID").Value = ID 
     Cmd.Parameters("@Price").Value = Request.Form(Key) 

     'Run the SQL to update the record' 
     Cmd.ExecuteNonQuery() 

    End If 
Next 

connection.Close() 

추가적인 assitance가 필요한 경우 알려주십시오. 그렇지 않은 경우 정답으로 표시하십시오.

+0

나는 html 테이블을 사용해야한다. 코드는 이미 완료되었다. 이제 데이터베이스에 텍스트 상자에 새 값을 저장하는 방법은 무엇입니까? – iuers

+0

회원 코드를 업데이트했습니다. 이게 당신의 질문을 해결합니까? –

관련 문제