I get number in SQL Server and show in label with number:
1.1.2
I want when the button is pressed the label will be 1.
Expectation result: 1.1.3
Bellow code I using in project:
private void FrmMain_Load(object sender, EventArgs e)
{
Checkversion();
}
public void Checkversion()
{
string maincon = ConfigurationManager.ConnectionStrings["Connstring"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(maincon);
string sqlquery = ("select * from tbl_Version ");
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
SqlDataAdapter da = new SqlDataAdapter();
{
sqlconn.Open();
da.SelectCommand = sqlcomm;
DataSet ds = new DataSet();
da.Fill(ds);
serverVersion = ds.Tables[0].Rows[0]["Version"].ToString();
lbVerionUpdate.Text = serverVersion;
sqlconn.Close();
}
}
private void Button1_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(lbVerionUpdate.Text);
i ;
var version = i.ToString();
lbVerionUpdate.Text = version;
}
I know, if it is integer then ok, but the result is decimal so the program gives error: System.FormatException: 'Input string was not in a correct format.'
I tried replacing from int to float but same result, Please help me fix it or provide alternative solution. Thanks
CodePudding user response:
The text "1.1.2" is not a number (not an integer, not a decimal too) threre is 2 points !
You can split text, convert the last entry of the array into an integer, increment it and then concat the array.
Like
private void Button1_Click(object sender, EventArgs e)
{
lbVerionUpdate.Text = IncremVersion(lbVerionUpdate.Text);
}
private string IncremVersion(string version)
{
if (!string.IsNullOrWhiteSpace(version))
{
var nfos = version.Split('.');
if (nfos != null && nfos.Length > 0)
{
string revisionTxt = nfos[nfos.Length - 1];
if (int.TryParse(revisionTxt, out int revision))
{
revisionTxt = (revision ).ToString();
}
nfos[nfos.Length - 1] = revisionTxt;
return string.Join('.', nfos);
}
}
// Fail to increment Version here !
return version
}
CodePudding user response:
The problem is now solved thanks to @jarlh.
I use the following code.
private void Button1_Click(object sender, EventArgs e)
{
string _versionDB = lbVerionUpdate.Text;
var version = new Version(_versionDB);
var newRevisionVersion = new Version(version.Major, version.Minor, version.Build 1);
_versionDB = newRevisionVersion.ToString();
lbVerionUpdate.Text = _versionDB;
}