Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have sql server 2005 .there i have 1 table name data which has slno,name,type,shape as column fields
i want to display the records of name,type,shape in textbox on combobox selection of slno

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

'Dim strcon As String = ("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
'Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim ds As New DataSet
Dim dt As DataTable


'Try

cmd.Connection = cn
cn.Open()

Dim da As New SqlDataAdapter("select * from data", cn)
dt = New DataTable

ds = New DataSet
da.Fill(ds, "data")

For i As Integer = 0 To ds.Tables("data").Rows.Count - 1
If ComboBox1.SelectedItem = ds.Tables("data").Rows(i).Item("lot_no").ToString() Then
TextBox3.Text = ds.Tables("data").Rows(i + 1).Item("type").ToString()
TextBox4.Text = ds.Tables("data").Rows(i + 2).Item("shape").ToString()
TextBox5.Text = ds.Tables("data").Rows(i + 3).Item("size").ToString()
TextBox6.Text = ds.Tables("data").Rows(i + 4).Item("place").ToString()
TextBox7.Text = ds.Tables("data").Rows(i + 5).Item("weight").ToString()



End If

Next

this is the above code, bt showing error in for loop statement as null reference
Posted
Updated 13-Aug-17 22:25pm

I saw you access different rows in each setting statement. A row can be access by Rows(i) as below
VB
TextBox3.Text = ds.Tables("data").Rows(i).Item("type").ToString()
TextBox4.Text = ds.Tables("data").Rows(i).Item("shape").ToString()
TextBox5.Text = ds.Tables("data").Rows(i).Item("size").ToString()
TextBox6.Text = ds.Tables("data").Rows(i).Item("place").ToString()
TextBox7.Text = ds.Tables("data").Rows(i).Item("weight").ToString()
 
Share this answer
 
Comments
sudeshna from bangkok 4-Jul-13 5:50am    
thanks a lot,
its working now.

I have another query that on same form i have 2 other fields name and date

after the textbox gets filled on selection of combo box, i will manually enter name and date in the other two tet box and this data on clicking submit button will get stored in another table name sort.

previously my table name was data from where i was fetching data and storing in textbox based on combo box selection.

now in that same form, i want two other records to be added in textboxs which is column and date and will be updated in database table sort.

what code will i write?

please help.

thanks in advance
sudeshna
sudeshna from bangkok 4-Jul-13 5:52am    
this is the query i wrote bt showing error that lot_no cannot be null

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim ds As New DataSet
'Dim dr As SqlDataReader
cmd.Connection = cn
cn.Open()
cmd.CommandText = "insert into sort(name,issue_dt) values (' " & TextBox2.Text & " ','" & TextBox8.Text & " ')"
cmd.ExecuteNonQuery()
MsgBox("Data successfully added", MsgBoxStyle.Information)
' Me.SortTableAdapter.Insert(ComboBox1.Text, TextBox6.Text)
Me.SortTableAdapter.Fill(Me.RecordDataSet.sort)
thanh_bkhn 4-Jul-13 6:14am    
If you want the new values to be updated into the database, you must use Update command.
Try this:
cmd.CommandText = "Update Sort set " &
"type = '" & TextBox3.Text & "', " &
(shape, size, place, weight is similar to type)
"name = '" & TextBox2.Text & "', " &
"issue_dt = '" & TextBox8.Text & "'" &
"Where lot_no = '" & ComboBox1.SelectedItem & "'"
sudeshna from bangkok 4-Jul-13 6:46am    
after update sort set how to write the syntax? i did not understand. the quotation part
Can you please post again.

thanks
thanh_bkhn 4-Jul-13 6:54am    
Don't you understand the Update syntax? So you could take a look at http://msdn.microsoft.com/en-us/library/8hwekas8(v=vs.80).aspx
The essential thing to keep in mind is, the update command requires a Where clause, in your case it is the lot_no is the previously selected combobox's value, otherwise you will update entire the database.
Because of i+1, i+2, i+3... is the problem.

Just remove that +1, +2 +3... then it will work.
 
Share this answer
 
Comments
sudeshna from bangkok 4-Jul-13 5:50am    
thanks a lot,
its working now.

I have another query that on same form i have 2 other fields name and date

after the textbox gets filled on selection of combo box, i will manually enter name and date in the other two tet box and this data on clicking submit button will get stored in another table name sort.

previously my table name was data from where i was fetching data and storing in textbox based on combo box selection.

now in that same form, i want two other records to be added in textboxs which is column and date and will be updated in database table sort.

what code will i write?

please help.

thanks in advance
sudeshna
sudeshna from bangkok 4-Jul-13 5:51am    
this is the query i wrote, but showing error

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim ds As New DataSet
'Dim dr As SqlDataReader
cmd.Connection = cn
cn.Open()
cmd.CommandText = "insert into sort(name,issue_dt) values (' " & TextBox2.Text & " ','" & TextBox8.Text & " ')"
cmd.ExecuteNonQuery()
MsgBox("Data successfully added", MsgBoxStyle.Information)
' Me.SortTableAdapter.Insert(ComboBox1.Text, TextBox6.Text)
Me.SortTableAdapter.Fill(Me.RecordDataSet.sort)
Try this way

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

'Dim strcon As String = ("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
'Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim ds As New DataSet
Dim dt As DataTable


'Try

cmd.Connection = cn
cn.Open()

Dim da As New SqlDataAdapter("select * from data where lot_no=" & cint(ComboBox1.SelectedItem), cn)
dt = New DataTable

ds = New DataSet
da.Fill(ds, "data")



TextBox3.Text = ds.Tables("data").Rows(0).Item("type").ToString()
TextBox4.Text = ds.Tables("data").Rows(0).Item("shape").ToString()
TextBox5.Text = ds.Tables("data").Rows(0).Item("size").ToString()
TextBox6.Text = ds.Tables("data").Rows(0).Item("place").ToString()
TextBox7.Text = ds.Tables("data").Rows(0).Item("weight").ToString()
 
Share this answer
 
Comments
sudeshna from bangkok 5-Jul-13 9:24am    
i have another doubt. can you help me?
bhuvamehul 6-Jul-13 1:03am    
Sure help you If I can. Tell me
Because of i+1, i+2, i+3... is the problem. You are referring a row which is not available.

Just remove that +1, +2 +3... then it will work.
 
Share this answer
 
v2
Comments
sudeshna from bangkok 5-Jul-13 4:12am    
I have another query that on same form i have 2 other fields name and date after the textbox gets filled on selection of combo box, i will manually enter name and date in the other two tet box and this data on clicking submit button will get stored in another table name sort. previously my table name was data from where i was fetching data and storing in textbox based on combo box selection. now in that same form, i want two other records to be added in textboxs which is column and date and will be updated in database table sort. what code will i write? please help. thanks in advance sudeshna
sudeshna from bangkok 5-Jul-13 4:14am    
i wrote the code
Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat") Dim da As New SqlDataAdapter Dim cmd As New SqlCommand Dim ds As New DataSet 'Dim dr As SqlDataReader cmd.Connection = cn cn.Open() cmd.CommandText = cmd.CommandText = "Update Sort set " & "type = '" & TextBox3.Text & "', " & "shape='" & TextBox4.Text & " '," & "size='" & TextBox5.Text & " '," & "place='" & TextBox6.Text & " ' ," & "weight='" & TextBox7.Text & " '," & "name = '" & TextBox2.Text & "', " & "issue_dt = '" & TextBox8.Text & "'" & "Where lot_no = '" & ComboBox1.SelectedItem & "'" cmd.ExecuteNonQuery() MsgBox("Data successfully updated", MsgBoxStyle.Information) Me.SortTableAdapter.Insert(ComboBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text) Me.SortTableAdapter.Fill(Me.RecordDataSet.sort) but showing error in that Could not find stored procedure 'False'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900