You must Sign In to post a response.
  • Category: ASP.NET

    C# to VB conversion issue

    Hi All,

    Here I have attached my C# and VB code for your reference. My code in C# works perfectly, but in VB following line:-
    Dim txtHour As TextBox = DirectCast(grdrow.Cells(j).FindControl(String.Concat("TextBox", k.ToString())), TextBox)

    returns null value always. TextBox 'txtHour' not accepting any value in VB. Could anyone suggests solution for this problem? Thank you in advance

    C# Code
    ////////////////////////////////////////////////////////////////////////////////

    protected void Button1_Click(object sender, EventArgs e)

    {

    try

    {

    Session["MonthNumber"] = Convert.ToInt32(DropDownList1.SelectedValue);

    int days = GetDaysInMonth(Convert.ToInt32(DropDownList1.SelectedValue), Convert.ToInt32(DateTime.Now.Year));

    int MonthNumber = Convert.ToInt32(DropDownList1.SelectedValue);

    SqlConnection connection;

    string connectionString = @ConfigurationManager.ConnectionStrings["LSConnectionString"].ConnectionString;

    connection = new SqlConnection(connectionString);

    connection.Open();

    string stringID = null;




    foreach (GridViewRow grdrow in GridView1.Rows)

    {

    int k = 1;

    for (int j = 4; j < GridView1.Columns.Count; j++)

    {

    TextBox txtHour = (TextBox)grdrow.Cells[j].FindControl("TextBox" + k.ToString());

    if (txtHour.Text != null)

    {

    if (!string.IsNullOrEmpty(txtHour.Text))

    {

    System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand("InsertHour", connection);

    System.Data.SqlClient.SqlParameter Parameter = null;

    Command.CommandType = System.Data.CommandType.StoredProcedure;




    stringID = grdrow.Cells[0].Text;

    Guid aGuid = new Guid(stringID);




    Parameter = new System.Data.SqlClient.SqlParameter("@EmployeeID", System.Data.SqlDbType.UniqueIdentifier);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    Parameter.Value = aGuid;

    Command.Parameters.Add(Parameter);




    Parameter = new System.Data.SqlClient.SqlParameter("@Date", System.Data.SqlDbType.Date);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    Parameter.Value = new DateTime(DateTime.Now.Year, MonthNumber, k);

    //Parameter.Value = DateTime.Now.ToShortDateString();

    Command.Parameters.Add(Parameter);




    Parameter = new System.Data.SqlClient.SqlParameter("@Month", System.Data.SqlDbType.Int);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    Parameter.Value = MonthNumber;

    Command.Parameters.Add(Parameter);

    int number = 0;




    if (int.TryParse(txtHour.Text.Trim(), out number))

    {

    Parameter = new System.Data.SqlClient.SqlParameter("@Hour", System.Data.SqlDbType.Float);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    Parameter.Value = float.Parse(txtHour.Text);

    Command.Parameters.Add(Parameter);




    Parameter = new System.Data.SqlClient.SqlParameter("@AttendanceID", System.Data.SqlDbType.Int);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    Parameter.Value = 11;

    Command.Parameters.Add(Parameter);




    }

    else

    {

    string LeaveType = txtHour.Text.Trim();

    if (LeaveType == "A" || LeaveType == "C" || LeaveType == "F" || LeaveType == "J" || LeaveType == "L" || LeaveType == "O" || LeaveType == "P" || LeaveType == "R" || LeaveType == "S" || LeaveType == "W")

    {

    System.Data.SqlClient.SqlCommand Command1 = new System.Data.SqlClient.SqlCommand("SelectAttendanceID", connection);

    System.Data.SqlClient.SqlParameter Parameter1 = null;

    Command1.CommandType = System.Data.CommandType.StoredProcedure;

    string ID = String.Empty;

    int aID = 0;




    Parameter1 = new System.Data.SqlClient.SqlParameter("@ASymbol", System.Data.SqlDbType.NVarChar, 4);

    Parameter1.Direction = System.Data.ParameterDirection.Input;

    Parameter1.Value = LeaveType;

    Command1.Parameters.Add(Parameter1);




    System.Data.SqlClient.SqlDataReader Reader = null;




    Reader = Command1.ExecuteReader();

    while (Reader.Read())

    {

    if (Reader.HasRows)

    {

    ID = Reader["AttendanceID"].ToString();

    aID = Convert.ToInt32(ID);




    Parameter = new System.Data.SqlClient.SqlParameter("@Hour", System.Data.SqlDbType.Float);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    Parameter.Value = 0;

    Command.Parameters.Add(Parameter);




    Parameter = new System.Data.SqlClient.SqlParameter("@AttendanceID", System.Data.SqlDbType.Int);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    Parameter.Value = aID;

    Command.Parameters.Add(Parameter);




    }

    else

    {

    ClientScriptManager cs = Page.ClientScript;

    String cstext1 = "Please enter valid Leave Type in column " + j;

    cs.RegisterStartupScript(this.GetType(), "PopupScript", cstext1, true);

    ScriptManager.RegisterStartupScript(Page, this.GetType(), "jscript", "alert('Please enter data in column' + k);", true);




    }

    }

    }

    else if (LeaveType == "S.5" || LeaveType == "A.5" || LeaveType == "R.5")

    {

    Parameter = new System.Data.SqlClient.SqlParameter("@Hour", System.Data.SqlDbType.Float);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    Parameter.Value = 4;

    Command.Parameters.Add(Parameter);







    Parameter = new System.Data.SqlClient.SqlParameter("@AttendanceID", System.Data.SqlDbType.Int);

    Parameter.Direction = System.Data.ParameterDirection.Input;

    if (LeaveType == "S.5")

    Parameter.Value = 12;

    else if (LeaveType == "A.5")

    Parameter.Value = 13;

    else if (LeaveType == "R.5")

    Parameter.Value = 14;

    Command.Parameters.Add(Parameter);




    }

    }

    Command.ExecuteNonQuery();

    }

    k++;

    }

    }

    }

    }

    catch (Exception ex)

    {



    }

    }

    -------------------------------------------------------------------------------------------

    VB Code

    Protected Sub Button1_Click(sender As Object, e As EventArgs)


    Try


    Session("MonthNumber") = Convert.ToInt32(ddlMonth.SelectedValue)

    Dim days As Integer = GetDaysInMonth(Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(DateTime.Now.Year))

    Dim MonthNumber As Integer = Convert.ToInt32(ddlMonth.SelectedValue)

    Dim connection As SqlConnection

    Dim connectionString As String = ConfigurationManager.ConnectionStrings("csIntranet").ConnectionString

    connection = New SqlConnection(connectionString)

    connection.Open()

    Dim stringID As String = Nothing




    For Each grdrow As GridViewRow In tblAttendance.Rows


    Dim k As Integer = 1

    For j As Integer = 4 To tblAttendance.Columns.Count - 1


    Dim txtHour As TextBox = DirectCast(grdrow.Cells(j).FindControl(String.Concat("TextBox", k.ToString())), TextBox)

    If txtHour.Text IsNot Nothing Then


    If Not String.IsNullOrEmpty(txtHour.Text) Then


    Dim Command As New System.Data.SqlClient.SqlCommand("InsertHour", connection)

    Dim Parameter As System.Data.SqlClient.SqlParameter = Nothing

    Command.CommandType = System.Data.CommandType.StoredProcedure




    stringID = grdrow.Cells(0).Text

    Dim aGuid As New Guid(stringID)




    Parameter = New System.Data.SqlClient.SqlParameter("@EmployeeID", System.Data.SqlDbType.UniqueIdentifier)

    Parameter.Direction = System.Data.ParameterDirection.Input

    Parameter.Value = aGuid

    Command.Parameters.Add(Parameter)




    Parameter = New System.Data.SqlClient.SqlParameter("@Date", System.Data.SqlDbType.[Date])

    Parameter.Direction = System.Data.ParameterDirection.Input

    Parameter.Value = New DateTime(DateTime.Now.Year, MonthNumber, k)

    'Parameter.Value = DateTime.Now.ToShortDateString();

    Command.Parameters.Add(Parameter)




    Parameter = New System.Data.SqlClient.SqlParameter("@Month", System.Data.SqlDbType.Int)

    Parameter.Direction = System.Data.ParameterDirection.Input

    Parameter.Value = MonthNumber

    Command.Parameters.Add(Parameter)

    Dim number As Integer = 0




    If Integer.TryParse(txtHour.Text.Trim(), number) Then


    Parameter = New System.Data.SqlClient.SqlParameter("@Hour", System.Data.SqlDbType.Float)

    Parameter.Direction = System.Data.ParameterDirection.Input

    Parameter.Value = Single.Parse(txtHour.Text)

    Command.Parameters.Add(Parameter)




    Parameter = New System.Data.SqlClient.SqlParameter("@AttendanceID", System.Data.SqlDbType.Int)

    Parameter.Direction = System.Data.ParameterDirection.Input

    Parameter.Value = 11





    Command.Parameters.Add(Parameter)
    Else



    Dim LeaveType As String = txtHour.Text.Trim()

    If LeaveType = "A" OrElse LeaveType = "C" OrElse LeaveType = "F" OrElse LeaveType = "J" OrElse LeaveType = "L" OrElse LeaveType = "O" OrElse LeaveType = "P" OrElse LeaveType = "R" OrElse LeaveType = "S" OrElse LeaveType = "W" Then


    Dim Command1 As New System.Data.SqlClient.SqlCommand("SelectAttendanceID", connection)

    Dim Parameter1 As System.Data.SqlClient.SqlParameter = Nothing

    Command1.CommandType = System.Data.CommandType.StoredProcedure

    Dim ID As String = [String].Empty

    Dim aID As Integer = 0




    Parameter1 = New System.Data.SqlClient.SqlParameter("@ASymbol", System.Data.SqlDbType.NVarChar, 4)

    Parameter1.Direction = System.Data.ParameterDirection.Input

    Parameter1.Value = LeaveType

    Command1.Parameters.Add(Parameter1)




    Dim Reader As System.Data.SqlClient.SqlDataReader = Nothing




    Reader = Command1.ExecuteReader()

    While Reader.Read()


    If Reader.HasRows Then


    ID = Reader("AttendanceID").ToString()

    aID = Convert.ToInt32(ID)




    Parameter = New System.Data.SqlClient.SqlParameter("@Hour", System.Data.SqlDbType.Float)

    Parameter.Direction = System.Data.ParameterDirection.Input

    Parameter.Value = 0

    Command.Parameters.Add(Parameter)




    Parameter = New System.Data.SqlClient.SqlParameter("@AttendanceID", System.Data.SqlDbType.Int)

    Parameter.Direction = System.Data.ParameterDirection.Input

    Parameter.Value = aID





    Command.Parameters.Add(Parameter)
    Else



    Dim cs As ClientScriptManager = Page.ClientScript

    Dim cstext1 As [String] = "Please enter valid Leave Type in column " + j

    cs.RegisterStartupScript(Me.[GetType](), "PopupScript", cstext1, True)





    ScriptManager.RegisterStartupScript(Page, Me.[GetType](), "jscript", "alert('Please enter data in column' + k);", True)

    End If

    End While

    ElseIf LeaveType = "S.5" OrElse LeaveType = "A.5" OrElse LeaveType = "R.5" Then


    Parameter = New System.Data.SqlClient.SqlParameter("@Hour", System.Data.SqlDbType.Float)

    Parameter.Direction = System.Data.ParameterDirection.Input

    Parameter.Value = 4

    Command.Parameters.Add(Parameter)







    Parameter = New System.Data.SqlClient.SqlParameter("@AttendanceID", System.Data.SqlDbType.Int)

    Parameter.Direction = System.Data.ParameterDirection.Input

    If LeaveType = "S.5" Then

    Parameter.Value = 12

    ElseIf LeaveType = "A.5" Then

    Parameter.Value = 13

    ElseIf LeaveType = "R.5" Then

    Parameter.Value = 14
    End If





    Command.Parameters.Add(Parameter)

    End If
    End If


    Command.ExecuteNonQuery()
    End If


    k += 1

    End If

    Next

    Next

    Catch ex As Exception




    End Try

    End Sub

    ----------------------------------------------------------------------------------------------
  • #765841
    following line in C#
    TextBox txtHour = (TextBox)grdrow.Cells[j].FindControl("TextBox" + k.ToString());

    basically above line find a control in grivdiew with the name as 'TextBox'+k (where k is the variable name)
    here you can use below VB.net conversion for this particular line

    Dim txtHour As TextBox =TryCast(GridView1.FooterRow.FindControl("TextBox" + k.ToString()), TextBox).


    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765851
    Hi Prasad, I tried it, but didn't work out


  • Sign In to post your comments