SqlCommand sc = new SqlCommand(string.Format("SELECT doklad FROM netpokl WHERE id_dok='{0}'", newIdentity), spojeni); object obj = sc.ExecuteScalar(); if(obj == null) ; //Should show some message or throw exception string id_dok = obj.ToString().PadRight(20); //... SqlCommand sc2 = new SqlCommand(string.Format("UPDATE kliplat set doklad=@doklad WHERE id='{0}'",newIdentity), spojeni); //...
By the way: I do not think it is necessary. In fact, you should check if the length is> 20, then the line should be truncated. You should also use nvarchar(20) in the database table.
source share