Hello out there ? i have one question rather a strange one

i have two tables say admission and attendance with different fields however admn id , name are the fileds common to both of them

when i add my data in admission table (via form) can i at the same time add the data to attendance table ?? ie .. when i click on save button on admission form will the admn , name fileds will also be entered in the attendance database table??

can it be done ? if yes how ?? can we bind a textbox with 2 data bindings?
i am binding thru properties n rest coding....plz tell me how to do./..

Dani AI

Generated

Yes — do both inserts from the Save button (or have the database do it with a trigger), but do it safely: use a single transaction, parameterized commands, and — if Admission uses an auto-increment/identity key — capture that new id and use it when inserting into Attendance. That keeps the two tables consistent and avoids partial saves if something fails. This follows 's suggestion to run two inserts, but with a concrete, robust pattern.

Example pattern in VB.NET (SQL Server) — replace connectionString and control names:

Dim newId As Integer = 0
Using conn As New SqlClient.SqlConnection(connectionString)
    conn.Open()
    Using tx = conn.BeginTransaction()
        Try
            Using cmd As New SqlClient.SqlCommand("INSERT INTO Admission (Name,OtherField) VALUES (@name,@other); SELECT SCOPE_IDENTITY();", conn, tx)
                cmd.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value = txtName.Text
                cmd.Parameters.Add("@other", SqlDbType.Int).Value = CInt(txtOther.Text)
                newId = Convert.ToInt32(cmd.ExecuteScalar())
            End Using

            Using cmd2 As New SqlClient.SqlCommand("INSERT INTO Attendance (AdmnID, Name) VALUES (@admnId, @name)", conn, tx)
                cmd2.Parameters.Add("@admnId", SqlDbType.Int).Value = newId
                cmd2.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value = txtName.Text
                cmd2.ExecuteNonQuery()
            End Using

            tx.Commit()
        Catch ex As Exception
            tx.Rollback()
            Throw
        End Try
    End Using
End Using

To clear the combo box selection (do not remove the item from the list): set the selection to no item rather than deleting it. For example:

ComboBox1.SelectedIndex = -1   'deselects but keeps the items
' or
ComboBox1.SelectedItem = Nothing

Note: 's earlier snippet removed the item — that deletes it from the list, which is probably not what you want.

Quick troubleshooting: that intermittent "String or binary data would be truncated" typically means a parameter value is longer than the target column. Fixes: set TextBox.MaxLength to match the DB column, declare parameter sizes (as above) instead of relying on AddWithValue, and validate input before saving. If Attendance rows must always exist, consider a DB trigger so inserts happen server-side and you cannot forget to create the attendance record.

Recommended Answers

All 9 Replies

just run your commandtext.Insert or whatever it is (i am going by memory) and insert into both tables. No?

Lee

nah...re.....its nt wrkng! ne other solutions ? ne more appropriate solns?

just run your commandtext.Insert or whatever it is (i am going by memory) and insert into both tables. No?

Lee

yes, you just do twice inserting.

>>nah...re.....its nt wrkng! ne other solutions ? ne more appropriate solns?
lee was give the answer. Have u tried it?post your code.

yes, you just do twice inserting.

But how will i insert directly ? i have no form for attendance...directly update into the table ??? how can i do this ??? i have not designed any form for the same...i want the admission table values (for the specific rows common in attendance table ) to be also inserted in the attendance table.

2) Ne idea how to clear the Selected item in combo box? ie...when i click on next...my all text box gets clear but combo box values for the previous entry remains intact.....help me:)

1. Tables field. give the admission and attendance table field. ()
2. You want to all clear item on combo box (empty) or you want to clear selected (ex : ---select your choice---) ??

selected choise should be erased ...

*Very strange error comes sometimes while i try to enter data in database which says.....
String or Binary data would be truncated !
Whats this error ? its a very common error i cant figure it out.whts the problem....its cums only sumtimes not always...why so ???

reply soon !

1. Tables field. give the admission and attendance table field. ()
2. You want to all clear item on combo box (empty) or you want to clear selected (ex : ---select your choice---) ??

>> *Very strange error comes sometimes while i try to enter data in database which says.....
String or Binary data would be truncated !

this error came up if length of your input more longer than your data type length in database.
Ex: In your database
name -> type : String, Length : 5
but when you input data, its more than 5 character. so it would be truncated.
So, if you input Daniweb it would be -> Daniw.

Okie....i will try...but i have given enuf length for each field sufficiently....ne ways thanks a lot.....u havent yet replied for the combo box question...hp u ll help me out soon

>> *Very strange error comes sometimes while i try to enter data in database which says.....
String or Binary data would be truncated !

this error came up if length of your input more longer than your data type length in database.
Ex: In your database
name -> type : String, Length : 5
but when you input data, its more than 5 character. so it would be truncated.
So, if you input Daniweb it would be -> Daniw.

]Okie....i will try...but i have given enuf length for each field sufficiently....ne ways thanks a lot.....u havent yet replied for the combo box question...hp u ll help me out soon

>> selected choise should be erased ...
This following code will removed selected item on combo box after you click on button 1.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ComboBox1.Items.RemoveAt(ComboBox1.SelectedIndex)
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.