What is the equivalent of this Visual Basic code to c#? [on hold]

Home / Uncategorized / What is the equivalent of this Visual Basic code to c#? [on hold]

Question:
Can you please give me the equivalent codes for c#.

This is the VB.NET code:Private Sub Productname()
Myquery = "Select name from products where Item_ID = ‘" & txtsearch.Text & "’"
ConnOpen (connstring)

With myComm
.CommandText = Myquery
.Connection = myconn
End With

With myAdapter
.SelectCommand = myComm
.Fill(mydata1)
End With

For i = 0 to mydata1.Rows.Count – 1
With prod.
text = (mydata1.Rows(i)("Name"))
End With
Next
End Sub
mydata1 as Datatable.
i as integer.
Myquery as string.
myComm as MysqlCommand
myAdapter as MysqlAdapter


Answer:
private void Productname()
{
Myquery = $"Select name from products where Item_ID = ‘{txtsearch.Text}’";
ConnOpen(connstring);
myComm.CommandText = Myquery
myComm.Connection = myconn

myAdapter.SelectCommand = myComm
myAdapter.Fill(mydata1)

for (i = 0; i <= mydata1.Rows.Count - 1; i++) prod.text = mydata1.Rows[i]["Name"]; } You should note however that the for part needs to be reviewed. You are assigning the values read from all rows to same object's text property. Read more

Leave a Reply

Your email address will not be published. Required fields are marked *