private void button1_Click(object sender, EventArgs e)
{
string data = textBox1.Text;
Form2 form2=new Form2();
form2.LoadData(data);
this.Hide();
form2.Show();
}
In Form 2
----------
Declare 3 TextBox and write the below method
public void LoadData(string data)
{
using (SqlConnection conn = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog=MyDb;Persist Security Info=True;User ID=MyUser;Password=MyPassword"))
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string sql = "Select no,name,age from YourTable where no=@no";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@no", data);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
textBox1.Text = reader["no"].ToString();
textBox2.Text = reader["name"].ToString();
textBox3.Text = reader["age"].ToString();
}
reader.Close();
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
}
}
this code is in c# and i want to convert to vb.net ..please help how to do that ?
Riyaz AkhtarPosted Aug 7, 2013, 10:20 AM
Iftikar HussainPosted Aug 7, 2013, 8:28 AM
Here it is
Regards,
Iftikar
VulpesPosted Aug 7, 2013, 8:27 AM
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim data As String = textBox1.Text
Dim f2 As Form2 = New Form2()
f2.LoadData(data)
Me.Hide()
f2.Show()
End Sub
In Form 2
----------
Public Sub LoadData(ByVal data As String)
Using conn As New SqlConnection("Data Source=SQLEXPRESSInitial Catalog=MyDbPersist Security Info=TrueUser ID=MyUserPassword=MyPassword")
If conn.State = ConnectionState.Closed Then conn.Open()
Dim sql As String = "Select no,name,age from YourTable where no=@no"
Dim cmd As New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@no", data)
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
textBox1.Text = reader.Item("no").ToString()
textBox2.Text = reader.Item("name").ToString()
textBox3.Text = reader.Item("age").ToString()
End While
reader.Close()
if conn.State = ConnectionState.Open Then conn.Close()
' conn.Dispose() Rem Unnecessary in Using statement
End Using
End Sub
In Form2 you'll also need to add this declaration at the top of the file (before the Form code):
Imports System.Data.SqlClient