VB..net code error free but same C# code gives error?
I have converted my project from VB.net to C# using converter sw
Now i m facing some errors , which i try to remove
Here is the code in c# and vb.net also
But main thing is that it does not give error in vb.net
error is ,Error:- Use of unassingned local varialble "tmpYear"
public void cmdSearch_Click(System.Object sender, System.EventArgs e)
{
try
{
frmStudentSearch dlgStudentSearch = new frmStudentSearch();
int iStudentID;
string strCboSelection;
DataTable tmpDataTable;
string strSQL;
DataTable tmpYear;
DataTable tmpData;
DataTable tmpCboDataTable;
dlgStudentSearch.ShowForm();
//MsgBox("Student Fees" + vbCrLf + "iStudentID :: " + _CConstant._iStudentID.ToString)
iStudentID = modMain._CConstant._iStudentID;
if (iStudentID > 0)
{
strCboSelection = "SELECT ID, Name FROM tblStudent";
tmpCboDataTable = modMain._CDatabase.GetDataTable(strCboSelection);
CboStudentName.SelectedValue = iStudentID.ToString();
if (CboStudentName.Text.Trim().Length != 0)
{
strSQL = "SELECT BatchID FROM tblStudent ";
strSQL += "WHERE ID = " + CboStudentName.SelectedValue.ToString() + " ";
tmpYear = modMain._CDatabase.GetDataTable(strSQL);
if (tmpYear.Rows.Count > 0)
{
if (Information.IsDBNull(tmpYear.Rows[0]["BatchID"]) == true)
{
Interaction.MsgBox("Batch Not Present For This Student", MsgBoxStyle.Information, null);
cmdSave.Enabled = false;
return;
}
else
{
cmdSave.Enabled = true;
}
}
}
strSQL = "SELECT ClassID,SectionID FROM tblStudentDetail ";
strSQL += "WHERE StudentID = " + CboStudentName.SelectedValue.ToString() + " ";
strSQL += "AND OrganizationID = " + modMain._CVariable._ORGANIZATIONID.ToString() + " ";
tmpDataTable = modMain._CDatabase.GetDataTable(strSQL);
if (tmpDataTable.Rows.Count == 0)
{
Interaction.MsgBox("No Student Details Present For This Student", MsgBoxStyle.Information, null);
cmdSave.Enabled = false;
return;
}
else
{
cmdSave.Enabled = true;
}
if (cmdSave.Enabled == true)
{
strSQL = "SELECT Amount FROM tblLibraryStaticFees ";
strSQL += "WHERE BatchID = " + tmpYear.Rows[0]["BatchID"].ToString() + " ";
//In the above line of code it says that ,Error:- Use of unassingned local varialble "tmpYear"
strSQL += "AND ClassID = " + tmpDataTable.Rows[0]["ClassID"].ToString() + " ";
strSQL += "AND SectionID = " + tmpDataTable.Rows[0]["SectionID"].ToString() + " ";
strSQL += "AND OrganizationID = " + modMain._CVariable._ORGANIZATIONID.ToString() + " ";
tmpData = modMain._CDatabase.GetDataTable(strSQL);
if (tmpData.Rows.Count > 0)
{
txtRegAmount.Text = (string) (tmpData.Rows[0]["Amount"]);
}
else
{
txtRegAmount.Text = "0";
}
}
}
}
catch (Exception ex)
{
LibraryManagement.CError.ShowError(ex, true, false.ToString(), "");
}
}
Code in VB.net in which no error
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
Try
Dim dlgStudentSearch As New frmStudentSearch
Dim iStudentID As Int32
Dim strCboSelection As String
Dim tmpDataTable As DataTable
Dim strSQL As String
Dim tmpYear As DataTable
Dim tmpData As DataTable
Dim tmpCboDataTable As DataTable
dlgStudentSearch.ShowForm()
'MsgBox("Student Fees" + vbCrLf + "iStudentID :: " + _CConstant._iStudentID.ToString)
iStudentID = _CConstant._iStudentID
If iStudentID > 0 Then
strCboSelection = "SELECT ID, Name FROM tblStudent"
tmpCboDataTable = _CDatabase.GetDataTable(strCboSelection)
CboStudentName.SelectedValue = iStudentID.ToString
If Len(Trim(CboStudentName.Text)) <> 0 Then
strSQL = "SELECT BatchID FROM tblStudent "
strSQL += "WHERE ID = " + CboStudentName.SelectedValue.ToString + " "
tmpYear = _CDatabase.GetDataTable(strSQL)
If tmpYear.Rows.Count > 0 Then
If IsDBNull(tmpYear.Rows(0).Item("BatchID")) = True Then
MsgBox("Batch Not Present For This Student", MsgBoxStyle.Information)
cmdSave.Enabled = False
Exit Sub
Else
cmdSave.Enabled = True
End If
End If
End If
strSQL = "SELECT ClassID,SectionID FROM tblStudentDetail "
strSQL += "WHERE StudentID = " + CboStudentName.SelectedValue.ToString + " "
strSQL += "AND OrganizationID = " + _CVariable._ORGANIZATIONID.ToString + " "
tmpDataTable = _CDatabase.GetDataTable(strSQL)
If tmpDataTable.Rows.Count = 0 Then
MsgBox("No Student Details Present For This Student", MsgBoxStyle.Information)
cmdSave.Enabled = False
Exit Sub
Else
cmdSave.Enabled = True
End If
If cmdSave.Enabled = True Then
strSQL = "SELECT Amount FROM tblLibraryStaticFees "
strSQL += "WHERE BatchID = " + tmpYear.Rows(0).Item("BatchID").ToString + " "
strSQL += "AND ClassID = " + tmpDataTable.Rows(0).Item("ClassID").ToString + " "
strSQL += "AND SectionID = " + tmpDataTable.Rows(0).Item("SectionID").ToString + " "
strSQL += "AND OrganizationID = " + _CVariable._ORGANIZATIONID.ToString + " "
tmpData = _CDatabase.GetDataTable(strSQL)
If tmpData.Rows.Count > 0 Then
txtRegAmount.Text = tmpData.Rows(0).Item("Amount")
Else
txtRegAmount.Text = 0
End If
End If
End If
Catch ex As Exception
CError.ShowError(ex, True, False)
End Try
End Sub
So what is doing wrong in c# code?