locked
convert Vb to C# RRS feed

  • Question

  • Plz help me 

    i want to some one convert the Vb code in to C# plz

    Dim dgi As DataGridItem
            Dim cb As CheckBox
            Dim rollno As String
            Dim con As New SqlConnection(Application("cs"))
            Try
                con.Open()
                Dim cmd As New SqlCommand("insert into attendence values(@lname,@classid,@rollno,getdate(),@period,@status)", con)
                cmd.Parameters.Add("@lname", SqlDbType.VarChar, 10).Value = Session("lname")
                cmd.Parameters.Add("@classid", SqlDbType.VarChar, 10).Value = ddlClass.SelectedItem.Value
                cmd.Parameters.Add("@rollno", SqlDbType.Int)
                cmd.Parameters.Add("@period", SqlDbType.Int).Value = ddlPeriod.SelectedItem.Value
                cmd.Parameters.Add("@status", SqlDbType.Char)
                For Each dgi In DataGrid1.Items
                    cb = CType(dgi.FindControl("chkpresent"), CheckBox)
                    rollno = dgi.Cells(0).Text
                    ' insert rows into attendence 
                    cmd.Parameters("@rollno").Value = rollno
                    cmd.Parameters("@status").Value = IIf(cb.Checked, "y", "n")
                    cmd.ExecuteNonQuery()
                Next
                lblmsg.Text = "Attendence Is Stored. Use <a href=changeattendence.aspx>Change Attendence</a> to change attendence."
            Catch ex As Exception
                lblmsg.Text = ex.Message
            Finally
                con.Close()
            End Try

    plz help me


    unseendreamzzz

    Wednesday, April 11, 2012 11:48 AM

Answers

  • Hi unseendreamzzz,

    try below code. i used to convert your code http://www.carlosag.net/tools/codetranslator/

    DataGridItem dgi;
    CheckBox cb;
    string 
    rollno;
    SqlConnection con = new SqlConnection(Application("cs"));
    try 
    {
        con.Open()
    ;
        
    SqlCommand cmd = new SqlCommand("insert into attendence values(@lname,@classid,@rollno,getdate(),@period,@status)", con);
        
    cmd.Parameters.Add("@lname", SqlDbType.VarChar, 10).Value Session("lname");
        
    cmd.Parameters.Add("@classid", SqlDbType.VarChar, 10).Value ddlClass.SelectedItem.Value;
        
    cmd.Parameters.Add("@rollno", SqlDbType.Int);
        
    cmd.Parameters.Add("@period", SqlDbType.Int).Value ddlPeriod.SelectedItem.Value;
        
    cmd.Parameters.Add("@status", SqlDbType.Char);
        foreach 
    (dgi in DataGrid1.Items) {
            cb 
    ((CheckBox)(dgi.FindControl("chkpresent")));
            
    rollno dgi.Cells[0].Text;
            
    //  insert rows into attendence 
            
    cmd.Parameters["@rollno"].Value rollno;
            
    cmd.Parameters["@status"].Value ( cb.Checked ? "y" "n" );
            
    cmd.ExecuteNonQuery();
        
    }
        lblmsg.Text 
    "Attendence Is Stored. Use <a href=changeattendence.aspx>Change Attendence</a> to change attendence.";
    }
    catch (Exception ex) {
        lblmsg.Text 
    ex.Message;
    }
    finally {
        con.Close()
    ;
    }

    Regards

    Purvi

    • Proposed as answer by Jacob Wagner Wednesday, April 11, 2012 3:43 PM
    • Marked as answer by Mike Feng Saturday, April 21, 2012 7:30 AM
    Wednesday, April 11, 2012 1:39 PM

All replies

  • A free online tool which converts VB.net to C# and C# to VB.net is

    http://www.developerfusion.com/tools/

    http://www.developerfusion.com/tools/convert/csharp-to-vb/  


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Wednesday, April 11, 2012 1:01 PM
  • Also you can use the telerik convertor.

    http://converter.telerik.com/


    Kathir

    Wednesday, April 11, 2012 1:07 PM
  • Hi unseendreamzzz,

    try below code. i used to convert your code http://www.carlosag.net/tools/codetranslator/

    DataGridItem dgi;
    CheckBox cb;
    string 
    rollno;
    SqlConnection con = new SqlConnection(Application("cs"));
    try 
    {
        con.Open()
    ;
        
    SqlCommand cmd = new SqlCommand("insert into attendence values(@lname,@classid,@rollno,getdate(),@period,@status)", con);
        
    cmd.Parameters.Add("@lname", SqlDbType.VarChar, 10).Value Session("lname");
        
    cmd.Parameters.Add("@classid", SqlDbType.VarChar, 10).Value ddlClass.SelectedItem.Value;
        
    cmd.Parameters.Add("@rollno", SqlDbType.Int);
        
    cmd.Parameters.Add("@period", SqlDbType.Int).Value ddlPeriod.SelectedItem.Value;
        
    cmd.Parameters.Add("@status", SqlDbType.Char);
        foreach 
    (dgi in DataGrid1.Items) {
            cb 
    ((CheckBox)(dgi.FindControl("chkpresent")));
            
    rollno dgi.Cells[0].Text;
            
    //  insert rows into attendence 
            
    cmd.Parameters["@rollno"].Value rollno;
            
    cmd.Parameters["@status"].Value ( cb.Checked ? "y" "n" );
            
    cmd.ExecuteNonQuery();
        
    }
        lblmsg.Text 
    "Attendence Is Stored. Use <a href=changeattendence.aspx>Change Attendence</a> to change attendence.";
    }
    catch (Exception ex) {
        lblmsg.Text 
    ex.Message;
    }
    finally {
        con.Close()
    ;
    }

    Regards

    Purvi

    • Proposed as answer by Jacob Wagner Wednesday, April 11, 2012 3:43 PM
    • Marked as answer by Mike Feng Saturday, April 21, 2012 7:30 AM
    Wednesday, April 11, 2012 1:39 PM
  • 'foreach' variables must be declared in the control statement in C#:

    foreach (DataGridItem dgi in DataGrid1.Items)


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)

    • Proposed as answer by Mike Feng Tuesday, April 17, 2012 2:56 AM
    • Unproposed as answer by Mike Feng Tuesday, April 17, 2012 2:56 AM
    Wednesday, April 11, 2012 2:13 PM