Converting VB to C#

D

Dave1031

I am trying to translate some VB code into C#.

THis works in VB: Link button in repeater controll is clicked....

Sub repeater1_ItemCommand(sender As Object, e As RepeaterCommandEventArgs)

If e.CommandName = "Detail" then

Dim x as Label = e.Item.FindControl("lblTitleID")


Dim sTitleID as String = x.Text


DataGrid1.DataSource = getDetail(sTitleID)
DataGrid1.DataBind()

End If
End Sub


....getDetail function is called to populate a datagrid with the details....


Function getDetail(TitleID as String) as SqlDataReader


Const strConnString as String = "server=localhost;uid=sa;pwd=xxx;
database=pubs"
Dim objConn as New SqlConnection(strConnString)

Dim strSQL as String
strSQL = "SELECT * FROM titles WHERE title_id = @TitleID"
Dim objCmd as New SqlCommand(strSQL, objConn)


Dim paramTitleID as SqlParameter
paramTitleID = New SqlParameter("@TitleID", SqlDbType.VarChar, 10)
paramTitleID.Value = TitleID
objCmd.Parameters.Add(paramTitleID)

objConn.Open() 'Open the connection

Return objCmd.ExecuteReader(CommandBehavior.CloseConnection)

End Function


.... but this cvode in c# throws numerous errors...


void ShowDetails(object sender, EventArgs e)

void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e)
{

string sTitleID =
DataGrid1.DataKeys[DataGrid1.SelectedIndex].ToString();


DataGrid1.DataSource = getDetail(sTitleID);
DataGrid1.DataBind();


}


void getDetail(sTitleID) SqlDataReader
{

const string strConnString =
"server=localhost;uid=sa;pwd=xxx;database=pubs";
SqlConnection objConn = new SqlConnection(strConnString);

const string strSQL = "SELECT * FROM titles WHERE title_id =
@TitleID";
SqlCommand objCmd = new SqlCommand(strSQL, objConn);

SqlParameter paramTitleID;
paramTitleID = new SqlParameter("@TitleID", SqlDbType.VarChar,
10);
paramTitleID.Value = TitleID;
objCmd2.Parameters.Add(paramTitleID);

objConn2.Open();

return objCmd.ExecuteReader(CommandBehavior.CloseConnection);

objConn2.Close();

}

Can anyone give me some hints on where I going wrong? Specifically, how do
I call the procedure to populate the sata reader?
 
P

Pavel Minaev

I am trying to translate some VB code into C#.

THis works in VB: Link button in repeater controll is clicked....

Sub repeater1_ItemCommand(sender As Object, e As RepeaterCommandEventArgs)

    If e.CommandName = "Detail" then

     Dim x as Label = e.Item.FindControl("lblTitleID")

     Dim sTitleID as String = x.Text

     DataGrid1.DataSource = getDetail(sTitleID)
     DataGrid1.DataBind()

    End If
End Sub

...getDetail function is called to populate a datagrid with the details.....

  Function getDetail(TitleID as String) as SqlDataReader

    Const strConnString as String = "server=localhost;uid=sa;pwd=xxx;
database=pubs"
    Dim objConn as New SqlConnection(strConnString)

    Dim strSQL as String
    strSQL = "SELECT * FROM titles WHERE title_id = @TitleID"
    Dim objCmd as New SqlCommand(strSQL, objConn)

    Dim paramTitleID as SqlParameter
    paramTitleID = New SqlParameter("@TitleID", SqlDbType.VarChar, 10)
    paramTitleID.Value = TitleID
    objCmd.Parameters.Add(paramTitleID)

    objConn.Open()  'Open the connection

    Return objCmd.ExecuteReader(CommandBehavior.CloseConnection)

   End Function

... but this cvode in c# throws numerous errors...

Please list the errors then.
 
M

Morten Wennevik [C# MVP]

Dave1031 said:
I am trying to translate some VB code into C#.

THis works in VB: Link button in repeater controll is clicked....

Sub repeater1_ItemCommand(sender As Object, e As RepeaterCommandEventArgs)

If e.CommandName = "Detail" then

Dim x as Label = e.Item.FindControl("lblTitleID")


Dim sTitleID as String = x.Text


DataGrid1.DataSource = getDetail(sTitleID)
DataGrid1.DataBind()

End If
End Sub


...getDetail function is called to populate a datagrid with the details....


Function getDetail(TitleID as String) as SqlDataReader


Const strConnString as String = "server=localhost;uid=sa;pwd=xxx;
database=pubs"
Dim objConn as New SqlConnection(strConnString)

Dim strSQL as String
strSQL = "SELECT * FROM titles WHERE title_id = @TitleID"
Dim objCmd as New SqlCommand(strSQL, objConn)


Dim paramTitleID as SqlParameter
paramTitleID = New SqlParameter("@TitleID", SqlDbType.VarChar, 10)
paramTitleID.Value = TitleID
objCmd.Parameters.Add(paramTitleID)

objConn.Open() 'Open the connection

Return objCmd.ExecuteReader(CommandBehavior.CloseConnection)

End Function


... but this cvode in c# throws numerous errors...


void ShowDetails(object sender, EventArgs e)

void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e)
{

string sTitleID =
DataGrid1.DataKeys[DataGrid1.SelectedIndex].ToString();


DataGrid1.DataSource = getDetail(sTitleID);
DataGrid1.DataBind();


}


void getDetail(sTitleID) SqlDataReader
{

const string strConnString =
"server=localhost;uid=sa;pwd=xxx;database=pubs";
SqlConnection objConn = new SqlConnection(strConnString);

const string strSQL = "SELECT * FROM titles WHERE title_id =
@TitleID";
SqlCommand objCmd = new SqlCommand(strSQL, objConn);

SqlParameter paramTitleID;
paramTitleID = new SqlParameter("@TitleID", SqlDbType.VarChar,
10);
paramTitleID.Value = TitleID;
objCmd2.Parameters.Add(paramTitleID);

objConn2.Open();

return objCmd.ExecuteReader(CommandBehavior.CloseConnection);

objConn2.Close();

}

Can anyone give me some hints on where I going wrong? Specifically, how do
I call the procedure to populate the sata reader?

Hi Dave,

The VB code would translate to something like the below

void repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if(e.CommandName == "Detail")
{
Label x = e.Item.FindControl("lblTitleID") as Label;

string sTitleID = x.Text;

DataGrid1.DataSource = getDetail(sTitleID);
DataGrid1.DataBind();
}
}

SqlDataReader getDetail(string TitleID)
{
const string strConnString =
"server=localhost;uid=sa;pwd=xxx;database=pubs";
SqlConnection objConn = new SqlConnection(strConnString);

string strSQL = "SELECT * FROM titles WHERE title_id =
@TitleID";
SqlCommand objCmd = new SqlCommand(strSQL, objConn);

objCmd.Parameters.AddWithValue("@TitleID", TitleID);

objConn.Open();

return objCmd.ExecuteReader(CommandBehavior.CloseConnection);
}

Are you really using an SqlDataReader as DataSource?! You would leave an
open SqlConnection. I would suggest you use an SqlDataAdapter and create a
DataSet of the data. Use the DataSet as DataSource. Something along the
lines of the below

const string strConnString =
"server=localhost;uid=sa;pwd=xxx;database=pubs";

DataSet getDetail(string TitleID)
{
string strSQL = "SELECT * FROM titles WHERE title_id = @TitleID";

using (SqlConnection objConn = new SqlConnection(strConnString))
{
using (SqlCommand objCmd = new SqlCommand(strSQL, objConn))
{
objCmd.Parameters.AddWithValue("@TitleID", TitleID);

using (SqlDataAdapter sda = new SqlDataAdapter(objCmd))
{
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
}
}
}
 
D

David Anton

Your 'getDetails' is nothing like your original VB logic (and you're
returning from the method before you get to the call to objConn2.Close().

This is the conversion of both methods from Instant C#:

public void repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Detail")
{
Label x = e.Item.FindControl("lblTitleID");
string sTitleID = x.Text;
DataGrid1.DataSource = getDetail(sTitleID);
DataGrid1.DataBind();
}
}

public SqlDataReader getDetail(string TitleID)
{
const string strConnString = "server=localhost;uid=sa;pwd=xxx;
database=pubs";
SqlConnection objConn = new SqlConnection(strConnString);
string strSQL = null;
strSQL = "SELECT * FROM titles WHERE title_id = @TitleID";
SqlCommand objCmd = new SqlCommand(strSQL, objConn);
SqlParameter paramTitleID = null;
paramTitleID = new SqlParameter("@TitleID", SqlDbType.VarChar, 10);
paramTitleID.Value = TitleID;
objCmd.Parameters.Add(paramTitleID);
objConn.Open(); //Open the connection
return objCmd.ExecuteReader(CommandBehavior.CloseConnection);
}
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI


Dave1031 said:
I am trying to translate some VB code into C#.

THis works in VB: Link button in repeater controll is clicked....

Sub repeater1_ItemCommand(sender As Object, e As RepeaterCommandEventArgs)

If e.CommandName = "Detail" then

Dim x as Label = e.Item.FindControl("lblTitleID")


Dim sTitleID as String = x.Text


DataGrid1.DataSource = getDetail(sTitleID)
DataGrid1.DataBind()

End If
End Sub


...getDetail function is called to populate a datagrid with the details....


Function getDetail(TitleID as String) as SqlDataReader


Const strConnString as String = "server=localhost;uid=sa;pwd=xxx;
database=pubs"
Dim objConn as New SqlConnection(strConnString)

Dim strSQL as String
strSQL = "SELECT * FROM titles WHERE title_id = @TitleID"
Dim objCmd as New SqlCommand(strSQL, objConn)


Dim paramTitleID as SqlParameter
paramTitleID = New SqlParameter("@TitleID", SqlDbType.VarChar, 10)
paramTitleID.Value = TitleID
objCmd.Parameters.Add(paramTitleID)

objConn.Open() 'Open the connection

Return objCmd.ExecuteReader(CommandBehavior.CloseConnection)

End Function


... but this cvode in c# throws numerous errors...


void ShowDetails(object sender, EventArgs e)

void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e)
{

string sTitleID =
DataGrid1.DataKeys[DataGrid1.SelectedIndex].ToString();


DataGrid1.DataSource = getDetail(sTitleID);
DataGrid1.DataBind();


}


void getDetail(sTitleID) SqlDataReader
{

const string strConnString =
"server=localhost;uid=sa;pwd=xxx;database=pubs";
SqlConnection objConn = new SqlConnection(strConnString);

const string strSQL = "SELECT * FROM titles WHERE title_id =
@TitleID";
SqlCommand objCmd = new SqlCommand(strSQL, objConn);

SqlParameter paramTitleID;
paramTitleID = new SqlParameter("@TitleID", SqlDbType.VarChar,
10);
paramTitleID.Value = TitleID;
objCmd2.Parameters.Add(paramTitleID);

objConn2.Open();

return objCmd.ExecuteReader(CommandBehavior.CloseConnection);

objConn2.Close();

}

Can anyone give me some hints on where I going wrong? Specifically, how do
I call the procedure to populate the sata reader?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top