Question Retrieve data from textboxes and save them

Benniit

Member
Joined
Mar 14, 2009
Messages
12
Programming Experience
Beginner
'Already declared the DataSet as dtFile and SqlDataAdapter as daFile
Dim SqlQuery as String

SqlQuery = "Select * from FileRegister where FILE_NO='" & frmBsearch.txtFileNo.Text.Trim & "'"

daFile = New SqlClient.SqlDataAdapter(SqlQuery , SQLConnection)
daFile.Fill(dtFile , "FileRegister")


frmBsearch.txtSPrefix.DataBindings.Add("text", dtFile, "FileRegister.Prefix")
frmBsearch.txtSPlotNo.DataBindings.Add("text", dtFile, "FileRegister.Plot_No")
frmBsearch.txtSBlockNo.DataBindings.Add("text", dtFile, "FileRegister.Block_No")

So please how can I add a new row and save those records in the TextBoxes?

ben
 
Dim rowNew as DataRow

rowNew = dtFile.NewRow
rowNew("Prefix") = TextBox1.Text
rowNew("Plot_No") = TextBox2.Text
rowNew("Block_No") = TextBox3.Text
dtFile.Rows.Add(rowNew)

The above will add a new row to your datatable. You will still need an update command assigned to your dataAdapter and then to call your datatable update statement to update the data to your database.
 
As said I assumed a prefix of "dt" stood for DataTable instead of Dataset. Make the following changes.

VB.NET:
Dim rowNew as DataRow

rowNew = dtFile.Tables("FileRegister").NewRow
rowNew("Prefix") = TextBox1.Text
rowNew("Plot_No") = TextBox2.Text
rowNew("Block_No") = TextBox3.Text
dtFile.Tables("FileRegister").Rows.Add(rowNew)

Another assumption is that how I named the columns is that way... if not change appropiately....

Additional note, if you have a typed dataset instead of untyped, the actual table name should display in intellisense right after you click the . after the dataset name. so instead of UntypedDatasetName.Tables("tablename") it would be TypeDataSetName.TableName

VB.NET:
Dim rowNew as DataRow

rowNew = dtFile.FileRegister.NewRow
rowNew("Prefix") = TextBox1.Text
rowNew("Plot_No") = TextBox2.Text
rowNew("Block_No") = TextBox3.Text
dtFile.FileRegister.Rows.Add(rowNew)

Now wouldnt something like the following be so much more descriptive & helpful....

dsFile.tblFileRegister.Rows.Add(rowNew)
 
Re:saving data from text boxes into the database

After the connection has been established successfully, I did something like this but i get this error "System.NullReferenceException was unhandled
"Object reference not set to an instance of an object.", though your code will be helpful.

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim rowNew As DataRow
Dim dtFile as DataSet
dtFile = New DataSet
rowNew = dtFile.Tables("FileRegister").NewRow
rowNew("Prefix") = Me.txtPrefix.Text.Trim
rowNew("Plot_No") = Me.txtPlotNo.Text.Trim
rowNew("Block_No") = Me.txtBlockNo.Text
rowNew("Location") = Me.txtLocation.Text
dtFile.Tables("FileRegister").Rows.Add(rowNew)

End Sub
 
Your declaring dtFile as an new untyped dataset, then calling a table in it named "FileRegister" but the dataset doesnt actually have any tables created in it. This is the cause of your error.

Change your declaration from a Dataset to Datatable as below; plus the code example I first posted in this thread

VB.NET:
Dim dtFile as New DataTable
Dim rowNew as DataRow

rowNew = dtFile.NewRow
rowNew("Prefix") = TextBox1.Text
rowNew("Plot_No") = TextBox2.Text
rowNew("Block_No") = TextBox3.Text
dtFile.Rows.Add(rowNew)
 
Retrieve values of textbox and save them

Please I did make the changes u gave me, but on a click of a save button this error pops up again. Column Prefix does not does not belong to Table. Meanwhile, I have those columns present in the table FileRegister. I don't know why. Am getting worried.


Dim dtFile as New DataTable
Dim rowNew as DataRow

rowNew = dtFile.NewRow
rowNew("Prefix") = TextBox1.Text
rowNew("Plot_No") = TextBox2.Text
rowNew("Block_No") = TextBox3.Text
dtFile.Rows.Add(rowNew)
 
Please I did make the changes u gave me, but on a click of a save button this error pops up again. Column Prefix does not does not belong to Table. Meanwhile, I have those columns present in the table FileRegister. I don't know why. Am getting worried.

Dim dtFile as New DataTable
Dim rowNew as DataRow

rowNew = dtFile.NewRow
rowNew("Prefix") = TextBox1.Text
rowNew("Plot_No") = TextBox2.Text
rowNew("Block_No") = TextBox3.Text
dtFile.Rows.Add(rowNew)

My code snippet relates (or attempts too) specifically towards your question which is reading the values from the textbox into a new table record(datarow). I'm assumming many things such as that you can create a valid connection string, dataadapter, dataset/datatable, and you have defined the structures of your table(s)to fit your needs.

Look at what the code is doing.... In the very first line of this example it ceates a brand new DataTable, its a blank table with no data or columns. Where do you expect this table to get columns named (Prefix, Plot_No etc) from unless you add them somewhere?

You either have to create a typed dataset and design your tables in design mode or through coding create a untyped dataset/datatable and have it add the DataColumns you want at run time. Also I think your misunderstanding the scope of these tables.
 
Back
Top